首页 > 解决方案 > 如何将线和点组合到同一个图例?

问题描述

这是我的数据框

Days,Observed,Simulated
0,0,653.8209779
1,982,1300.359539
2,2002,2245.28519
3,3086,3465.029007
4,4290,4891.001133
5,6030,6431.473538
6,7658,7994.170186
7,9063,9501.969562
8,10405,10899.95599
9,11625,12155.67626
10,12759,13255.52404
11,13949,14199.72821
12,14961,14997.49918
13,16151,15663.0628
14,16931,16212.76706
15,17554,16663.16302
16,17874,17029.85323
17,18114,17326.89033
18,18231,17566.54139
19,18231,17759.27762
20,18231,17913.89111

我用的鳕鱼如下:

R <- ggplot(Data, aes(x = Days)) +
  geom_line(aes(y = Simulated, color="Simulated")) +
  geom_point(aes(y = Observed, color="Observed"))
a <- ggtitle("C14=2kg of Placenta & 0.8kg of seed") 
n <- scale_color_calc(name = "Legend")
c <- labs(x = 'Time(Days)', y = "Cumulative Biogas Yield(ml)")
h <- theme(plot.title = element_text(hjust = 0.1))
o <- theme(
  plot.title = element_text(colour = "black"),
  axis.title.x = element_text(colour = "black", size = 10),
  axis.title.y = element_text(colour = "black", size = 10),
  legend.title = element_text(colour = "black", size = 12.5),
  legend.text = element_text(colour = "black", size = 10),
  axis.text.x = element_text(colour = "black", size = 10),
  axis.text.y = element_text(colour = "black", size = 10))
MyPlot <- R+a+n+c+h+o
MyPlot

这就是我得到的。图例显示线和点的组合

图例显示线和点的组合

这就是我要的。只有点的传说中的点

只有点的传说中的点

标签: ggplot2

解决方案


这是你想要的?

这个问题涉及对不同的几何图形使用相同的美学 - 颜色。

您实际上并没有绘制红点,所以我不确定您为什么要在图例中使用红点?

library(ggplot2)


ggplot(df, aes(x = Days)) +
  geom_line(aes(y = Simulated, color="Simulated")) +
  geom_point(aes(y = Observed, fill = "Observed")) +
  ggtitle("C14=2kg of Placenta & 0.8kg of seed")+
  labs(x = 'Time(Days)', 
          y = "Cumulative Biogas Yield(ml)",
          colour = NULL,
          fill = "Legend title")+
  theme(plot.title = element_text(hjust = 0.1))+
  theme( plot.title = element_text(colour = "black"),
         axis.title.x = element_text(colour = "black", size = 10),
         axis.title.y = element_text(colour = "black", size = 10),
         legend.title = element_text(colour = "black", size = 12.5),
         legend.text = element_text(colour = "black", size = 10),
         axis.text.x = element_text(colour = "black", size = 10),
         axis.text.y = element_text(colour = "black", size = 10))

数据

structure(list(Days = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 
12, 13, 14, 15, 16, 17, 18, 19, 20), Observed = c(0, 982, 2002, 
3086, 4290, 6030, 7658, 9063, 10405, 11625, 12759, 13949, 14961, 
16151, 16931, 17554, 17874, 18114, 18231, 18231, 18231), Simulated = c(653.8209779, 
1300.359539, 2245.28519, 3465.029007, 4891.001133, 6431.473538, 
7994.170186, 9501.969562, 10899.95599, 12155.67626, 13255.52404, 
14199.72821, 14997.49918, 15663.0628, 16212.76706, 16663.16302, 
17029.85323, 17326.89033, 17566.54139, 17759.27762, 17913.89111
)), class = "data.frame", row.names = c(NA, -21L))

reprex 包于 2020-05-22 创建(v0.3.0)


推荐阅读