我有两个时间序列的数据,我希望通过其具有不同形状的各个数据点来表示。正确的形状显示在图形中(圆形和正方形),尽管图例对于两条线(以及不需要点表示的逐步线)显示相同的形状。如何在图例中分别为葡萄糖和CDW画一个圆圈和一个正方形,而在底物上没有形状。
我用scale_shape_manual尝试了其他选项,但似乎没有一个起作用。
data = read.table("Aps_F12_data_samples_raw.csv", header=TRUE, sep=",", fileEncoding="UTF-8-BOM")
df = data[,c(2,3,4,5,6,7,8,10)]
names(df) = c("time", "glucose", "substrate_use", "alkyl", "bola", "oleyl", "cdw", "substrate")
p1 <- ggplot(df, aes(x = time)) +
geom_point(aes(y = cdw, colour = "CDW", shape="CDW"), size=2) +
geom_point(aes(y = glucose, colour = "glucose", shape="glucose"), size=2) +
geom_line(data=df[!is.na(df$cdw),], aes(y = cdw, colour = "CDW"), size=1) +
geom_line(aes(y = glucose, colour = "glucose"), size=1) +
geom_step(aes(y = substrate, colour = "substrate"), size=1, linetype="dashed") +
theme_classic() + ylab("Concentration (g/l)") +
xlab("Time (h)") +
scale_colour_manual(name="Time (h)", values = c("CDW" = "grey", "glucose"= "#fb6a4a", "substrate"= "black")) +
scale_shape_manual(name="Time (h)", values = c("CDW"=16, "glucose"=15)) +
scale_y_continuous(breaks=c(0, 25, 50, 75, 100, 125, 150), labels=c("0", "25", "50", "75", "100", "125", "150")) +
theme(legend.position="bottom", legend.title=element_blank())
p2 <- ggplot(df, aes(x=time)) +
geom_line(aes(y = alkyl, colour = "alkyl SS"), size=1) +
geom_line(aes(y = oleyl, colour = "bola SS"), size=1) +
theme_classic() +
xlab("Time (h)") +
ylab("Concentration (g/l)") +
scale_colour_manual(values = c("#addd8e", "#f7fcb9")) +
theme(legend.position="bottom", legend.title=element_blank())
grid.arrange(p1,p2)
time glucose substrate_use alkyl bola oleyl cdw substrate
0.00000 163 NA NA NA NA NA 0
6.00000 165 NA NA NA NA 1.0 0
24.16667 144 1.1559633 NA NA NA 13.2 0
31.16667 134 1.2317881 NA NA NA 14.4 0
49.86667 115 1.1398176 NA NA NA 18.6 0
77.21667 96 0.9688743 NA NA NA 17.4 0
94.28333 83 0.9288276 NA NA NA 18.4 0
103.35000 77 0.9039548 NA NA NA 18.4 0
103.75000 128 NA NA NA NA NA 50
118.43333 122 0.4086266 NA NA NA 17.0 50
128.18333 119 0.3076923 NA NA NA 17.6 50
142.38333 111 0.5633803 NA NA NA 17.8 50
151.13333 107 NA NA NA NA NA 50
166.25000 100 NA NA NA NA 18.6 50
175.13333 95 NA NA NA NA 18.8 50
190.03333 89 NA NA NA NA 19.4 50
答案 0 :(得分:1)
这结合了传说:
ggplot(df, aes(x = time)) +
geom_point(aes(y = cdw, colour = "CDW", shape="CDW"), size=2) +
geom_point(aes(y = glucose, colour = "glucose", shape="glucose"), size=2) +
#you need a mapping in geom_point for substrate:
geom_point(aes(y = substrate, colour = "substrate", shape="substrate")) +
geom_line(data=df[!is.na(df$cdw),], aes(y = cdw, colour = "CDW"), size=1) +
geom_line(aes(y = glucose, colour = "glucose"), size=1) +
geom_step(aes(y = substrate, colour = "substrate"), size=1, linetype="dashed") +
theme_classic() + ylab("Concentration (g/l)") +
xlab("Time (h)") +
scale_colour_manual(name="Time (h)", values = c("CDW" = "grey", "glucose"= "#fb6a4a", "substrate"= "black")) +
#you can remove the points for substrate in the scale:
scale_shape_manual(name="Time (h)", values = c("CDW"=16, "glucose"=15, substrate = NA)) +
scale_y_continuous(breaks=c(0, 25, 50, 75, 100, 125, 150), labels=c("0", "25", "50", "75", "100", "125", "150")) +
theme(legend.position="bottom", legend.title=element_blank())