如何用线性模型的函数填充背景色? [英] How can I fill the background with a color scale in function of a lineal model?
问题描述
我正在分析阿尔贝托·开罗的功能性艺术中的datavis的一个例子(我推荐给你)
在这本书中,有这样的例子
我在R试试。在左下图(散点图)中
<我使用这本书中的数据,并且我计算了在人口函数中的有效军队,并且人口的效用预算与效益函数中的效用相关, / p>
这里是一个问题:如何绘制散点图的背景与颜色比例(渐变),颜色代表BUDGET的值?
之后,我想用预算的颜色来表示分数(人口,efects)。
通过这种方式,我可以比较国家的预算和人口+效益函数中的预计预算。我知道基本的R,我已经安装了 ggplot2 和缩放比例包
我想要一个类似于
的图表
但是正确的颜色。
谢谢。
geom_raster
来获得你想要的效果。我将使用与@GeekOnAcid建议的数据集相同的数据集:
crime = read.csv(http://datasets.flowingdata.com/crimeRatesByState2005.tsv,
header = TRUE,sep =\t )
##拟合回归模型
m = lm(犯罪$ burglary〜犯罪$谋杀)
接下来我们为背景颜色创建一个网格:
##为背景颜色创建一个网格
x = seq(1,10,length.out = 100)
y = seq(400,1200,length.out = 100)
z = expand.grid(x,y)
现在我们需要渐变颜色的距离度量。我只是用回归线的平方距离:
$ $ $ $ $ $ $ $ $ $ $然后绘制:
b $ b
require(ggplot2)
ggplot(z)+ geom_raster(aes(Var1,Var2,fill = grad))+
geom_point(data =犯罪[1:15],aes(谋杀,入室盗窃,人数=人口),pch = 1)+
geom_text(数据=犯罪[1:15,],
aes(谋杀,入室盗窃, label = state),
hjust = -0.2,size = 4)+
scale_size_continuous(range = c(1,10))+
scale_fill_continuous(high =red,low = (大小= FALSE,填充=假)+
scale_y_continuous(expand) = c(0,0))+
scale_x_continuous(expand = c(0,0))
获得:
I'm analysing an example of datavis in "The Functional Art " of Alberto Cairo (I recomend to you)
In that book, there are this example
And I try in R. In the bottom left graph (scatter plot)
I use the data from the book, and i calculate the efectives militaries in function of population with lm(efect ~ pop)
and the budget in function of population + efect
And here is the question: How can I paint the background of the scatterplot with colorscale (gradient) where the color represent the value of BUDGET?
After that I want to put the points (population, efects) with the color of the budget. In this way, I can compare the budget of the country with the stimated budget in function of population + efect
I know basic R, I have installed ggplot2 and scales packages
I want a graph similar to
but with the correct colors.
Thank you.
To get the background you can use geom_raster
to get the effect you want. I'll use the same data set as suggested by @GeekOnAcid:
First get the data and fit a regression model:
crime = read.csv("http://datasets.flowingdata.com/crimeRatesByState2005.tsv",
header=TRUE, sep="\t")
##Fit the regression model
m = lm(crime$burglary ~ crime$murder)
Next we create a grid for the background colour:
##Create a grid for the background colour
x = seq(1, 10, length.out=100)
y = seq(400, 1200, length.out=100)
z = expand.grid(x,y)
Now we need a distance measure for the gradient colour. I just used squared distance from the regression line:
z$grad = (z[,2] - (398.3 + 62.2*z[,1]))^2
Then plot:
require(ggplot2)
ggplot(z) + geom_raster(aes(Var1, Var2, fill=grad)) +
geom_point(data=crime[1:15,], aes(murder, burglary, size=population),pch=1 ) +
geom_text(data=crime[1:15,],
aes(murder, burglary, label=state),
hjust=-0.2, size=4) +
scale_size_continuous(range=c(1,10)) +
scale_fill_continuous(high="red", low="white", trans="sqrt") +
xlab("Murder") + ylab("Burglary") +
guides(size=FALSE, fill=FALSE) +
scale_y_continuous(expand=c(0, 0)) +
scale_x_continuous(expand=c(0, 0))
To get:
这篇关于如何用线性模型的函数填充背景色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!