Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

r - How can I add a line to one of the facets?

ggplot(all, aes(x=area, y=nq)) +
  geom_point(size=0.5) +
  geom_abline(data = levelnew, aes(intercept=log10(exp(interceptmax)), slope=fslope)) + #shifted regression line
  scale_y_log10(labels = function(y) format(y, scientific = FALSE)) + 
  scale_x_log10(labels = function(x) format(x, scientific = FALSE)) + 
  facet_wrap(~levels) +
  theme_bw() +
  theme(panel.grid.major = element_line(colour = "#808080"))

And I get this figure

enter image description here

Now I want to add one geom_line to one of the facets. Basically, I wanted to have a dotted line (Say x=10,000) in only the major panel. How can I do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

enter image description here

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")

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...