【问题标题】:Geom_area with different colors for fillGeom_area 用不同颜色填充
【发布时间】:2020-12-03 13:14:11
【问题描述】:

我有一个DF,有 4 列:dateoriginalpredictedchange。我想制作一个面积图,根据change 列,负值是红色,正值是绿色。

我的代码:

data %>% 
  filter(variaveis == c("diferenca.em.pontos")) %>%
  mutate(change = ifelse(value > 0, "positive", "negative")) %>%
  ggplot(aes(x = Período, y = value, fill = change)) +
  geom_area() +
  geom_hline(yintercept = 0)

但它会像这样返回,这条绿线高于红色值,这是不应该发生的。我做错了什么?

如果我使用geom_col 而不是geom_area,则图表是正确的:

我的dput

structure(list(Período = structure(c(13453, 13483, 13514, 13545, 
13573, 13604, 13634, 13665, 13695, 13726, 13757, 13787, 13818, 
13848, 13879, 13910, 13939, 13970, 14000, 14031, 14061, 14092, 
14123, 14153, 15918, 15949, 15979, 16010, 16040, 16071), class = "Date"), 
    variaveis = structure(c(5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
    5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
    5L, 5L, 5L, 5L, 5L, 5L), .Label = c("Brasil", "Brasil.predicted", 
    "Brasil.nivel", "Brasil.predicted.nivel", "diferenca.em.pontos"
    ), class = "factor"), value = c(28.5307986472031, 22.8246389177625, 
    16.3033135126875, 6.52132540507499, 4.07582837817188, -12.2274851345156, 
    -10.5971537832469, -8.96682243197813, -6.521325405075, -4.89099405380624, 
    -11.4123194588812, -20.3791418908594, -8.96682243197812, 
    -10.5971537832469, -9.7819881076125, -6.521325405075, 0, 
    -8.15165675634375, -27.7156329715687, -43.2037808086219, 
    -34.2369583766437, -26.9004672959344, -17.9336448639562, 
    -38.3127867548156, -18.7488105395906, -25.2701359446656, 
    -21.1943075664937, -14.6729821614187, -8.15165675634375, 
    0.815165675634374), change = c("positive", "positive", "positive", 
    "positive", "positive", "negative", "negative", "negative", 
    "negative", "negative", "negative", "negative", "negative", 
    "negative", "negative", "negative", "negative", "negative", 
    "negative", "negative", "negative", "negative", "negative", 
    "negative", "negative", "negative", "negative", "negative", 
    "negative", "positive")), row.names = c(1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
19L, 20L, 21L, 22L, 23L, 24L, 82L, 83L, 84L, 85L, 86L, 87L), class = "data.frame")

【问题讨论】:

  • 问题是geom_area基本上是geom_line的一种。因此,每组的数据点得到连接。
  • 没有办法按照我提到的那种方式绘制图形吗?
  • 有一种方法,但最好改用geom_ribbon。要使其工作,您需要为每个 x 值和填充颜色计算 yminymax
  • geom_areageom_linegeom_ribbon 的混合体。它按 x 变量的顺序连接观察值,但将 ymin/ymax 变量之一指定为零。
  • 您可以尝试将tidyr::complete(change, Período, fill = list(value = 0)) 添加为数据预处理的最后一部分,然后再将结果传送到ggplot。这会在两者之间添加 0 个值。

标签: r ggplot2 geom-area


【解决方案1】:

不要尝试在同一个 geom_area() 函数中绘制所有内容,而是尝试使用它两次:一次用于正值,另一次用于负值。可能不是最好的解决方案,但它确实有效。

ggplot(dput) +
geom_area(aes(x=Período, y=ifelse(value<0, value,0)), fill="red") +
geom_area(aes(x=Período, y=ifelse(value>0, value, 0)), fill="green")

Resulting plot with dput

我希望这对你有所帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-23
    • 2021-11-09
    • 2020-10-03
    • 1970-01-01
    相关资源
    最近更新 更多