如何在具有相同 "y" 的行之间添加阴影?
How do I shade between lines with identical "y"s?
我花了几个小时在 geom_polygon 和 geom_ribbon 之间尝试,但我就是无法让它工作。我正在寻找遮蔽两条线之间的区域(我研究了很多)。看,图表的两个轴都是数字并且两条线共享相同的 y 轴,因此,我无法计算 ymin 或最大。有什么想法吗?我将不胜感激。这是我的代码:
ggplot(npsmelt,aes(Score,Unresolved)) +
geom_line(aes(linetype=Metric, color=Metric)) +
theme(legend.position="top", legend.title = element_blank()) +
geom_point(aes(color=Metric)) +
theme(plot.caption=element_text(size=8, margin=margin(t=10))) +
scale_x_continuous(breaks=seq(54,70,1)) +
geom_ribbon(data=subset(npsmelt,
Score[Metric=="Old.NPS"]<Score[Metric=="New.NPS"]),
aes(ymin =0, ymax =..y..), alpha=0.10)
结果图:
我的数据:
Unresolved Metric Score
5 New.NPS 66.48
6 New.NPS 65.32
7 New.NPS 64.16
8 New.NPS 63
9 New.NPS 61.84
10 New.NPS 60.68
5 Old.NPS 68.5
6 Old.NPS 62.28
7 Old.NPS 61.74
8 Old.NPS 61.41
9 Old.NPS 61.67
10 Old.NPS 60.42
dput
:
structure(list(Unresolved = c(5L, 6L, 7L, 8L, 9L, 10L, 5L, 6L,
7L, 8L, 9L, 10L), Metric = c("New.NPS", "New.NPS", "New.NPS",
"New.NPS", "New.NPS", "New.NPS", "Old.NPS", "Old.NPS", "Old.NPS",
"Old.NPS", "Old.NPS", "Old.NPS"), Score = c(66.48, 65.32, 64.16,
63, 61.84, 60.68, 68.5, 62.28, 61.74, 61.41, 61.67, 60.42)), .Names = c("Unresolved",
"Metric", "Score"), row.names = c(NA, -12L), class = "data.frame")
在我看来,这似乎是一种罕见的情况,最好让数据集采用 'wide' 格式。
我将为 geom_line
和 geom_point
使用长格式,然后为 geom_ribbon
切换为宽格式。
library(ggplot2)
library(tidyr)
ggplot(df, aes(Unresolved, Score, group = Metric, color = Metric)) +
geom_line() +
geom_point() +
geom_ribbon(data = spread(df, Metric, Score),
aes(x = Unresolved, ymin = New.NPS, ymax = Old.NPS),
alpha = .2,
inherit.aes = FALSE)
注意 inherit.aes = FALSE
,它是必需的,否则 geom_ribbon
将尝试使用映射到 y
.
的 Score
列
数据:
df <- structure(list(Unresolved = c(5L, 6L, 7L, 8L, 9L, 10L, 5L, 6L,
7L, 8L, 9L, 10L), Metric = c("New.NPS", "New.NPS", "New.NPS",
"New.NPS", "New.NPS", "New.NPS", "Old.NPS", "Old.NPS", "Old.NPS",
"Old.NPS", "Old.NPS", "Old.NPS"), Score = c(66.48, 65.32, 64.16,
63, 61.84, 60.68, 68.5, 62.28, 61.74, 61.41, 61.67, 60.42)), .Names = c("Unresolved",
"Metric", "Score"), row.names = c(NA, -12L), class = "data.frame")
我花了几个小时在 geom_polygon 和 geom_ribbon 之间尝试,但我就是无法让它工作。我正在寻找遮蔽两条线之间的区域(我研究了很多)。看,图表的两个轴都是数字并且两条线共享相同的 y 轴,因此,我无法计算 ymin 或最大。有什么想法吗?我将不胜感激。这是我的代码:
ggplot(npsmelt,aes(Score,Unresolved)) +
geom_line(aes(linetype=Metric, color=Metric)) +
theme(legend.position="top", legend.title = element_blank()) +
geom_point(aes(color=Metric)) +
theme(plot.caption=element_text(size=8, margin=margin(t=10))) +
scale_x_continuous(breaks=seq(54,70,1)) +
geom_ribbon(data=subset(npsmelt,
Score[Metric=="Old.NPS"]<Score[Metric=="New.NPS"]),
aes(ymin =0, ymax =..y..), alpha=0.10)
结果图:
我的数据:
Unresolved Metric Score
5 New.NPS 66.48
6 New.NPS 65.32
7 New.NPS 64.16
8 New.NPS 63
9 New.NPS 61.84
10 New.NPS 60.68
5 Old.NPS 68.5
6 Old.NPS 62.28
7 Old.NPS 61.74
8 Old.NPS 61.41
9 Old.NPS 61.67
10 Old.NPS 60.42
dput
:
structure(list(Unresolved = c(5L, 6L, 7L, 8L, 9L, 10L, 5L, 6L,
7L, 8L, 9L, 10L), Metric = c("New.NPS", "New.NPS", "New.NPS",
"New.NPS", "New.NPS", "New.NPS", "Old.NPS", "Old.NPS", "Old.NPS",
"Old.NPS", "Old.NPS", "Old.NPS"), Score = c(66.48, 65.32, 64.16,
63, 61.84, 60.68, 68.5, 62.28, 61.74, 61.41, 61.67, 60.42)), .Names = c("Unresolved",
"Metric", "Score"), row.names = c(NA, -12L), class = "data.frame")
在我看来,这似乎是一种罕见的情况,最好让数据集采用 'wide' 格式。
我将为 geom_line
和 geom_point
使用长格式,然后为 geom_ribbon
切换为宽格式。
library(ggplot2)
library(tidyr)
ggplot(df, aes(Unresolved, Score, group = Metric, color = Metric)) +
geom_line() +
geom_point() +
geom_ribbon(data = spread(df, Metric, Score),
aes(x = Unresolved, ymin = New.NPS, ymax = Old.NPS),
alpha = .2,
inherit.aes = FALSE)
注意 inherit.aes = FALSE
,它是必需的,否则 geom_ribbon
将尝试使用映射到 y
.
Score
列
数据:
df <- structure(list(Unresolved = c(5L, 6L, 7L, 8L, 9L, 10L, 5L, 6L,
7L, 8L, 9L, 10L), Metric = c("New.NPS", "New.NPS", "New.NPS",
"New.NPS", "New.NPS", "New.NPS", "Old.NPS", "Old.NPS", "Old.NPS",
"Old.NPS", "Old.NPS", "Old.NPS"), Score = c(66.48, 65.32, 64.16,
63, 61.84, 60.68, 68.5, 62.28, 61.74, 61.41, 61.67, 60.42)), .Names = c("Unresolved",
"Metric", "Score"), row.names = c(NA, -12L), class = "data.frame")