簡體   English   中英

如何將手動色標傳遞給ggplot中的geom_smooth?

[英]How to pass manual colour scale to geom_smooth in ggplot?

我的數據看起來像這樣:

# A tibble: 6 x 4
    CFU         strain   diltn order
  <dbl>         <fctr>   <dbl> <dbl>
1   0.0 M12-611025 (0) 5.89279     1
2   1.0 M12-611025 (0) 5.19382     1
3   0.0 M12-611025 (0) 4.49485     1
4   0.5 M12-611025 (0) 3.79588     1
5   1.0 M12-611025 (0) 3.09691     1
6   7.0 M12-611025 (0) 2.39794     1

我有16種不同的“應變”,只想在我的圖形上繪制一個子集,並使用geom_smooth繪制一條線。 我已達到以下目的:

lines1 <- c("M12-611025 (0)" = "solid",
            "M12-611025 (0) HI" = "dashed",
            "M12-611025 (300)" = "solid",
            "M12-611025 (300) HI" = "dashed",
            "M12-611025 (700)" = "solid",
            "M12-611025 (700) HI" = "dashed",
            "M12-611025 (1100)" = "solid",
            "M12-611025 (1100) HI" = "dashed")

ggplot(data = (subset(data,
  strain %in% c("M12-611025 (0)", "M12-611025 (0) HI",
                "M12-611025 (300)", "M12-611025 (300) HI",
                "M12-611025 (700)", "M12-611025 (700) HI",
                "M12-611025 (1100)", "M12-611025 (1100) HI"))),
  (aes(x = diltn, y = CFU, colour = factor(strain),
       fill = factor(strain), linetype = factor(strain)))) +
  geom_smooth(se = F, span = 1) +
  geom_point(shape = 21, colour = "black", size = 2, stroke = 1) +
  scale_y_continuous(limits = c(-1, 120))+
  scale_x_continuous(breaks = c(0, 1, 2, 3, 4),
                     labels = c(0, 10, 100, 1000, 10000),
                     limits = c(1, 4))+
  annotation_logticks(base = 10, sides = "b", scaled = TRUE) +
  theme(axis.line = element_line(colour = "black",
                                 size = 1,    
                                 linetype = "solid")) +
  ggtitle("RPM Test M12 - 611025") + 
  xlab(expression(paste("Dilution "))) + 
  ylab("CFU") +
  scale_linetype_manual(values = lines1)

這里的關鍵是我想繪制帶有“HI”的strians為虛線,其他為實線,這給了我:

在此輸入圖像描述

然后我想要定義我自己的配色方案,因此相同的應變具有相同的色點和線,但線條為“HI”版本的虛線。 我試過這個:

cols1 <- c("M12-611025 (0)" = "blue",
           "M12-611025 (0) HI" = "blue",
           "M12-611025 (300)" = "#ff9700",
           "M12-611025 (300) HI" = "#ff9700",
           "M12-611025 (700)" = "#33d100",
           "M12-611025 (700) HI" = "#33d100", 
           "M12-611025 (1100)" = "#fe0000",
           "M12-611025 (1100) HI" = "#fe0000")

M12_611025 + scale_fill_manual(values = cols1)

這改變了點而不是線條,我無法弄清楚如何讓線條與點相同的顏色?

在此輸入圖像描述

線條使用color美學,形狀點21使用fill美學(對於內部顏色,輪廓也是color美學)。 因此,在您的情況下,您需要更改fillcolor標度:

M12_611025 + 
  scale_fill_manual(values = cols1) +
  scale_color_manual(values = cols1)

暫無
暫無

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

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