R改变已收集的GGPLOT上的图例 [英] R Changing Legend on GGPLOT that has been gathered

查看:187
本文介绍了R改变已收集的GGPLOT上的图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个数据集:

  birdspp smallsaplings mediumsaplings largesaplings 
95 5.044642857 2.384615385 1.30952381
97 3.482269504 1.873684211 1.390625
63 6.285714286 2 2.4
57 5.216216216 1.666666667 1.125

我创建了一个图表:

< img src =https://i.stack.imgur.com/g0E47.pngalt =Graph>



使用代码:

 图书馆(ggplot2)
图书馆(dplyr)
图书馆(tidyr)

树苗%> ;%
gather(sapling_type,mean_number,-birdspp)

sapling%>%
gather(sapling_type,mean_number,-birdspp)%>%
ggplot (aes(mean_number,birdspp))+
geom_point(aes(color = sapling_type))+
geom_smooth(aes(group = sapling_type,color = sapling_type,fill = sapling_type),method =lm,show.legend = FALSE)+
labs(x =平均树苗密度,y =鸟类种数)+
theme_classic()

正如您所看到的传说不是很整齐。



我尝试了将show.legend = FALSE并加入:

  + scale_colour_discrete(name =sapling size,
breaks = c(smallsaplings,mediumsaplings,largesaplings),
labels = c(smallsaplings,mediumsaplings ,largesaplings))+
scale_shape_discrete(name =Sapling Size,
breaks = c(smallsaplings,mediumsaplings,largesaplings),
labels = c(小树苗,中型树苗,大型树苗))

但没有任何运气。任何建议,将不胜感激。



谢谢。

解决方案

应工作

 树苗%>%
聚会(sapling_type,mean_number,-birdspp)%>%
ggplot(aes(mean_number,birdspp))+
geom_point(aes(color = sapling_type))+
geom_smooth(aes(group = sapling_type,color = sapling_type,fill = sapling_type),method = lm,show.legend = FALSE)+
labs(x =平均树苗密度,y =鸟种数量)+
theme_classic()+
scale_colour_discrete(name = ,
break = c(smallsaplings,mediumsaplings,largesaplings),
labels = c(小树苗,中型树苗,大树苗))


I am struggling to add a modified legend to my ggplot.

I have a data set:

birdspp smallsaplings   mediumsaplings  largesaplings
95      5.044642857     2.384615385     1.30952381
97      3.482269504     1.873684211     1.390625
63      6.285714286     2               2.4
57      5.216216216     1.666666667     1.125

Which I've created a graph with:

Using the code:

 library(ggplot2)
 library(dplyr)
 library(tidyr)

 sapling %>% 
   gather(sapling_type, mean_number, -birdspp)

 sapling %>% 
     gather(sapling_type, mean_number, -birdspp) %>% 
     ggplot(aes(mean_number, birdspp)) + 
       geom_point(aes(color = sapling_type)) + 
       geom_smooth(aes(group =sapling_type, color = sapling_type, fill = sapling_type), method = "lm", show.legend=FALSE) + 
       labs(x="Mean Sapling Density", y="Number of  Bird Species") + 
       theme_classic() 

As you can see the legend is not very neat.

I've tried turning show.legend = FALSE on and adding this:

+ scale_colour_discrete(name  ="Sapling Size",
                            breaks=c("smallsaplings", "mediumsaplings", "largesaplings"),
                            labels=c("smallsaplings", "mediumsaplings", "largesaplings")) +
      scale_shape_discrete(name  ="Sapling Size",
                            breaks=c("smallsaplings", "mediumsaplings", "largesaplings"),
                            labels=c("smallsaplings", "mediumsaplings", "largesaplings"))

But haven't had any luck. Any suggestions would be appreciated.

Thank you.

解决方案

This should work

 sapling %>% 
     gather(sapling_type, mean_number, -birdspp) %>% 
     ggplot(aes(mean_number, birdspp)) + 
       geom_point(aes(color = sapling_type)) + 
       geom_smooth(aes(group =sapling_type, color = sapling_type, fill = sapling_type), method = "lm", show.legend=FALSE) + 
       labs(x="Mean Sapling Density", y="Number of  Bird Species") + 
       theme_classic() +
       scale_colour_discrete(name="",
           breaks=c("smallsaplings", "mediumsaplings", "largesaplings"),
           labels=c("Small Saplings", "Medium Saplings", "Large Saplings"))

这篇关于R改变已收集的GGPLOT上的图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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