2017-07-24 1 views
2

Ich frage mich, ob es möglich ist, eine Legende für geom_point und geom_segment hinzuzufügen? Von geom_point bekomme ich die Legende automatisch, aber ich habe keine Ahnung, wie man die Legende für geom_segment hinzufügt. Ich habe versucht scale_linetype_manual, aber anscheinend funktioniert es nicht.Ist es möglich, eine Legende für geom_point und geom_segment hinzuzufügen?

Dies ist ein Fragment meiner Daten:

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

Und das ist ein Code, ich habe bisher:

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

Was ich will, ist ein Diagramm unten, mit festen Legende. enter image description here

+0

Haben Sie Datenrahmen 'ph7' weggelassen? Ich kann deine Handlung nicht reproduzieren. – Lyngbakr

Antwort

1

Alles, was Sie tun müssen, ist Linienart = "bedeutet" in der aes Abschnitt Ihrer geom_segment hinzufügen:

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()) 
Verwandte Themen