Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scatter plot and boxplot overlay

Tags:

r

ggplot2

Based on the previous post ggplot boxplots with scatterplot overlay (same variables),

I would like to have one boxplot for each day of week instead of two boxplots while have scatter points on it with different colour.

The code will be like:

#Box-plot for day of week effect
plot1<-ggplot(data=dodgers, aes(x=ordered_day_of_week, y=Attend)) + geom_boxplot()

#Scatter with specific colors for day of week
plot2<-ggplot(dodgers, aes(x=ordered_month, y=Attend, colour=Bobblehead, size=1.5)) + geom_point()

#Box-ploy with Scatter plot overlay
plot3<-ggplot(data=dodgers, aes(x=ordered_day_of_week, y=Attend, colour=Bobblehead)) + geom_boxplot() + geom_point()

And the result would be:
1, scatter plot

enter image description here

2, boxplot plot enter image description here

3, combined plot enter image description here

like image 212
useR Avatar asked Feb 27 '14 16:02

useR


1 Answers

Put color= inside the aes() of geom_point() and remove it from ggplot() aes(). If you put color= inside the ggplot() then it affects all geoms. Also you could consider to use position dodge to separate points.

Example with mtcars data as OP didn't provide data.

ggplot(mtcars,aes(factor(cyl),mpg))+geom_boxplot()+
  geom_point(aes(color=factor(am)),position=position_dodge(width=0.5))

enter image description here

like image 60
Didzis Elferts Avatar answered Nov 19 '22 06:11

Didzis Elferts