R (ggplot) function to add an 'empty' row in an existing legend, split legend?

て烟熏妆下的殇ゞ 提交于 2020-06-16 18:40:14

问题


I'm currently working on a plot. It's all coming together, but i'm wondering about one thing... I tried googling this, but I couldn't found what I was looking for.

Right now, I've created a legend that I liked. With 'scale_color_manual' and 'scale_size_manual', I've created a combined legend that includes the thickness of the corresponding line and the color. I have put the code I used below.

scale_color_manual(name = "combined legend",
                     labels = c("Nederland totaal", "Noord-Holland", "Utrecht", "Noord-Brabant", "Zuid-Holland", "Gelderland", "Flevoland", "Overijssel", "Limburg", "Drenthe", "Zeeland", "Friesland", "Groningen"),
                     values=c("#000000", "#001EFF", "#2ECC71","#FF009D","#00FFCD",
                              "#FF8400", "#8514EB", "#EB1A14", "#FFE100", 
                              "#FF00AA", "#00AAFF", "#16A085", "#B903FC")) +   

scale_size_manual(name = "combined legend", 
                        labels = c("Nederland totaal", "Noord-Holland", "Utrecht", "Noord-Brabant", "Zuid-Holland", "Gelderland", "Flevoland", "Overijssel", "Limburg", "Drenthe", "Zeeland", "Friesland", "Groningen"),
                        values = c(1.75, 0.8, 0.8,0.8,0.8,0.8,0.8,0.8,0.8,0.8,0.8,0.8,0.8))

This is what the legend looks like right now

This is what the legend looks like right now.

My question is: Is it possible to create a little bit of space between the first 'legend' part (so "Nederland totaal" and the other lines?).

I want it to look more like this

I want it to look more like this

(I made this for clarification via word).

Is there a function to add some space between certain legend items? I hope somebody could help me :))

More detailed:

The dataset I'm working with, are the average yearly housing prices per Dutch province for 2005 until 2019. I created a ggplot, the current plot looks like this now. It is basically a ggplot with the year on the horizontal axis and the average housing price on the vertical axis. I have sorted the color by province, and added the black, thicker line that corresponds the total average of the netherlands (which I also put in the province vector). I used geom_line all of the legend items are factors. I hope this was clear enough, if not, let me know


回答1:


Could this be applied to your data?

library(tidyverse)

tib <- 
  tibble(x = 1:3,
         a = 1:3,
         b = 1.5:3.5,
         c = 2:4) %>% 
  pivot_longer(cols = a:c, names_to = "id", values_to = "val")


ggplot()+
  geom_line(data = filter(tib, id == "a"), aes(x, val, linetype = id))+
  geom_line(data = filter(tib, id != "a"), aes(x, val, colour = id))+
  labs(linetype = "legend", colour = NULL)


Gives you:




回答2:


One option is to use stat_summary.

This will add another aesthetic to the graph, and thus another legend, far apart from the other one.

airquality %>%
  mutate(Month=factor(Month)) %>%
  ggplot(aes(x=Day, y=Temp, col=Month)) +
  geom_line() +
  stat_summary(aes(lwd="Nederland totaal"), fun=mean, geom="line", col="black") +
  theme_classic() +
  theme(legend.title = element_blank()) + 
  guides(lwd = guide_legend(order = 1))

But the benefit of this method is that you don't have to calculate the averages per year and manually add it to your Province variable.



来源:https://stackoverflow.com/questions/61273294/r-ggplot-function-to-add-an-empty-row-in-an-existing-legend-split-legend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!