How to fill a background using a color bar in a linear model function?

I am analyzing the datavis example in Functional Art by Alberto Cairo (I recommend you)

This book has this example.

http://img546.imageshack.us/img546/8327/snap1026.png

And I'm trying in R. In the lower left graph (scatter plot)

I use the data from the book and I calculate the effective armed forces as a population function with lm(efect ~ pop) and the budget as a population function + efect

And here's the question: how can I draw the background of the scatterplot using scalecale (gradient), where the color represents the BUDGET value?

After that I want to put points (population, efects) in the color of the budget. Thus, I can compare the country's budget with a stimulated budget in the function of the population + efect

I know the base R, I installed the ggplot2 packages and

I need a graph similar to

enter image description here

but with the right colors.

Thanks.

+4
source share
2 answers

To get the background, you can use geom_raster to get the desired effect. I will use the same dataset as suggested by @GeekOnAcid:

First get the data and set the 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) 

Then we create a grid for the background color:

 ##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 for the gradient color. I just used the square of the distance from the regression line:

 z$grad = (z[,2] - (398.3 + 62.2*z[,1]))^2 

Then the 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 obtain:

enter image description here

+5
source

To get started, take a look at this Nathan Yau tutorial , which shows how to create a bubble chart with basic graphics in R I just got 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) 

enter image description here

The background thing will be complicated, but are you sure you really need it? Even with different colors, it will look ugly ...

+2
source

Source: https://habr.com/ru/post/1443984/


All Articles