Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

r - Using different data in ggplot's geom_rug than I use in the rest of the plot

I am having trouble getting geom_rug to plot some data into an existing plot. Here's an example plot, where I am comparing some visit day to the magnitude of some measurement.

test <- data.frame(
    visit = rep(c(0, 1.5, 3.5, 6.5, 12), 5),
    mag = rnorm(n = 25)
    )

ggplot(test, aes(x = visit, y = mag)) + geom_point()

Which generates the following plot. testplot2

I also have some other data, that I'd like to add just as extra marks on the x axis.

vac <- data.frame(
    visit = c(2, 4, 6, 8)
    )

For reasons I don't understand, I get no plot at all when I run the following code.

ggplot(test, aes(x = visit, y = mag)) + geom_point() + 
      geom_rug(data=vac, aes(x = visit))

I presume I have messed up on syntax somehow, but I can't seem to figure out what I am doing wrong here. Any suggestions?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You should specify inherit.aes = FALSE in the geom_rug() line, otherwise it inherits y = mag from the main ggplot() call.

ggplot(test, aes(x = visit, y = mag)) + 
  geom_point() + 
  geom_rug(data=vac, aes(x = visit), inherit.aes = F)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...