如何将手动色标传递给ggplot中的geom_smooth?

时间:2017-12-19 01:55:07

标签: r ggplot2

我的数据看起来像这样:

# 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为虚线,其他为实线,这给了我:

enter image description here

然后我想定义我自己的配色方案,因此相同的应变具有相同的色点和线,但是“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)

这改变了点而不是线条,我无法弄清楚如何让线条与点颜色相同?

enter image description here

1 个答案:

答案 0 :(得分:1)

线条使用color美学和形状点21使用fill美学(对于内部颜色,轮廓也是color美学)。因此,在您的情况下,您需要更改fillcolor比例:

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