R 线条图中显示变化的颜色梯度
我试图创建一个历史数据的时间序列图,该图显示了一个由阴影包围的单年数据,该阴影表示多年数据中存在的可变性(最多2个标准差)。我真的希望这个阴影是一个颜色梯度,像颜色越深越接近平均值变得越亮 到目前为止,我已经使用下面的代码创建了一个具有置信区间的时间序列图R 线条图中显示变化的颜色梯度,r,ggplot2,R,Ggplot2,我试图创建一个历史数据的时间序列图,该图显示了一个由阴影包围的单年数据,该阴影表示多年数据中存在的可变性(最多2个标准差)。我真的希望这个阴影是一个颜色梯度,像颜色越深越接近平均值变得越亮 到目前为止,我已经使用下面的代码创建了一个具有置信区间的时间序列图 > library(ggplot2) ## Here is the what the data looks like. Not lower_ci and upper_ci are just ## Mean_sales - (2 * S
> library(ggplot2)
## Here is the what the data looks like. Not lower_ci and upper_ci are just
## Mean_sales - (2 * Std_dev_sales) and Mean_sales + (2 * Std_dev_sales) respectively.
## upper_dif and lower_dif is simply the difference between the mean and
## the confidence intervals
> head(sales_summary, 3)
# A tibble: 3 x 7
Date Mean_sales Std_dev_sales lower_ci upper_ci lower_dif upper_dif
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 06-26 4761794. 970015. 2821763. 6701824. -1940030. 1940030.
2 06-27 4528859. 327306. 3874247. 5183471. -654612. 654612.
3 06-28 5139476. 1105831. 2927814. 7351138. -2211662. 2211662.
> theme_set(theme_classic())
> ggplot(data = sales_summary,
> aes(x = as.Date(Date, format = "%m-%d"), y = Mean_sales)) +
> geom_ribbon(aes(ymin=lower_ci,
> ymax=upper_ci),
> linetype=0, alpha=0.2,
> fill = "blue")
这可以为我提供所需的效果,如图中所示,但复制粘贴非常繁重,尤其是当我无法使用ggplot获得任何循环时(仅返回最后一个迭代图)
提前感谢。您可以将几何图形的
列表添加到绘图中,以一次添加多个几何图形。这意味着您可以使用purrr::map
创建具有不同比例的几何图形:
library(purrr)
ggplot(data = sales_summary,
aes(x = as.Date(Date, format = "%m-%d"), y = Mean_sales)) +
map(seq(0.1, 1, by = 0.1), function(prop) {
geom_ribbon(aes(ymin=lower_ci - (prop * lower_dif),
ymax=upper_ci - (prop * upper_dif)),
linetype=0, alpha=0.1,
fill = "steelblue")
})
library(purrr)
ggplot(data = sales_summary,
aes(x = as.Date(Date, format = "%m-%d"), y = Mean_sales)) +
map(seq(0.1, 1, by = 0.1), function(prop) {
geom_ribbon(aes(ymin=lower_ci - (prop * lower_dif),
ymax=upper_ci - (prop * upper_dif)),
linetype=0, alpha=0.1,
fill = "steelblue")
})