In this section, we'll produce a color differentiated scatter plot with respect to a third variable. Let's begin by implementing the following steps:
- Choose a subset of dataset 1 (gapminder) and select a few countries. Use the following subset command:
dfs <- subset(df,Country %in%c("Germany","India","China","United States"))
- Make a scatter plot of the two variables and change the x and y titles:
p1<- ggplot(df,aes_string(x=var1,y=var2))+)geom_point(color=2,shape=2)+xlim(0,10000)+xlab(name1)+ylab(name2)
- Then, change the colors and shapes of the points for plot 1:
p2 <- ggplot(df,aes_string(x=var1,y=var2))+geom_t(aes(color=Country,shape=Country))+xlim(0,10000)+xlab(name1)+ylab(name2)
- Now, group points by Country, mapped by color and shape, for plot 3 and plot 4:
grid.arrange(p1, p2, nrow = 2)
Refer to the complete code at https://goo.gl/RheL2G.
The following plots should be obtained on completion of this example: