0

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?

5
  • 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 :) Commented Feb 26, 2017 at 1:51
  • Many thanks Corinne, I hope the table above helps? BAlpine Commented Feb 26, 2017 at 12:09
  • 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". Commented Feb 26, 2017 at 12:53
  • 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. Commented Feb 27, 2017 at 3:08
  • Thanks Corinne, please find new table above Commented Feb 27, 2017 at 9:02

1 Answer 1

1

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)))
Sign up to request clarification or add additional context in comments.

3 Comments

Many thanks cimentadaj, I am aware of the double dots for the outliers. Also, I wanted to fill the boxplots with specific colors. My problem is if you observe the boxplot that there should be 3 dots per color (three colors in total according to variable 'hor') for each of the 9 boxplots. Infact most of the times there are six dots of one color and three of another color, but there is one color missing. @cimentadaj
Yes, sorry, I misunderstood, but I figured out your mistake. You need to erase mydata$ from the geom_point call. Just leave plot. I'll edit the answer.
Would you mind accepting the answer by clicking on the check sign?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.