0

I'm having some issue overlaying 2 point graphs on a box plot. The code seems to work well when i added only one point graph. Here is the code below:

ggplot(data1, aes(x= reorder(DMU,order), y = Efficiency)) + 
  geom_boxplot() + 
  geom_point(data = data2, aes(x = dmu, y = eff, color = "eff")) + 
  scale_color_manual("", breaks = c("eff"), values = c("blue")) + 
  geom_point(data = data3, aes(x = DMU, y = eff2, color = "eff2")) + 
  scale_color_manual("", breaks = c("eff2"), values = c("red")) 

I keep getting the error below: Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale. Error: Insufficient values in manual scale. 2 needed but only 1 provided.

Leonardo
  • 2,439
  • 33
  • 17
  • 31
george
  • 11
  • 2
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Each ggplot can only have one color scale so you can't add scale_color_manual twice to the same object. – MrFlick Jan 29 '21 at 19:26

1 Answers1

0

You cannot add scale_color_manual() twice. Build a single dataframe for the colon:

df_points <- data.frame(x = c(data2$dmu, data3$DMU), 
                        y = c(data2$eff, data3$eff2), 
                        data = c("data2", "data3")
      )

And then:

ggplot(data1, aes(x = reorder(DMU,order), y = Efficiency)) + 
  geom_boxplot() + 
  geom_point(data = df_points, aes(x = x, y = y, color = data)) + 
  scale_colour_manual(values = c("red", "blue") + 
  theme(legend.position = "none")

Not having the data available I could have made a mistake

Leonardo
  • 2,439
  • 33
  • 17
  • 31