我想知道是否可以为geom_point
和geom_segment
添加图例?从geom_point我自动获得图例,但我不知道如何为geom_segment
添加图例。我已经尝试scale_linetype_manual
,但显然它不起作用。
这是我的数据片段:
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"))
这是一个代码,我到目前为止:
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())
答案 0 :(得分:1)
您需要做的就是在geom_segment的aes部分添加linetype =“mean”:
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())