R ggplot2-geom_路径按组着色时删除路径 更新:找到了答案,但我不能说我理解:
原版: 我有两个完全相同的绘图,只有一个例外:颜色(下图)。如何使路径仍然连接?我相信这是因为geom_path()基本上是在构建3个不同的图并将它们重叠。如何为同一路径的不同轨迹着色 作为参考,这是我基本上使用的代码:R ggplot2-geom_路径按组着色时删除路径 更新:找到了答案,但我不能说我理解:,r,ggplot2,R,Ggplot2,原版: 我有两个完全相同的绘图,只有一个例外:颜色(下图)。如何使路径仍然连接?我相信这是因为geom_path()基本上是在构建3个不同的图并将它们重叠。如何为同一路径的不同轨迹着色 作为参考,这是我基本上使用的代码: demand %>% ggplot(aes(x = prices, y = quantities, color = active_segments)) + geom_path() vs 示例数据帧: structure(list(prices = c(210,
demand %>% ggplot(aes(x = prices, y = quantities, color = active_segments)) +
geom_path()
vs
示例数据帧:
structure(list(prices = c(210, 211.5, 213, 214.5, 216, 217.5,
219, 220.5, 222, 223.5, 225, 226.5, 228, 229.5, 231, 232.5, 234,
235.5, 237, 238.5, 240, 241.5, 243, 244.5, 246, 247.5, 249, 250.5,
252, 253.5, 255, 256.5, 258, 259.5, 261, 262.5, 264, 265.5, 267,
268.5, 270, 271.5, 273, 274.5, 276, 277.5, 279, 280.5, 282, 283.5,
285, 286.5, 288, 289.5, 291, 292.5, 294, 295.5, 297, 298.5, 300,
301.5, 303, 304.5, 306, 307.5, 309), quantities = c(1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6,
0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6,
0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.25, 0.25, 0.25, 0.25, 0.25,
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
0.25), Segment1 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Segment2 = c(1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
Segment3 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), active_segments = c("All segments",
"All segments", "All segments", "All segments", "All segments",
"All segments", "All segments", "All segments", "All segments",
"All segments", "All segments", "All segments", "Segment 3 and 2",
"Segment 3 and 2", "Segment 3 and 2", "Segment 3 and 2",
"Segment 3 and 2", "Segment 3 and 2", "Segment 3 and 2",
"Segment 3 and 2", "Segment 3 and 2", "Segment 3 and 2",
"Segment 3 and 2", "Segment 3 and 2", "Segment 3 and 2",
"Segment 3 and 2", "Segment 3 and 2", "Segment 3 and 2",
"Segment 3 and 2", "Segment 3 and 2", "Segment 3 and 2",
"Segment 3 and 2", "Segment 3 and 2", "Segment 3 and 2",
"Segment 3 and 2", "Segment 3 and 2", "Segment 3 and 2",
"Segment 3 and 2", "Segment 3 and 2", "Segment 3", "Segment 3",
"Segment 3", "Segment 3", "Segment 3", "Segment 3", "Segment 3",
"Segment 3", "Segment 3", "Segment 3", "Segment 3", "Segment 3",
"Segment 3", "Segment 3", "Segment 3", "Segment 3", "Segment 3",
"Segment 3", "Segment 3", "Segment 3", "Segment 3", "Segment 3",
"Segment 3", "Segment 3", "Segment 3", "Segment 3", "Segment 3",
"Segment 3")), row.names = c(1L, 151L, 301L, 451L, 601L,
751L, 901L, 1051L, 1201L, 1351L, 1501L, 1651L, 1801L, 1951L,
2101L, 2251L, 2401L, 2551L, 2701L, 2851L, 3001L, 3151L, 3301L,
3451L, 3601L, 3751L, 3901L, 4051L, 4201L, 4351L, 4501L, 4651L,
4801L, 4951L, 5101L, 5251L, 5401L, 5551L, 5701L, 5851L, 6001L,
6151L, 6301L, 6451L, 6601L, 6751L, 6901L, 7051L, 7201L, 7351L,
7501L, 7651L, 7801L, 7951L, 8101L, 8251L, 8401L, 8551L, 8701L,
8851L, 9001L, 9151L, 9301L, 9451L, 9601L, 9751L, 9901L), class = "data.frame")
有趣的问题,因为它突出了美学
颜色=
(或填充=
)和组=
如何在ggplot
中工作ggplot
的编写使得图形的创建非常简单,因此,它可以根据您的输入进行一些猜测和计算
以下是示例数据集和简单路径图:
df <- data.frame(
x=1:10,
y=c(10, 10, 8, 8, 4, 4, 4, 4, 2, 2),
grp=c('A','A','B','B','C','C','C','C','D','D')
)
p <- ggplot(df, aes(x,y)) + theme_bw()
p + geom_path()
仔细想想,ggplot
通过将所有点连接在一起来绘制所有这些线。它足够聪明,知道所提供的数据都是同一组的一部分,并且应该有线连接所有数据:ggplot
将此数据集称为属于同一组。您可以复制类似于您所看到的内容,如下所示:
# method 1:
p + geom_line(aes(color=grp))
# method 2:
p + geom_path(aes(color=grp))
# method 3:
p +
geom_line(data=df[which(df$grp=='A'),], aes(color=grp)) +
geom_line(data=df[which(df$grp=='B'),], aes(color=grp)) +
geom_line(data=df[which(df$grp=='C'),], aes(color=grp)) +
geom_line(data=df[which(df$grp=='D'),], aes(color=grp))
我展示了上面所有三种方法来证明一点:每种方法都在进行相同的事情,而上面描述线条画的最具描述性的方法是方法3。应用color=
美学也可以告诉ggplot
您希望根据df$grp
对点进行分组,并基于该分组连接线。如果删除颜色并希望绘制相同的线条,只需使用组=
美学:
p + geom_path(aes(group=grp))
伟大的但是我们如何解决它呢?
既然您了解了color=
和group=
的工作方式类似,那么在同一个调用中同时使用它们会发生什么?看看上面的例子,我们看到当我们使用color=
时,实际上发生了两件事:
行的颜色调整为匹配df$grp
根据df$grp
当您同时使用组=
和颜色=
美学时,组=
控制线条的连接性,颜色=
美学只控制颜色。这意味着如果我们将其分解,我们希望根据df$grp
对行进行着色,但我们希望根据“整个数据集”连接行。在这里,我们必须指定color=group
,但实际上我们可以为group=
美学设置几乎任何我们想要的东西。。。只要观察结果是一样的:
# method 1 <-- this works
p + geom_path(aes(color=grp, group=1))
# method 2 <-- this works
p + geom_path(aes(color=grp, group='pasta'))
# method 3 <-- this returns an ERROR
p + geom_path(aes(color=grp, group=pasta))
#方法1请以纯文本形式提供demand
中的数据(例如使用dput()
)。问题是“demand”数据框有超过12k行哈哈,让我看看是否可以获得一些好的标记。我不知道dput的事,很酷!还有一个建议:对于大型数据集,您可以通过使用sample()
函数“挑选”具有代表性的行样本。因此,对于demand
,您可以通过:dput(demand[sample(1:nrow(demand),100),])
对100行进行采样。
p + geom_path(aes(group=grp))
# method 1 <-- this works
p + geom_path(aes(color=grp, group=1))
# method 2 <-- this works
p + geom_path(aes(color=grp, group='pasta'))
# method 3 <-- this returns an ERROR
p + geom_path(aes(color=grp, group=pasta))