1

I have been struggling in fixing the legend for a complex ggplot2 plot.

The plot is as follow:

colors = brewer.pal(8, "Dark2")

p <- ggplot(dataf, aes(x = Category)) + 
  geom_linerange(aes(ymin = low_quantile, ymax = top_quantile),
                 linetype = 1, color = "#5B9BD5", size = 7) + 
  geom_point(aes(y = low_quantile, color=low_quantile_plot, shape=low_quantile_plot),
             size = 3) + 
  geom_point(aes(y = top_quantile,color=top_quantile_plot, shape=top_quantile_plot),
             size = 3) +
  geom_errorbar(aes(y = Mean, ymin = Mean, ymax = Mean,color='Mean'),
                lty=1, size=1, width=0.24) + 
  scale_color_manual(values = c('Mean'='red', 'top_quantile_plot'='green', 'low_quantile_plot'='blue'), 
                     labels = c('Mean', top_quantile_plot, low_quantile_plot)) + 
  scale_shape_manual(values = c(16, 17)) + 
  geom_text(aes(x = Category, y = (top_quantile + 10), 
                label = paste('€', specify_decimal(top_quantile, 2), 'm'))) 

The problem is with mapping the correct colors and shapes together.

I did not provide the data set, as I would just like to know if the way I'm approaching the ggplot syntax is correct or no.

The output should be the line ranges, with top/low quantile (in different colors and shapes) and the error bar presenting the mean in the middle of the line range (different color).

I'm able to generate the plot correctly, but the problem is with the correct mapping of the legends. At the moment is looks like this, but this is not nice.

enter image description here

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
ifreak
  • 1,726
  • 4
  • 27
  • 45
  • See [here](https://stackoverflow.com/questions/12410908/combine-legends-for-color-and-shape-into-a-single-legend) or [here](https://stackoverflow.com/questions/37140266/how-to-merge-color-line-style-and-shape-legends-in-ggplot) – Roman Oct 12 '17 at 09:41
  • @Jimbou managed to combine the shape and the color, but the line is not appearing in the legend. – ifreak Oct 12 '17 at 10:18
  • if I understand your problem, try to use this: `scale_shape_manual(values=c(120,25)) + ggtitle("title")+theme(plot.title = element_text(family = "Trebuchet MS", color="black", face="italic", size=15, hjust=0.5)` –  Oct 12 '17 at 10:31
  • 2
    @ifreak why not simple boxplot? – pogibas Oct 12 '17 at 11:12

0 Answers0