首页 > 解决方案 > 图例中的形状与图表不对应

问题描述

我有两个时间序列的数据,我想用不同形状的单个数据点来表示它们。图形中显示了正确的形状(圆形和方形),尽管图例显示两条线的形状相同(以及不需要用点指示的逐步线)。我怎样才能在图例中分别为葡萄糖和 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

标签: rggplot2legend

解决方案


这结合了传说:

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())

推荐阅读