两点线之间的填充区域 R
Fill area between two point-lines R
我有一个 tibble 类似于下面的:
Offensive <- tibble(OffenseFormation = c("A","B","C"),
yardas_mean = c(3,4,5),
yardas_min = c(1,4,1),
yardas_max = c(5,4,6))
我用下面的代码绘制线条(如下图所示):
Offensive %>%
pivot_longer(starts_with("yardas_"),names_to = "yardas") %>%
ggplot(aes(x = OffenseFormation, y = value, group = yardas)) +
geom_line(aes(colour = yardas)) +
geom_point(aes(colour = yardas))
我想要的是填充 yardas_min
和 yardas_max
行之间的区域。
我已经使用了以下 ggplot 命令:
geom_area(alpha=0.1)
geom_polygon( aes(y = value, group = yardas), alpha = 0.1)
并且还阅读了一些以前的 post,比如这些:
https://mcfromnz.wordpress.com/2014/06/02/shading-between-two-lines-ggplot/
How to highlight area between two lines? ggplot
但是没有成功,有什么帮助吗?
谢谢,
阿尔贝托
问题是您将离散值作为 x 轴。您可以通过在 geom_ribbon
中添加连续值来制作色带:
Offensive %>%
pivot_longer(starts_with("yardas_"),names_to = "yardas") %>%
ggplot(aes(x = OffenseFormation, y = value, group = yardas)) +
geom_line(aes(colour = yardas))+
geom_ribbon(data = Offensive,
inherit.aes = FALSE,
aes(x = 1:3, ymin = yardas_min, ymax = yardas_max),
fill = "grey70")+
geom_line(aes(colour = yardas))+
geom_point(aes(colour = yardas))
我有一个 tibble 类似于下面的:
Offensive <- tibble(OffenseFormation = c("A","B","C"),
yardas_mean = c(3,4,5),
yardas_min = c(1,4,1),
yardas_max = c(5,4,6))
我用下面的代码绘制线条(如下图所示):
Offensive %>%
pivot_longer(starts_with("yardas_"),names_to = "yardas") %>%
ggplot(aes(x = OffenseFormation, y = value, group = yardas)) +
geom_line(aes(colour = yardas)) +
geom_point(aes(colour = yardas))
我想要的是填充 yardas_min
和 yardas_max
行之间的区域。
我已经使用了以下 ggplot 命令:
geom_area(alpha=0.1)
geom_polygon( aes(y = value, group = yardas), alpha = 0.1)
并且还阅读了一些以前的 post,比如这些:
https://mcfromnz.wordpress.com/2014/06/02/shading-between-two-lines-ggplot/
How to highlight area between two lines? ggplot
但是没有成功,有什么帮助吗?
谢谢,
阿尔贝托
问题是您将离散值作为 x 轴。您可以通过在 geom_ribbon
中添加连续值来制作色带:
Offensive %>%
pivot_longer(starts_with("yardas_"),names_to = "yardas") %>%
ggplot(aes(x = OffenseFormation, y = value, group = yardas)) +
geom_line(aes(colour = yardas))+
geom_ribbon(data = Offensive,
inherit.aes = FALSE,
aes(x = 1:3, ymin = yardas_min, ymax = yardas_max),
fill = "grey70")+
geom_line(aes(colour = yardas))+
geom_point(aes(colour = yardas))