gpt4 book ai didi

r - 如何在ggplot中将手动色标传递给geom_smooth?

转载 作者:行者123 更新时间:2023-12-04 11:38:44 25 4
gpt4 key购买 nike

我的数据看起来像这样:

# 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”作为虚线绘制条纹,将其他线条绘制为实线,这给了我:

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

最佳答案

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

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

关于r - 如何在ggplot中将手动色标传递给geom_smooth?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47878979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com