向图例添加一个额外的项目 [英] Adding an extra item to the legend

查看:122
本文介绍了向图例添加一个额外的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据:

pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 0.078,0.01,9.00E-13,group1,1
trait2,0.076,0.01,1.70E-11,group1,1
trait3,-0.032,0.01,0.004,group1,0
trait4,0.026,0.01,0.024,group1,0
trait5,0.023,0.01,0.037,group1,0
trait1,0.042,0.01,4.50E-04,group2,1
trait2, 0.04,0.01,0.002,group2,1
trait3,0.03,0.01,0.025,group2,0
trait4,0.025,0.01,0.078,group2,0
trait5,0.015,0.01,0.294 ,group2,0
trait1,0.02,0.01,0.078,group3,0
trait2,0.03,0.01,0.078,group3,0
trait3,0.043,0.01,1.90E-04,group3 ,0
trait4,0.043,0.01,2.40E-04,group3,1
trait5,0.029,0.01,0.013,group3,0

$ b

使用以下代码绘制一个图:

  library(ggplot2 )
ggplot(GEE,aes(y = beta,x = reorder(trait,beta),group = analysis))+
geom_point(data = GEE [GEE $ signif == 1,],
color =red,
shape =*,
size = 12 ,
show.legend = F)+
geom_point(aes(color = analysis))+
geom_errorbar(aes(ymin = beta-2 * se,ymax = beta + 2 * se, color = analysis),width = .2,
position = position_dodge(.2))+
geom_hline(yintercept = 0)+
theme_light()+
theme(axis。 title.y = element_blank(),
legend.title = element_blank())+
coord_flip()

这给了我下面的图:





我想为图例添加一个额外元素,即红色星号,并且我希望它说重要。我该怎么做呢?



PS。如果你喜欢这段代码,我还有一个问题,指定


I have the following data:

trait,beta,se,p,analysis,signif
trait1,0.078,0.01,9.00E-13,group1,1
trait2,0.076,0.01,1.70E-11,group1,1
trait3,-0.032,0.01,0.004,group1,0
trait4,0.026,0.01,0.024,group1,0
trait5,0.023,0.01,0.037,group1,0
trait1,0.042,0.01,4.50E-04,group2,1
trait2,0.04,0.01,0.002,group2,1
trait3,0.03,0.01,0.025,group2,0
trait4,0.025,0.01,0.078,group2,0
trait5,0.015,0.01,0.294,group2,0
trait1,0.02,0.01,0.078,group3,0
trait2,0.03,0.01,0.078,group3,0
trait3,0.043,0.01,1.90E-04,group3,0
trait4,0.043,0.01,2.40E-04,group3,1
trait5,0.029,0.01,0.013,group3,0

And make a plot with the following code:

library(ggplot2)
ggplot(GEE, aes(y=beta, x=reorder(trait, beta), group=analysis)) + 
  geom_point(data = GEE[GEE$signif == 1, ],
             color="red",
             shape = "*", 
             size=12, 
             show.legend = F) +
  geom_point(aes(color=analysis)) +
  geom_errorbar(aes(ymin=beta-2*se, ymax=beta+2*se,color=analysis), width=.2,
                position=position_dodge(.2)) +
  geom_hline(yintercept = 0) +
  theme_light() +
  theme(axis.title.y=element_blank(),
        legend.title=element_blank()) +
  coord_flip() 

Which gives me the following plot:

I would like to add an extra element to the legend, namely the red asterisk, and I want it to say "significant". How do I go about doing that?

PS. If you like this piece of code, I have another problem with it, specified here :)

解决方案

Add dummy aes() to geom_point - for example fill that is named significant aes(fill = "Significant").

# Using OPs data
library(ggplot2)
ggplot(GEE, aes(y=beta, x=reorder(trait, beta), group=analysis)) + 
  geom_point(data = GEE[GEE$signif == 1, ],
             color="red",
             shape = "*", 
             size=12,
             aes(fill = "Significant")) +
  geom_point(aes(color=analysis)) +
  geom_errorbar(aes(ymin=beta-2*se, ymax=beta+2*se,color=analysis), width=.2,
                position=position_dodge(.2)) +
  geom_hline(yintercept = 0) +
  theme_light() +
  theme(axis.title.y=element_blank(),
        legend.title=element_blank()) +
  coord_flip() +
  guides(colour = guide_legend(order = 1), 
         fill = guide_legend(override.aes = list(size = 5))) +
  theme(legend.margin = margin(-0.5,0,0,0, unit="cm"))

PS: I also removed show.legend = F from asterik geom_point

这篇关于向图例添加一个额外的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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