1

I am trying to make a series of graphs that are based on a binomial variable. I want to add data points to the graph based on a different factored variable with 3 levels. I have been trying to use geom_jitter which worked to put the points on the box plot but I havent been able to change the colors to represent the different levels of the factored variable.

Here is the code I have been using

longg <- ggplot(long, aes(x = mbbase, y= beta)) + 
  geom_boxplot() + facet_wrap(~test) + 
  ylab("Beta") + 
  theme_cleveland() +
  scale_fill_viridis(discrete = TRUE, alpha=0.09) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) + 
  theme(axis.title.x = element_blank()) + 
  geom_jitter( size=0.7, alpha=1, width = 0.05)

Here is an example of the graph I want with the mtcars data but instead of a numeric variable as the color id like a factored variable with 3 levels but I only want the color of the data points to change without adding a new box plot for each level of the factored variable

2
  • 1
    It would be easier to help if you create a small reproducible example along with expected output. Read about how to give a reproducible example. Commented Mar 27, 2021 at 2:59
  • @RonakShah I updated the question with an example hopefully that helps! Commented Mar 27, 2021 at 18:11

1 Answer 1

1

With mtcars, you can try this:

library(ggplot2)
library(dplyr)
library(viridis)

mtcars %>%
# optional: divide the column to color in three. There are more elegant ways
#to do it, but in this way probably it's easier to use it in your data
mutate(new_carb = as.factor(ifelse(carb %in% c(1,2),1,
                                 ifelse(carb %in% c(3,4),2,3)))) %>%
ggplot( aes(x = as.factor(am), y= mpg)) + 
  geom_boxplot() +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
        axis.title.x = element_blank()) +
  geom_boxplot(outlier.shape=NA) + 
  # add the color here
  geom_jitter(aes(color = new_carb),
                  size=0.7, alpha=1, width = 0.05) +
  scale_color_viridis(discrete = TRUE, alpha=0.09)

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.