问题
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.
回答1:
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:

回答2:
To get you started, take a look at this tutorial by Nathan Yau, who shows how to create bubble chart with basic graphics in R. I only retrieved his solution to make it more relevant for your example.
#get some example data
crime <- read.csv("http://datasets.flowingdata.com/crimeRatesByState2005.tsv",
header=TRUE, sep="\t")
#define the radius of circles
radius <- sqrt( crime$population/ pi )
#makes your plot, bg defines colour, inches scales circles
symbols(crime$murder[1:15], crime$burglary[1:15], circles=radius[1:15],
inches=0.5, bg=gray(0.9), xlab="Murder Rate", ylab="Burglary Rate")
#makes your labels, you can offset them by adding values to x and y arguments
text(crime$murder[1:15], crime$burglary[1:15], crime$state[1:15], cex=0.6)

The background thing will be tricky, but are you sure you really need it? Even with different colours it will look ugly...
来源:https://stackoverflow.com/questions/13221830/how-can-i-fill-the-background-with-a-color-scale-in-function-of-a-lineal-model