0
votes

I am currently trying to do a box plot with superimposed points. The points in the boxplot though have to be different in color according to a factor variable. The code goes well until here:

  MioBox <- ggplot(mydata, aes(x=mng, y=Active, fill=mng))+ 
  geom_boxplot(color="black", notch=TRUE)+ 
  geom_point(position="jitter", color="blue", alpha=.5)+ 
  geom_rug(side="l", color="black")+
  facet_grid(.~hor,scales = "free", space = "free")+
  labs(title='bla bla bla')

(see picture below)

When I try to add colours to the points in the boxplot according to variableplot

MioBox <- ggplot(mydata, aes(x=mng, y=Active, fill=mng))+ 
geom_boxplot(color="black", notch=TRUE)+ 
#geom_point(position="jitter", color="blue", alpha=.5)+ 
geom_rug(side="l", color="black")+
facet_grid(.~hor,scales = "free", space = "free")+
labs(title='bla bla bla')
MioBox + scale_fill_manual(values=c("#669966", "#CC9966", "#CCCC66"))+
geom_point(position="jitter",aes(color = factor(mydata$plot)))

I get a boxplot which does not match the variable plot which assigns 3 dots per color in each of the boxplots. The results is I always have 9 dots but most of the times there is 6 of one color and three of the other, and there is one color missing: superimposed points coloured by factor variable (there should be 3 dots per color in each boxplot)

And here is the table that reproduces the problem:

mydata <- read.table(header=TRUE, text="
Active hor plot mng
7.20    F   1   CH
8.80    O   1   CH
9.30    F   1   CH
9.20    O   1   CH
9.70    F   1   CH
9.30    O   1   CH
9.10    F   2   CH
7.50    O   2   CH
7.50    F   2   CH
8.70    O   2   CH
9.90    F   2   CH
7.60    O   2   CH
9.70    F   3   CH
7.70    O   3   CH
8.90    F   3   CH
8.60    O   3   CH
8.30    F   3   CH
8.30    O   3   CH
8.50    L   1   CH
7.40    L   1   CH
8.00    L   1   CH
9.70    L   2   CH
8.90    L   2   CH
8.40    L   2   CH
9.80    L   3   CH
8.00    L   3   CH
7.00    L   3   CH
7.30    F   1   Fe
6.60    O   1   Fe
6.50    F   1   Fe
6.60    O   1   Fe
6.90    F   1   Fe
5.80    O   1   Fe
6.60    F   2   Fe
7.00    O   2   Fe
6.00    F   2   Fe
5.10    O   2   Fe
6.10    F   2   Fe
5.10    O   2   Fe
5.10    F   3   Fe
6.50    O   3   Fe
7.70    F   3   Fe
6.90    O   3   Fe
5.20    F   3   Fe
6.30    O   3   Fe
6.50    L   1   Fe
5.00    L   1   Fe
7.80    L   1   Fe
5.10    L   2   Fe
5.50    L   2   Fe
5.60    L   2   Fe
5.50    L   3   Fe
7.80    L   3   Fe
7.70    L   3   Fe
7.20    F   1   W
8.80    O   1   W
7.80    F   1   W
7.80    O   1   W
7.90    F   1   W
8.10    O   1   W
8.60    F   2   W
7.40    O   2   W
7.40    F   2   W
8.40    O   2   W
7.70    F   2   W
8.90    O   2   W
6.70    F   3   W
6.10    O   3   W
7.50    F   3   W
8.60    O   3   W
7.80    F   3   W
8.60    O   3   W
8.30    L   1   W
8.20    L   1   W
8.70    L   1   W
8.60    L   2   W
6.80    L   2   W
6.30    L   2   W
7.30    L   3   W
7.10    L   3   W
7.70    L   3   W
")

Anybody can help me with this?

1
Can you add a subset of the data so we can reproduce the issue? See advice here on how you might want to do that :)corinne r
Many thanks Corinne, I hope the table above helps? BAlpineBAlpine
Corinne I just found out that it works with the table above, but it does not work when I add different values for variable "hor".BAlpine
Can you edit the table so that it doesn't work? -- Hard to know if hor needs to vary within levels of some other variable to reproduce your issue.corinne r
Thanks Corinne, please find new table aboveBAlpine

1 Answers

1
votes

The problem lays in that your calling mydata$plot when you should only call plot. The ggplot object already has a predefined data frame.

You can fix it by rewriting your last lines like this:

MioBox +
scale_fill_manual(values=c("#669966", "#CC9966", "#CCCC66"))+
geom_point(position="jitter",aes(color = factor(plot)))