Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjusting rugplot in ggplot2

Tags:

r

ggplot2

Below is the code for a graph I am making for an article I am working on. The plot showed the predicted probabilities along a range of values in my data set. Along the x-axis is a rug plot that shows the distribution of trade share values (I provided the code and an image of the graph):

sitc8 <- ggplot() + geom_line(data=plotdat8, aes(x = lagsitc8100, y = PredictedProbabilityMean), size = 2, color="blue") + 
  geom_ribbon(data=plotdat8, aes(x = lagsitc8100, ymin = lowersd, ymax = uppersd),
              fill = "grey50", alpha=.5) +
  ylim(c(-0.75, 1.5)) + 
  geom_hline(yintercept=0) +
  geom_rug(data=multi.sanctions.bust8.full@frame, aes(x=lagsitc8100), col="black", size=1.0, sides="b") + 
  xlab("SITC 8 Trade Share") + 
  ylab("Probability of Sanctions Busting") + 
  theme(panel.grid.major = element_line(colour = "gray", linetype = "dotted"), panel.grid.minor = 
  element_blank(), panel.background = element_blank())

enter image description here

My question is: is it possible to change the color of the lines of the rugplot of trade share in which the event I am modeling occurs? In other words, I would like to add red lines or red dots along those values of trade share when my event = 1.

Is this possible?

like image 546
Keith Avatar asked Aug 05 '26 09:08

Keith


2 Answers

Sure. You'd just have to add a color argument within an aes() function call within geom_rug().

Here's some code to create a dummy data frame.

library(tidyverse)

set.seed(42)
dummy_data <- tibble(x_var = rnorm(100),
                     y_var = abs(rnorm(100)) * x_var) %>%
rownames_to_column(var = "temp_row") %>% 
mutate(color_id = if_else(as.numeric(temp_row) <= 50,
                          "Type A",
                          "Type B"))

And here's a ggplot call where the color for geom_rug is mapped to a character column named color_id

ggplot(data = dummy_data, mapping = aes(x = x_var, y = y_var)) +
  geom_smooth(method = "lm") +
  geom_rug(mapping = aes(color = color_id), sides = "b")

enter image description here

Update:

Following OP's comment, here's an updated version. If it's a numeric vector of 0s and 1s, you have to tell ggplot to treat it as a dichotomous variable. You can do that by wrapping it in a call to factor() for instance.

For the color we can set that manually using scale_color_manual(). So the changes to the code are the following.

  • color_id is now a vector og 0s and 1s.
  • the color is now mapped to factor(color_id)
  • the color scale is determined using scale_color_manual
library(tidyverse)

set.seed(42)
dummy_data <- tibble(x_var = rnorm(100),
                     y_var = abs(rnorm(100)) * x_var) %>%
  rownames_to_column(var = "temp_row") %>% 
  mutate(color_id = if_else(as.numeric(temp_row) <= 50,
                            0,
                            1))

ggplot(data = dummy_data, mapping = aes(x = x_var, y = y_var)) +
  geom_smooth(method = "lm") +
  geom_rug(mapping = aes(color = factor(color_id)), sides = "b") +
  scale_color_manual(values = c("black", "red")) +
  labs(color = "This takes two values")

enter image description here

like image 122
Hlynur Avatar answered Aug 07 '26 23:08

Hlynur


Definitely possible. Here's an example using iris, and a dynamic condition in the rug. You could also do two rugs, if you chose.

library(tidyverse)
iris %>%
  ggplot(aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  geom_rug(aes(color = Petal.Length >3), sides = "b") 

# Second example, output not shown
iris %>%
  ggplot(aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  geom_rug(data = subset(iris, Petal.Length > 3), color = "black", sides = "b") +
  geom_rug(data = subset(iris, Petal.Length <= 3), color = "red", sides = "b") 


like image 25
ravic_ Avatar answered Aug 07 '26 23:08

ravic_