ggplot: 如何在三个不同的geom_line()下应用geom_area()或类似的填充区域而不重叠每个独立的填充?

问题描述 投票:0回答:1

我有一个基于我的数据的图 nd 有三个 geom_line() 说明1年后死亡的概率。nd$y_et, 3年后 nd$y_tre 和5年后的死亡概率,分别是切除淋巴结数量的函数。nd$y_fem分别是切除的淋巴结数量的函数。nd$n_fjernet.

问题。 我怎样才能填满每个区域下面的三个单独的。geom_line()nd$y_et, y_tre, y_fem填充物不与后续的 geom_line + fill?

我试过了 geom_areageom_polygon 但并没有接近一个合适的解决方案。

当前图

enter image description here

随着

ggplot(nd, aes(x=n_fjernet))  +
         geom_line(aes(y=y_et)) + 
         geom_line(aes(y=y_tre)) + 
         geom_line(aes(y=y_fem)) +    scale_x_continuous(breaks = seq(0,25,5), limits=c(0,25))

应该会有预期的输出。

enter image description here

UPDATE

我采用了下面提供的解决方案,得到

ndd %>% 
  rename(X3=y_et, X2=y_tre, X1=y_fem) %>% 
  pivot_longer(values_to="N", names_to="Variable", cols=c(X1:X3)) %>%
  ggplot(aes(x=n_fjernet, y=N, fill=Variable, colour=Variable)) +
  geom_area(position=position_identity(), alpha=.15) +
  geom_line(size=3, color="white") +
  geom_line(size=.75) +
  scale_fill_manual(values=c("#2C77BF", "#E38072", "#6DBCC3")) +
  scale_colour_manual(values=c("#2C77BF", "#E38072", "#6DBCC3")) +
  scale_x_continuous(breaks = seq(0,10,5), limits=c(0,10))

随着

enter image description here

由于我们越来越接近预定的情节,不幸的是仍然有重叠的。fills. 该 blue-fill 隐约可见 red-fill;而且,这两个 blue-fillred-fill 在后面 green-fill.

问题: 如何列入 fills 不重合?

我的数据 nd

    nd <- structure(list(y_et = c(0.473, 0.473, 0.472, 0.471, 0.471, 0.47, 
0.47, 0.469, 0.468, 0.468, 0.467, 0.467, 0.466, 0.465, 0.465, 
0.464, 0.464, 0.463, 0.462, 0.462, 0.461, 0.461, 0.46, 0.459, 
0.459, 0.458, 0.458, 0.457, 0.456, 0.456, 0.455, 0.455, 0.454, 
0.453, 0.453, 0.452, 0.452, 0.451, 0.45, 0.45, 0.449, 0.449, 
0.448, 0.447, 0.447, 0.446, 0.446, 0.445, 0.445, 0.444, 0.443, 
0.443, 0.442, 0.442, 0.441, 0.44, 0.44, 0.439, 0.439, 0.438, 
0.438, 0.437, 0.436, 0.436, 0.435, 0.435, 0.434, 0.433, 0.433, 
0.432, 0.432, 0.431, 0.431, 0.43, 0.429, 0.429, 0.428, 0.428, 
0.427, 0.427, 0.426, 0.425, 0.425, 0.424, 0.424, 0.423, 0.423, 
0.422, 0.421, 0.421, 0.42, 0.42, 0.419, 0.419, 0.418, 0.417, 
0.417, 0.416, 0.416, 0.415), y_tre = c(0.895, 0.894, 0.894, 0.893, 
0.893, 0.893, 0.892, 0.892, 0.891, 0.891, 0.89, 0.89, 0.889, 
0.889, 0.889, 0.888, 0.888, 0.887, 0.887, 0.886, 0.886, 0.886, 
0.885, 0.885, 0.884, 0.884, 0.883, 0.883, 0.882, 0.882, 0.881, 
0.881, 0.881, 0.88, 0.88, 0.879, 0.879, 0.878, 0.878, 0.877, 
0.877, 0.876, 0.876, 0.875, 0.875, 0.875, 0.874, 0.874, 0.873, 
0.873, 0.872, 0.872, 0.871, 0.871, 0.87, 0.87, 0.869, 0.869, 
0.868, 0.868, 0.867, 0.867, 0.866, 0.866, 0.865, 0.865, 0.865, 
0.864, 0.864, 0.863, 0.863, 0.862, 0.862, 0.861, 0.861, 0.86, 
0.86, 0.859, 0.859, 0.858, 0.858, 0.857, 0.857, 0.856, 0.856, 
0.855, 0.855, 0.854, 0.854, 0.853, 0.853, 0.852, 0.852, 0.851, 
0.851, 0.85, 0.85, 0.849, 0.848, 0.848), y_fem = c(0.974, 0.974, 
0.973, 0.973, 0.973, 0.973, 0.973, 0.973, 0.972, 0.972, 0.972, 
0.972, 0.972, 0.971, 0.971, 0.971, 0.971, 0.971, 0.971, 0.97, 
0.97, 0.97, 0.97, 0.97, 0.969, 0.969, 0.969, 0.969, 0.969, 0.968, 
0.968, 0.968, 0.968, 0.968, 0.967, 0.967, 0.967, 0.967, 0.967, 
0.966, 0.966, 0.966, 0.966, 0.966, 0.965, 0.965, 0.965, 0.965, 
0.965, 0.964, 0.964, 0.964, 0.964, 0.963, 0.963, 0.963, 0.963, 
0.963, 0.962, 0.962, 0.962, 0.962, 0.961, 0.961, 0.961, 0.961, 
0.961, 0.96, 0.96, 0.96, 0.96, 0.959, 0.959, 0.959, 0.959, 0.958, 
0.958, 0.958, 0.958, 0.957, 0.957, 0.957, 0.957, 0.957, 0.956, 
0.956, 0.956, 0.956, 0.955, 0.955, 0.955, 0.955, 0.954, 0.954, 
0.954, 0.954, 0.953, 0.953, 0.953, 0.952), n_fjernet = c(0, 0.1, 
0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.1, 1.2, 1.3, 1.4, 
1.5, 1.6, 1.7, 1.8, 1.9, 2, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 
2.8, 2.9, 3, 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4, 
4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5, 5.1, 5.2, 5.3, 
5.4, 5.5, 5.6, 5.7, 5.8, 5.9, 6, 6.1, 6.2, 6.3, 6.4, 6.5, 6.6, 
6.7, 6.8, 6.9, 7, 7.1, 7.2, 7.3, 7.4, 7.5, 7.6, 7.7, 7.8, 7.9, 
8, 8.1, 8.2, 8.3, 8.4, 8.5, 8.6, 8.7, 8.8, 8.9, 9, 9.1, 9.2, 
9.3, 9.4, 9.5, 9.6, 9.7, 9.8, 9.9)), row.names = c(NA, -100L), class = c("data.table", 
"data.frame"))
r ggplot2 plot fill geom-area
1个回答
2
投票
nd %>% 
  pivot_longer(values_to="N", names_to="Variable", cols=c(y_fem:y_et)) %>% 
  ggplot(aes(x=n_fjernet, y=N, fill=Variable)) + geom_area()

