簡體   English   中英

使用ggplot和管道將線型擬合到多因素圖解的圖例中

[英]Fit linetype in the legend of a multifactorial plot, using ggplot and pipes

以下代碼使用顏色和線型的組合來繪制許多不同的條件。 它使用一條線繪制“ R”,並使用虛線繪制“ W”。

我想圖例分成兩個部分: 條件 “HC”和“IC”,並型號類型 “R”和“W”。 Condition中 ,我們要表示線條的顏色。 Model type中 ,我們要表示線的類型(例如,黑色的虛線和非虛線)。

由於我使用管道,因此到目前為止,我還沒有找到解決方案。

library(ggplot2)
library(scales)
tribble(    ~y,    ~cb,   ~z,    ~x,
            1     , 0,   "H C R"    ,1,
            2     , 1,   "H C R"    ,1,
            2     , 0,   "I C R"    ,1,
            3     , 1,   "I C R"    ,1,
            1.5   , 0,   "H C W"    ,1,
            2     , 1,   "H C W"    ,1,
            2     , 0,   "I C W"    ,1,
            2     , 1,   "I C W"    ,1,
            3     , 0,   "H C R"    ,2,
            3     , 1,   "H C R"    ,2,
            0.5   , 0,   "I C R"    ,2,
            2     , 1,   "I C R"    ,2,
            2     , 0,   "H C W"    ,2,
            2     , 1,   "H C W"    ,2,
            1     , 0,   "I C W"    ,2,
            1     , 1,   "I C W"    ,2)-> datos


aux<-datos %>%
  group_by(x, z, cb) %>%
  summarise(media = mean(y), 
            desvio = rnorm(1),
            error_est = desvio / sqrt(n()),             
            intervalo_sup = media + (2*error_est),       
            intervalo_inf = media - (2*error_est)) #%>%


library(RColorBrewer)
display.brewer.pal(11,name = "Spectral")  
brewer.pal(n = 11, name = "Spectral")
# Que la línea de condición "H C" apareciera en el color anaranjado que utiliza la paleta "Spectral".
# Que la línea de condición "I C" apareciera en el color verde oscuro que utiliza la paleta "Spectral".
colores<-brewer.pal(n = 11, name = "Spectral")[c(3,3,9,9)]




  ggplot() +

  geom_line(data=aux%>%filter(grepl("W",z)),
            aes(x = x, y = media, color = z,group = z),
            size=0.5,
            linetype = "dashed") +                      
  geom_errorbar(data=aux%>%filter(grepl("W",z)),aes(x = x, color = z,
                              ymax = intervalo_sup,               
                    ymin = intervalo_inf),
                width=0.3) + 

    geom_line(data=aux%>%filter(!grepl("W",z)),
              aes(x = x, y = media, color = z,group = z),
              size=0.5) +                      
    geom_errorbar(data=aux%>%filter(!grepl("W",z)),
                  aes(x = x, color = z,
                                ymax = intervalo_sup,               
                                ymin = intervalo_inf),
                  width=0.3)+
  labs(x = "x", y = "y", color = "Condition") +

  scale_color_manual(values=colores) +
  scale_x_continuous(breaks = seq(1,7, by=1)) +
  theme(legend.position="bottom", legend.text=element_text(size=12)) +
  guides(color=guide_legend(ncol=2)) +
  theme(axis.text=element_text(size=14),
        axis.title=element_text(size=14))+
  facet_wrap(~cb)

在此處輸入圖片說明

解決方案使用stringr

library(dplyr)
library(ggplot2)
library(scales)
library(RColorBrewer)
library(stringr)

#Selection of desired colors
display.brewer.pal(11,name = "Spectral") 
colores<-brewer.pal(n = 11, name = "Spectral")[c(3,9)]

#Stats
datos %>%
  group_by(x, z, cb) %>%
  summarise(media = mean(y), 
            desvio = rnorm(1),                         
            error_est = desvio / sqrt(n()),             
            intervalo_sup = media + (2*error_est),       
            intervalo_inf = media - (2*error_est))%>%
  #Two columns for color and linetype (be careful when using stringr)
  mutate(Model=str_split(z," ")[[1]][3],
         Condition=str_remove(z,Model)
            )%>%
  #Plot
ggplot(aes(x = x, y = media, color = Condition,group = z)) +
  geom_line(aes(linetype=Model, color=Condition),size=0.5)+
  geom_errorbar(aes(x = x, color = Condition,
                    ymax = intervalo_sup,               
                    ymin = intervalo_inf),
                width=0.3)+
  scale_color_manual(values=colores) +
  scale_x_continuous(breaks = seq(1,7, by=1)) +
  theme(legend.position="bottom", legend.text=element_text(size=12)) +
  theme(axis.text=element_text(size=14),
        axis.title=element_text(size=14))+
  facet_wrap(~cb)+
  # Legends' title
  guides(color=guide_legend("Condition"),
         linetype=guide_legend("Model"))  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM