Home > OS >  Set color of the boxes in a boxplot
Set color of the boxes in a boxplot

Time:01-22

hist <- ggplot(df, aes(x = A,fill = ("red")))   
  geom_bar()   
  theme_minimal()   
  ggtitle("Treatment")   theme(legend.position = "bottom", plot.title = element_text(hjust=0.5), text = element_text(size = 20))   
  scale_fill_manual("A", values = c("0" = "dodgerblue4", "1" = "chocolate"))
    
hist

Image

I need help regarding setting the colors of the boxplots from the ggplot2 package which I am learning right now. I want the left one to be in the blue color and the right one in the other color. But unfortunately I cannot figure out how to set these instead this code just sets the right colors in the Legend and leaves the boxes unchanged?!

CodePudding user response:

Not enough code for me to know exactly what you're doing but I think if you remove:

mapping = aes(fill = "red")

it will solve your problem. A reprex would help others better understand your problem. I think instead you might need something like:

    ggplot(data = your_data)  
    geom_bar(mapping = aes(x = variable_1, y = variable_2, fill = variable_3), position = "dodge") 

CodePudding user response:

Like Rene said it is a bit hard to help while not seeing the dataset. Looking at the given code your A contains two values you want to color individually define a vector containing those two values:

df_colors <- c("0" , "1")

hist <- ggplot(df, aes(x = A, fill = df_colors ))   
  geom_bar()   
  theme_minimal()   
  ggtitle("Treatment")   theme(legend.position = "bottom", plot.title = element_text(hjust=0.5), text = element_text(size = 20))   
  scale_fill_manual("A", values = c("0" = "dodgerblue4", "1" = "chocolate"))
  •  Tags:  
  • Related