0

I have a 5 data frames (actually one and 4 sub sets of the same with some filters), and the idea is to plot them according to colors and have corresponding colors on the legend. But in the legend the colors are mixed, the order of the names are kept. I am not sure how are they ordered but not according to my input.

My code is:

 ggplot(regression_with_pred, aes(x= countF, y= countM)) + 
  geom_point(alpha = 0.5, size=0.1) +
  geom_smooth(method = lm, se=F, color ="#e08214") +
  geom_ribbon(aes(ymin=lwr, ymax=upr, fill="#e08214"), alpha = 0.2) +
  ggtitle(samplename) +
  theme_bw() +
  geom_point(data=y_mers, aes(x= countF, y= countM, color = "y_mers"), alpha = 0.5, size=0.1) +
  geom_point(data=x_mers, aes(x= countF, y= countM, color = "x_mers"), alpha = 0.5, size=0.1) +
  geom_point(data=z_mers, aes(x= countF, y= countM, color = "z_mers"), alpha = 0.5, size=0.1) +
  geom_point(data=w_mers, aes(x= countF, y= countM, color = "w_mers"), alpha = 0.5, size=0.1) +
  scale_color_manual(name = "Kmer association",
                      values = c("y_mers" = "#053061", "x_mers" = "#b2182b", "z_mers" = "#2166ac", "w_mers"="#d6604d"),
  labels = c("y_mers","x_mers","z_mers","w_mers")) +
  scale_fill_identity()

The wrong plot:

It's wrong because "y_mers" should have the color of "x_mers", "x_mers" the color of "zmers", "zmers" the color of "w_mers", and "w_mers" the color of "y_mers".

Like this: The correct order

Keyur Potdar
  • 7,158
  • 6
  • 25
  • 40
  • I'd suggest trying to use the single data frame instead of the four filtered subsets, which would allow you to set the color aesthetic in the `geom_point()` layer more easily. If you provide some example data, we can help you with that: see https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example for how to do that. – qdread Jan 10 '18 at 17:48
  • @qdread thanks for your fast reply and sorry for haven't said anything before. I did as you mentioned and I was easier. I also use the base commands to achieve this and it also was very easy. Thanks again! – N. Lichilín Feb 05 '18 at 15:11

0 Answers0