Creating A Rug Plot in ggplot2 with a line plot for PDP




I am trying to create a plot from 2 sets of data similar to the one produced here.

I want to plot the performance of a PDP plot but also want to put the amount of people in a rug plot below the PDP model similar to the link above. I can't seem to make it work. Can anyone see where I am going wrong

library(tidyverse, quietly = TRUE)

# Give you your main data
x = sample(1:7, replace = TRUE, size = 1000)
y = sample(seq(1:1000), replace = TRUE, size = 1000)/1000

main_df <- tibble(x,y) %>% 
  group_by(x) %>% 
  summarise(n = mean(y))

# Number of People to be used for the rug plot
score <- seq(1, 7, length.out = 17)
n <- sample(1:100, 17)
rug_data <- tibble(score, n)

# Attempt to plot it (line and point work find separately but when combined with rug plot gives a poor plot)
ggplot(main_df, aes(x=x, y=n)) +
  geom_line() +
  geom_point() +
  geom_rug(data=rug_data, aes(x=score, alpha = n), col="steelblue", size=1.5, sides="b")



You're almost there, you just need to add inherit.aes = FALSE to your geom_rug() call. Otherwise (the default setup), ggplot tries to combine the aesthetics in geom_rug with the default aesthetics from ggplot(). You had two different things both named n, and I think that in this case that means the y-axis limits were set based on all the ns, which obviously results in the wrong scale for the data in main_df$n.


Thank you very much @jcblum