R(ggplot)函数可在现有图例中添加“空"行,拆分图例? [英] R (ggplot) function to add an 'empty' row in an existing legend, split legend?

查看:42
本文介绍了R(ggplot)函数可在现有图例中添加“空"行,拆分图例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在做一个情节.全部融合在一起,但是我想知道一件事...我尝试使用谷歌搜索,但是找不到我想要的东西.

现在,我已经创建了一个我喜欢的图例.通过"scale_color_manual"和"scale_size_manual",我创建了一个合并的图例,其中包括相应线条的粗细和颜色.我把下面使用的代码放进去了.

  scale_color_manual(name ="combined legend",标签= c("Nederland totaal","Noord-Holland","Utrecht","Noord-Brabant","Zuid-Holland","Gelderland","Flevoland","Overijssel","Limburg","Drenthe","Zeeland","Friesland","Groningen"),值= c(#000000",#001EFF",#2ECC​​71",#FF009D",#00FFCD",#FF8400",#8514EB",#EB1A14",#FFE100",#FF00AA",#00AAFF",#16A085",#B903FC"))+scale_size_manual(name ="combined legend",标签= c("Nederland totaal","Noord-Holland","Utrecht","Noord-Brabant","Zuid-Holland","Gelderland","Flevoland","Overijssel","Limburg","Drenthe","Zeeland","Friesland","Groningen"),值= 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)) 

这就是传说现在的样子

我的问题是:是否有可能在第一个传奇"部分(例如"Nederland totaal"和其他线条)之间留出一些空间?

我希望它看起来更像这样

(为澄清起见,我做了此说明).

是否存在在某些图例项之间添加一些空间的功能?我希望有人可以帮助我:))

更详细:

我正在使用的数据集是2005年至2019年每个荷兰省的平均年房价.我创建了一个ggplot,当前图看起来像

但是这种方法的好处是您不必计算每年的平均值并将其手动添加到Province变量中.

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

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 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

解决方案

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.

这篇关于R(ggplot)函数可在现有图例中添加“空"行,拆分图例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