且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在其中一个方面添加一条线?

更新时间:2023-02-14 15:03:39

我没有您的数据,所以我做了一些弥补:

I don't have your data, so I made some up:

df <- data.frame(x=rnorm(100),y=rnorm(100),z=rep(letters[1:4],each=25))

ggplot(df,aes(x,y)) +
  geom_point() +
  theme_bw() +
  facet_wrap(~z)

要在x = 1处添加垂直线,我们可以将geom_vline()与具有相同构面变量(在我的情况下为z='b',但您将为levels='major')的数据框一起使用:

To add a vertical line at x = 1 we can use geom_vline() with a dataframe that has the same faceting variable (in my case z='b', but yours will be levels='major'):

ggplot(df,aes(x,y)) +
  geom_point() +
  theme_bw() +
  facet_wrap(~z) +
  geom_vline(data = data.frame(xint=1,z="b"), aes(xintercept = xint), linetype = "dotted")