是否可以为geom_point和geom_segment添加图例? [英] Is it possible add legend for geom_point and geom_segment?

查看:255
本文介绍了是否可以为geom_point和geom_segment添加图例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以为geom_pointgeom_segment添加图例?从geom_point我可以自动获取图例,但是我不知道如何为geom_segment添加图例.我已经尝试过scale_linetype_manual,但显然它不起作用.

I am wondering if it is possible to add a legend for geom_point and geom_segment? From geom_point I get the legend automatically, however I have no clue how to add the legend for geom_segment. I have tried scale_linetype_manual but apperently it is not working.

这是我的数据的一部分:

This is a fragment of my data:

 df1 <- structure(list(type = c("WT", "SHC", "WT", "SHC", "WT", "SHC", 
                                   "WT", "SHC", "WT", "SHC", "WT", "SHC", "WT", "SHC", "WT", "SHC", 
                                   "WT", "SHC", "WT", "SHC", "WT", "SHC", "WT", "SHC"), lipids = structure(c(3L, 
                                                                                                             3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
                                                                                                             3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("DIP", "mDIP", "CL [66:0]", 
                                                                                                                                                     "CL [70:2]", "CL [70:4]", "CL [72:4]", "CL [74:2]", "PC [32:1]", 
                                                                                                                                                     "PC [32:2]", "PC [34:1]", "PC [34:2]", "PC [34:3]", "PC [34:4]", 
                                                                                                                                                     "PC [36:2]", "PC [36:3]", "PC [36:4]", "PE [32:1]", "PE [34:1]", 
                                                                                                                                                     "PE [34:2]", "PE [36:1]", "PE [36:2]", "PE [36:3]", "PG [32:1]", 
                                                                                                                                                     "PG [34:1]", "PG [34:2]", "PG [36:2]", "PG [36:3]"), class = "factor"), 
                          rep = c("pH 5.5 1", "pH 5.5 1", "pH 5.5 2", "pH 5.5 2", "pH 5.5 3", 
                                  "pH 5.5 3", "pH 6 1", "pH 6 1", "pH 6 2", "pH 6 2", "pH 6 3", 
                                  "pH 6 3", "pH 7 1", "pH 7 1", "pH 7 2", "pH 7 2", "pH 7 3", 
                                  "pH 7 3", "pH 7.5 1", "pH 7.5 1", "pH 7.5 2", "pH 7.5 2", 
                                  "pH 7.5 3", "pH 7.5 3"), num = c(0.004633127, 0.04514786, 
                                                                   0.009330446, 0.032803203, 0.016920389, 0.045960156, 0.017778907, 
                                                                   0.028992327, 0.023182626, 0.03861396, 0.023031255, 0.04358965, 
                                                                   0.007902522, 0.01725995, 0.005021786, 0.018188753, 0.006793196, 
                                                                   0.032270599, 0.007153489, 0.06383629, 0.017068877, 0.026518865, 
                                                                   0.014697648, 0.02748779), groups = c("Ph", "Ph", "Ph", "Ph", 
                                                                                                        "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", 
                                                                                                        "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph"
                                                                   )), row.names = c(NA, -24L), class = c("tbl_df", "tbl", "data.frame"
                                                                   ), .Names = c("type", "lipids", "rep", "num", "groups")) 

这是代码,到目前为止,我已经知道了:

And this is a code, I have so far:

ph7 <- df1
ph7$rep<-sub("X","",ph7$rep)
ph7$rep<-sub("\\d$","",ph7$rep)
ph7$rep<-sub("\\.$","",ph7$rep)
ph7 <- ph7 %>% group_by(type, lipids, groups, rep)%>% summarise_all( funs(mean(.)))

pos1 <- data.frame(x1=c(0.8),
                         x2=c(1.0),
                         y1=filter(ph7, grepl('pH 7 ', rep))$num[1],
                         y2=filter(ph7, grepl('pH 7 ', rep))$num[1],
                         label=filter(ph7, grepl('pH 7 ', rep))$type[1])
pos6 <- data.frame(x1=c(1.0),
                         x2=c(1.2),
                         y1=filter(ph7, grepl('pH 7 ', rep))$num[2],
                         y2=filter(ph7, grepl('pH 7 ', rep))$num[2],
                         label=filter(ph7, grepl('pH 7 ', rep))$type[2])

ggplot(df1, aes(x = factor(lipids), y= num, col = type)) + 
  geom_point(size = 0.4, position = position_dodge(width = 0.3))+
  geom_segment(aes(x=x1,y=y1,xend=x2,yend=y2,colour=label),lwd=1,data=pos1, col = "grey50") +
  geom_segment(aes(x=x1,y=y1,xend=x2,yend=y2,colour=label),lwd=1,data=pos6, col = "grey50") + 
  scale_color_manual(name = "",values = c( "red","grey50"),labels=c( "SHC","WT"))+ 
  scale_linetype_manual("mean",values=c("mean"=2))+
  theme(legend.title=element_blank())

我想要的是下面的图,带有固定的图例.

What I want is a plot below, with fixed legend.

推荐答案

您需要做的就是在geom_segment的aes部分中添加linetype ="mean":

All you need to do is add linetype = "mean" in the aes section of your geom_segment:

  ggplot(df1, aes(x = factor(lipids), y= num, col = type)) + 
  geom_point(size = 0.4, position = position_dodge(width = 0.3))+
  geom_segment(aes(x=x1,y=y1,xend=x2,yend=y2,colour=label, linetype = "mean"),lwd=1,data=pos1, col = "grey50") +
  geom_segment(aes(x=x1,y=y1,xend=x2,yend=y2,colour=label, linetype = "mean"),lwd=1,data=pos6, col = "grey50",) + 
  scale_color_manual(name = "",values = c( "red","grey50"),labels=c( "SHC","WT"))+ 
  scale_linetype_manual("mean",values=c("mean"=1))+
  theme(legend.title=element_blank())

这篇关于是否可以为geom_point和geom_segment添加图例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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