Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add legend manually to ggplot2 does not work [duplicate]

Tags:

r

legend

ggplot2

I´ve searched not just stackoverflow, but many other websites too. Unfortunately, I could not find any help yet. I am going to explain my problem as explicitly as I can. Since this is my first question here on stackoverflow, please be gentle I am a complete beginner in R. My goal is to add a legend manually to my already created ggplot2 object.

This is the dataset I am working with:

structure(list(Values = 0:5, Count = c(213L, 128L, 37L, 18L, 
3L, 1L), rel_freq = c(0.5325, 0.32, 0.0925, 0.045, 0.0075, 0.0025
), pois_distr = c(0.505352031744286, 0.344902761665475, 0.117698067418343, 
0.0267763103376731, 0.00456870795136548, 0.000623628635361388
)), class = "data.frame", row.names = c(NA, -6L))

which looks like

  Values Count rel_freq   pois_distr
1      0   213   0.5325 0.5053520317
2      1   128   0.3200 0.3449027617
3      2    37   0.0925 0.1176980674
4      3    18   0.0450 0.0267763103
5      4     3   0.0075 0.0045687080
6      5     1   0.0025 0.0006236286

Next, I already succeeded in creating a ggplot which is ok and the code is:

cols <- c('Beob. Häufigkeiten' = 'lightblue', 'Theor. Häufigkeiten' = 'darkblue')
plot_yeast1 <- ggplot(data.frame(data1_plot), aes(x=Values)) + 
  geom_col(aes(y=rel_freq, fill = 'Beob. Häufigkeiten'), col = 'lightblue4', alpha = 0.8) + 
  geom_point(aes(y=pois_distr, colour = 'Theor. Häufigkeiten'), alpha = 0.9, size = 4) +
  scale_fill_manual(name = 'Legende', values = cols) +
  scale_colour_manual(name ='Legende', values = cols) + 
  scale_y_continuous(breaks = seq(0, 0.6, 0.05)) +
  labs(title = 'Gegenüberstellung der beobachteten Häufigkeiten mit den theoretischen \nHäufigkeiten aus dem geschätzten Poissonmodell', x = 'Auftretende Fehler von Hefezellen', y = 'Relative Häufigkeit', subtitle = 'Konzentration 1') +
  theme_bw()
plot_yeast1

and the output is:

Plot 1 with both headers

My goal is, to merge both of the manually created legends on the right side of the plot into one. I have already tried to skip the second header of the legend, then it looks like

Plot without second header.

But the wide space is ugly and there must be a possibility to merge these two legends into one, where the two entries are close together. I have already been on it for more than 9 hours, and searched many posts, which did not solve my problem. If anything is unclear, please let me know. As I already wrote, this is the first time asking a problem. Thank you

like image 474
m0byn Avatar asked Jun 27 '26 04:06

m0byn


1 Answers

If it is primarily about visually creating "one" legend out of the two, this approach might help - details see comments to theme(...) - call at the end:

cols <- c('Beob. Häufigkeiten' = 'lightblue', 'Theor. Häufigkeiten' = 'darkblue')
plot_yeast1 <- ggplot(data.frame(data1_plot), aes(x=Values)) + 
  geom_col(aes(y=rel_freq, fill = 'Beob. Häufigkeiten'), col = 'lightblue4', alpha = 0.8) + 
  geom_point(aes(y=pois_distr, colour = 'Theor. Häufigkeiten'), alpha = 0.9, size = 4) +
  scale_fill_manual(name = 'Legende', values = cols) +
  scale_colour_manual(name ='', values = cols) + 
  scale_y_continuous(breaks = seq(0, 0.6, 0.05)) +
  labs(title = 'Gegenüberstellung der beobachteten Häufigkeiten mit den theoretischen \nHäufigkeiten aus dem geschätzten Poissonmodell', x = 'Auftretende Fehler von Hefezellen', y = 'Relative Häufigkeit', subtitle = 'Konzentration 1') +
  theme_bw() +
  theme(legend.box.background = element_rect(colour = "grey", fill = "white"), # create a box around all legends
        legend.box.margin = margin(0.1, 0.1, 0.1, 0.1, "cm"),                  # specify the margin of that box
        legend.background = element_blank(),                                   # remove boxes around legends (redundant here, as theme_bw() seems to do that already)
        legend.spacing = unit(-0.5, "cm"),                                     # move legends closer together
        legend.margin = margin(0, 0.2, 0, 0.2, "cm"))                          # specify margins of each legend: top and bottom 0 to move them closer
plot_yeast1

enter image description here

like image 61
Wolfgang Arnold Avatar answered Jun 29 '26 18:06

Wolfgang Arnold



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!