Question

I am pretty new to R and have been using Google (mainly directing me to this site) to fumble my way into making a passable graph for a project. I am having trouble figuring out what to search for to find other people with the same problem as me so I decided to ask instead.

I have a dataset which looks something like this:

    ATM TEMP PARENT variable value
1     1    5      1     DEAD     2
2     1    5      2     DEAD     0
3     1    5      3     DEAD     1
4     1   20      1     DEAD     1
55    1    5      1     LIVE    47
56    1    5      2     LIVE    42
57    1    5      3     LIVE    45
58    1   20      1     LIVE    45
109   1    5      1 SWIMMING     1
110   1    5      2 SWIMMING     8
111   1    5      3 SWIMMING     4
112   1   20      1 SWIMMING     4

ATM represents the pressure experiments were conducted at, TEMP the temperature, PARENT which one of 3 adults the larvae came from and variable represents the condition of the larvae at the given pressure/temperature with the value being how many (It was initially different but I merged them using reshape2).

I have been able to create this graph:

enter image description here

Using this code:

qplot(factor(ATM), value, data = CONDITION, geom = "boxplot", fill = factor(TEMP)) +
geom_point(aes(colour=factor(TEMP)) +
facet_wrap(~ variable, ncol = 1) +
scale_fill_manual(values = c("lightblue","#FF6666")) +
scale_colour_manual(values = c("lightblue","#FF6666")) +
labs(title = "Effect of Pressure on Condition of C.fornicata Larvae") +
xlab("Pressure \n (atm)") +
ylab("Number of Larvae") +
guides(fill=guide_legend(title="Incubation Temp (°C)"),colour=guide_legend(title="Incubation Temp (°C)"))

My problem is that the fill=factor(TEMP) is splitting up the boxplots into two (which I want) but the points from the geom_point do not line up with the now offset boxplots. I have tried messing around with the position argument in geom_point but haven't had any luck.

Thanks in advance!

Was it helpful?

Solution

Take a look at ?position_dodge.

# Making the example data set bigger    
library(ggplot2)
x = read.table(text='ATM TEMP PARENT variable value
1     1    5      1     DEAD     2
2     1    5      2     DEAD     0
3     1    5      3     DEAD     1
4     1   20      1     DEAD     1
55    1    5      1     LIVE    47
56    1    5      2     LIVE    42
57    1    5      3     LIVE    45
58    1   20      1     LIVE    45
109   1    5      1 SWIMMING     1
110   1    5      2 SWIMMING     8
111   1    5      3 SWIMMING     4
112   1   20      1 SWIMMING     4')
x = rbind(x,x)
x$ATM[13:24] = 50

# Example code that moves the points to the middle of the boxplots
ggplot( aes(x=factor(ATM),y=value), data=x ) +
  geom_boxplot( aes(fill=factor(TEMP))) +
  geom_point( aes(color=factor(TEMP)), 
              position=position_dodge(width=0.75) )

enter image description here

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