Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

ggplot2

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?

like image 413
ohnoplus Avatar asked Sep 11 '17 16:09

ohnoplus


People also ask

What does the stroke aesthetic do?

stroke adjusts the thickness of the border for shapes that can take on different colors both inside and outside. It only works for shapes 21-24. What happens if you map an aesthetic to something other than a variable name, like aes(colour = displ < 5) ?

What is a rug plot in R?

Source: R/geom-rug.r. geom_rug.Rd. A rug plot is a compact visualisation designed to supplement a 2d display with the two 1d marginal distributions. Rug plots display individual cases so are best used with smaller datasets.

What is rug in statistics?

A rug plot is a plot of data for a single quantitative variable, displayed as marks along an axis. It is used to visualise the distribution of the data. As such it is analogous to a histogram with zero-width bins, or a one-dimensional scatter plot.

What is ggplot2 in R?

ggplot2 is a robust and a versatile R package, developed by the most well known R developer, Hadley Wickham, for generating aesthetic plots and charts. The ggplot2 implies " Grammar of Graphics " which believes in the principle that a plot can be split into the following basic parts - data refers to a data frame (dataset).

How to create a ggplot2 plot with two different data frames?

Example: Creating ggplot2 Plot with Two Different Data Frames You have to specify NULL within the ggplot function. Then, you have to specify the different data sets within the geom_point and geom_line functions. my_plot <- ggplot (NULL) + # Printing ggplot2 grphic based on 2 data sets geom_point (data = iris, aes (x = Sepal.

What is rug plot in R?

Source: R/geom-rug.r A rug plot is a compact visualisation designed to supplement a 2d display with the two 1d marginal distributions. Rug plots display individual cases so are best used with smaller datasets.

How to use themes in ggplot2?

Themes can be used in ggplot2 to change the backgrounds,text colors, legend colors and axis texts. Firstly we save our plot to 'b' and hence create the visualizations by manipulating 'b'. Note that in aesthetics we have written mpg, disp which automatically plots mpg on x axis and disp on y axis.


2 Answers

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

like image 83
Z.Lin Avatar answered Oct 18 '22 12:10

Z.Lin


I would try either this:

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

or perhaps a better option this:

ggplot() + 
  geom_point(data = test,aes(x = visit,y = mag)) + 
  geom_rug(data=vac, aes(x = visit))
like image 26
joran Avatar answered Oct 18 '22 11:10

joran