Question

I asked a similar question regarding this a few days ago but my needs have changed slightly and I am having trouble. I have a dataset that looks like this:

http://pastebin.com/a1PzfeG1

And I have used the following code:

ggplot( aes(x=factor(ATM),y=value), data=CONDITION ) +
  geom_boxplot( aes(fill=factor(TEMP))) +
  geom_point( aes(color=factor(PARENT)), position=position_dodge(width=0.75) ) + 
facet_wrap(~ variable, ncol = 1) + 
scale_fill_manual(values = c("#88CCFF","#FF6666")) +
scale_colour_manual(values = c("#336699","#990000","#009900")) +
labs(title = "(Fig. 1) Effect of temperature and hydrostatic pressure on the development of larval Crepidula fornicata") +
xlab("Pressure \n (atm)") +
ylab("Number of Larvae") +
guides(fill=guide_legend(title="Temperature (°C)"),colour=guide_legend(title="Parent"))

To make a graph that looks like this:

Graph

Now, if I set the colour of the the geom_point as factor(TEMP) then the position dodge lines them up with the corresponding boxplots, but when I changed it to be based on the parent it now offsets them based on that variable instead. What I am wondering is if there is a way to line up the points based on the TEMP variable but colour them based on the PARENT.

Thanks in advance,

Kez

Was it helpful?

Solution

This is happening because TEMP has two levels whereas PARENT has three. ggplot will dodge based on how many levels you have. Since you have two boxplots dodge will line up properly when your points have two levels. If you had three boxplots, then PARENT dodge points would line up fine. Here is a workaround using group:

geom_point( aes(group=factor(TEMP), color=factor(PARENT)), position=position_dodge(width=0.75)) 

This works because by default color will assign groups if groups are not otherwise defined. Since here we actively specify groups, those prevail and dodge dodges based on the explicit groups rather than the color implied groups.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top