如何在使用scale_y_reverse()时将geom_area翻转到线下方

cwtwac6a  于 2023-02-27  发布在  其他
关注(0)|答案(1)|浏览(100)

我不得不翻转我的线的轴,但仍然需要geom_area在曲线下。但是我不知道如何做到这一点。
这是我试过的代码行

ggplot(PalmBeachWell, aes(x=Date, y=Depth.to.Water.Below.Land.Surface.in.ft.)) + 
  geom_area(position= "identity", fill='lightblue') + 
  theme_classic() + 
  geom_line(color="blue") + 
  scale_y_reverse()

这是我得到的

shyt4zoc

shyt4zoc1#

一个选项是使用geom_ribbon填充曲线上方的区域,在应用scale_y_reverse之后,将导致曲线下方的填充。
使用基于ggplot2::economics数据集的一些伪示例数据:

library(ggplot2)

PalmBeachWell <- economics[c("date", "psavert")]
names(PalmBeachWell) <- c("Date", "Depth.to.Water.Below.Land.Surface.in.ft.")

ggplot(PalmBeachWell, aes(x = Date, y = Depth.to.Water.Below.Land.Surface.in.ft.)) +
  geom_ribbon(aes(ymin = Depth.to.Water.Below.Land.Surface.in.ft., ymax = Inf),
    fill = "lightblue"
  ) +
  geom_line(color = "blue") +
  scale_y_reverse() +
  theme_classic()

相关问题