gpt4 book ai didi

r - 将图例添加到R ggplot2中的geom_rect

转载 作者:行者123 更新时间:2023-12-02 00:00:31 25 4
gpt4 key购买 nike

考虑 question 中的数据集。可以使用 ggplot2 中的 geom_rect 绘制阴影,如下所示。

data <- structure(list(Time = c(20L, 40L, 60L, 80L, 100L, 120L, 20L, 
40L, 60L, 80L, 100L), Average = c(5.8, 6.1, 6.4, 6.7, 7, 7.7,
8.47, 9.317, 10.2487, 11.27357, 12.40093), Test = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L), .Label = c("Control",
"Exp"), class = "factor"), n = c(9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L), se = c(0.12, 0.145, 0.188, 0.99, 0.44, 0.32,
0.5, 0.88, 0.9, 0.33, 0.456)), .Names = c("Time", "Average",
"Test", "n", "se"), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11")

ggplot(data, aes(x=Time, y=Average, colour=Test)) +
geom_rect(aes(xmin=20,xmax=30,ymin=-Inf,ymax=Inf),fill="pink",colour=NA,alpha=0.05) +
geom_errorbar(aes(ymin=Average-se, ymax=Average+se), width=0.2) +
geom_line() +
geom_point()

enter image description here

如何为阴影添加图例?

最佳答案

我们可以将填充插入到 aes 中,并为其指定一个比例:

library(ggplot2)

ggplot(data, aes(x=Time, y=Average, colour=Test)) +
geom_rect(aes(xmin=20,xmax=30,ymin=-Inf,ymax=Inf,fill="What"),colour=NA,alpha=0.05) +
geom_errorbar(aes(ymin=Average-se, ymax=Average+se), width=0.2) +
geom_line() +
geom_point() +
scale_fill_manual('Highlight this',
values = 'pink',
guide = guide_legend(override.aes = list(alpha = 1)))

关于r - 将图例添加到R ggplot2中的geom_rect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44652257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com