【问题标题】:geom_area resulting in horizontal lines where area fill should begeom_area 导致区域填充应该是水平线
【发布时间】:2018-09-25 13:01:36
【问题描述】:

我正在尝试使用 geom_area 填充分隔线下方和上方的区域。但是,一旦我使用代码,我会在图形线下方看到这些奇怪的水平线,并且该区域没有被填充。此外,geom_ribbon 也没有显示在图表上。

这是我的代码:

ggplot(my_data, aes(x = Concentration, y = OD600_avg)) +
  geom_area(colour = "black", fill = "blue", alpha = 0.2) +
  geom_ribbon(aes(x = Concentration, ymin = OD600_avg - OD600_sdv, ymax = OD600_avg + OD600_sdv), fill = "firebrick", alpha = 0.4) +
  geom_line(colour = "red", size = 1, aes(x = Concentration, y = OD600_avg, group = 1))

数据:

my_data <- data.frame("Concentration" = c("0", "5", "10", "15", "20", "25", "30", "35", "40", "45", "50", "55"), 
                   "OD600_avg" = c("0.8", "0.17", "0.15", "0.14", "0.137", "0.12", "0.11", "0.09", "0.08", "0.08", "0.08", "0.08"),
                   "OD600_sdv" = c("0.05", "0.004", "0.002", "0.005", "0.008", "0.005", "0.007", "0.02", "0.011", "0.02", "0.004", "0.004"))

结果如下:

Result of ggplot

有人知道为什么会这样吗?

【问题讨论】:

  • 您的数据不是数字,而是字符。先尝试纠正它们。
  • 成功了,谢谢!
  • 我添加了答案。随意接受它。

标签: r ggplot2


【解决方案1】:

您的数据不是numeric,而是characters。以下作品

my_data <- data.frame(Concentration = seq(0,55,5), 
                           OD600_avg = c(0.8, 0.17, 0.15, 0.14, 0.137, 0.12, 0.11, 0.09, 0.08, 0.08, 0.08, 0.08),
                          OD600_sdv = c(0.05, 0.004, 0.002, 0.005, 0.008, 0.005, 0.007, 0.02, 0.011, 0.02, 0.004, 0.004))

ggplot(my_data, aes(x = Concentration, y = OD600_avg)) +
  geom_area(colour = "black", fill = "blue", alpha = 0.2) +
  geom_ribbon(aes(x = Concentration, ymin = OD600_avg - OD600_sdv, ymax = OD600_avg + OD600_sdv), fill = "firebrick", alpha = 0.4) +
  geom_line(colour = "red", size = 1, aes(x = Concentration, y = OD600_avg, group = 1))

【讨论】: