0

I see the error

Error:Insufficient values in manual scale. 8 needed but only 4 provided

when I plot from the following code. If I exclude the geom_count layer the plot works, likewise if I exclude the geom_sf layer:

ggplot() +
  geom_sf(data = roads %>% filter(simple_classification=="A Road" | simple_classification=="B Road"
                                             | simple_classification=="Motorway" | simple_classification=="Minor"), 
          aes(colour=simple_classification)) + 
  scale_color_manual(values = road_colours, name="Roads",
                     breaks=c("Minor","A Road", "B Road", "Motorway"),
                     labels=c("Minor","A Road", "B Road", "Motorway")) +
   geom_count(data=journey_origins, 
             mapping=aes(x=easting, y=northing, colour=types), alpha=0.5, position=position_jitter(height=65,width=65))

Many thanks for any advice

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
Dave S
  • 43
  • 7
  • In geom_sf you map `simple_classifiaction` on `color` and in `geom_count` you map `types` on color. From the error message I guess that the categories of `types` are different from the categories of `simple_classification`, i.e. you want to map eight categories on `color` but provide only colors for four in scale_color_manual. As a first step I would simply remove the scale_color_manual. This will show you what categories are present in your data. – stefan Jan 20 '21 at 07:25
  • Thank you. That helps my understanding, removing the scale_color_manual creates a single colour range, with four colors from `roads` and four from `journey_orgins`. The colors of the two variables are in a single legend of 8 colors. Is there a way of keeping the color mapping separate between individual ggplot layers, so that two separate legends are shown - the ideal would be able to manually specify one of them so it stays constant between different plots. – Dave S Jan 20 '21 at 12:27
  • 1
    If you want a second color legend then have a look at the `ggnewscales` package. And sure is it possible to fix a legend for multiple plots. Depends a bit on what your are exactly trying to achieve. – stefan Jan 20 '21 at 12:34
  • Thank you, that worked I did not know about `ggnewscale` . Also whilst searching around that package came across [your answer](https://stackoverflow.com/questions/63858705/why-do-i-not-get-two-legends-using-ggplot2) to a question that was the same as mine that I hadn't seen, including a different approach to avoiding the problem. – Dave S Jan 20 '21 at 14:52

0 Answers0