给出enter image description here

这只是一个问题,让你的数据 整洁 在你的目标背景下。 在这里,你的数据并不整洁,因为你的列名包含信息。

为了回应OP的评论...

啊!这让事情变得有点棘手。 默认的 position 在...中 geom_areastack这意味着每个彩色区域的高度是相应变量的高度(堆栈的总高度是各个值的总和--例如,在 n_fjernet = 0, you havey_fem = 0.981,y_tre = 0.9199andy_et = 0.514, giving a total stack height of about2.5`. 从你的原图来看,你想把每条线的原始值绘制出来,然后填满它和下一个低端同伴之间的差距,对吧?

原则上,这很简单。 你可以只设置 position=position_identity() 在你 geom_area(). 但是,如果要按照你想要的方式工作,你需要手动跟踪变量值的顺序。 例如,根据你的数据,我们得到。

nd %>% 
  pivot_longer(values_to="N", names_to="Variable", cols=c(y_fem:y_et)) %>% 
  ggplot(aes(x=n_fjernet, y=N, fill=Variable)) + 
  geom_area(position=position_identity())

enter image description here

完全不是你想要的

一个真正的黑客方法可以得到正确的结果。这回

nd %>% 
  rename(X3=y_et, X2=y_tre, X1=y_fem) %>% 
  pivot_longer(values_to="N", names_to="Variable", cols=c(X1:X3)) %>% 
  ggplot(aes(x=n_fjernet, y=N, fill=Variable)) +   
  geom_area(position=position_identity())

enter image description here

您还可以通过自定义用于创建填充的比例来控制区域的绘制顺序,如上所述。此处.

另一个选择是使用 geom_ribbon 而非 geom_area. 但无论你使用哪种方法,我都不知道你如何在不手动控制填充创建顺序的情况下做到这一点。 这似乎是希望在绝对位置上绘制数值并填充下面的区域的一个不可避免的结果。 我能想到的唯一可行的办法是为每个填充设置一个小于1的alpha值。 但是,我个人认为这看起来很丑陋。

nd %>% 
  pivot_longer(values_to="N", names_to="Variable", cols=c(y_fem:y_et)) %>% 
  ggplot(aes(x=n_fjernet, y=N, fill=Variable)) + 
  geom_area(position=position_identity(), alpha=0.4)

enter image description here

而且当你沿着X轴移动时,变量的顺序会发生变化,你会怎么做? 就个人而言,我会放弃填充,只使用不同颜色的线条。 但这是你的决定。

如果有人有更好的方案,我很想看看。

* 编辑2回答OP关于手动控制颜色的问题。

nd %>% 
  rename(X3=y_et, X2=y_tre, X1=y_fem) %>% 
  pivot_longer(values_to="N", names_to="Variable", cols=c(X1:X3)) %>% 
  ggplot(aes(x=n_fjernet, y=N, fill=Variable, colour=Variable)) + 
  geom_area(position=position_identity()) +
  geom_line() +
  scale_fill_manual(values=c("pink", "darkseagreen2", "steelblue2")) +
  scale_colour_manual(values=c("red", "green4", "blue"))

给我

enter image description here

据我所知,我的代码和你的代码很相似,所以我不知道为什么对我有用而对你没用。 [你是否记得把 colour=Variable 里面 aes()?]

I get my colors from 此处.

你提到 geom_point 在你的评论中。 是打错了吗?

顺便说一下,我们不需要所有的200个数据点来解决这个问题。 半打就够了 最多一打。 也许下一次... ; )

© www.soinside.com 2019 - 2024. All rights reserved.