如何将标准偏差显示为形状?

时间:2016-11-02 13:34:54

标签: r ggplot2

我有以下数据框select

df

我创建了一个包含两条直线曲线的图表,第一条对应structure(list(queue = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("XXX1", "XXX2", "XXX3", "XXX4", "XXX5", "XXX6", "XXX7", "XXX8", "XXX9", "XXX10"), class = "factor"), hour = c(8, 9, 10, 11, 12, 13), num_at = c(5, 10.6923076923077, 8.35294117647059, 7.74285714285714, 10.0769230769231, 9.34615384615385 ), num_wt = c(1.8, 8.28571428571429, 11.3478260869565, 11.2121212121212, 17.5185185185185, 6.68421052631579),num_wt_se = c(0.8, 0.25, 1.3, 1.4, 1.5, 2.6),num_at_se = c(1.2, 1.2, 1.3, 1.2, 1.1, 2.3)), .Names = c("queue", "hour", "num_at", "num_wt","num_at_se","num_wt_se"), row.names = c(NA, 6L), class = "data.frame") ,而第二条对应num_wt值。

num_at

现在,我想显示相应存储在library("dplyr") library("tidyr") library("ggplot2") p <- df[df$queue == "XXX1",] %>% gather(key, num, num_wt, num_at) %>% ggplot(aes(x=hour, y=num, colour=key)) + geom_smooth(span = 0.3) + scale_color_manual(labels = c("Line 1", "Line 2"), values = c("#F8AA5A", "#BD63BD")) + labs(title = "Title", x = "Hour", y = "Value", color = "") p num_wt中的num_atnum_wt_se的标准偏差。我想将标准偏差描述为形状,而不是线条。我该怎么做? 我用Google搜索了一个可能的解决方案并找到了这个解决方案,但在我的案例中它没有显示任何内容:

num_at_se

1 个答案:

答案 0 :(得分:0)

假设您的数据集被称为d并且您不是gather(),那该怎么样

ggplot(d, aes(x=hour, y=num_at)) +
  geom_ribbon(aes(ymax = num_at + num_at_se, 
                  ymin = num_at - num_at_se), 
              fill = grey(0.5), alpha = 0.5) +
  geom_line()

如果您想维持gather()步骤,请尝试此操作

dd <- d %>% 
  gather(variable, value, -(1:2)) %>%
  separate(variable, c("type", "measurement"), "t") %>%
  spread(measurement, value)
colnames(dd)[4:5] <- c("value", "se")

ggplot(dd, aes(x=hour, y=value)) +
  geom_ribbon(aes(ymax = value + se, 
                  ymin = value - se, 
                  fill = type), 
              alpha = 0.5) +
  geom_line(aes(colour = type))