Search code examples
rggplot2violin-plot

How can I change the color of a violin plot in R?


I was just trying to change the color of my violin plot, but it isn't working at all and the color remains the same; What is the problem?

I want to set a 'light blue' color for the first group and a 'red' color for the second group.

Here is the code:

ggplot(data = Final , mapping = aes(x = Hot_group , y = PI.dAI , fill = Hot_group)) + 
  geom_violin(scale = "count" , position = "dodge", trim=FALSE) +
  geom_jitter(width = 0.25 , shape = 21 , color = "black") +
  geom_dotplot(binaxis = "y" , binwidth = 0.005, stackdir = "center") +
  labs(title = "A violin chart",
       x = "The frequency",
       y = "correlation") +
  facet_wrap(~PSQI_group,
             labeller = labeller(PSQI_group = c("0" = "No",
                                                "1" = "Yes"))) +
  stat_summary(fun = mean , geom = "point", shape = 18 , size = 2 , color = "black") +
  stat_summary(fun.data = mean_sdl, geom = "errorbar", color = "black", width = 0.2 , size = 0.5) +
  scale_x_discrete(labels = c("0" = "group 1",
                              "1" = "group 2")) +
  theme(plot.title = element_text(size = 16, face = "bold"),
        axis.title = element_text(size = 14),
        legend.title = element_text(size = 12),
        legend.text = element_text(size = 10),
        legend.position = "right") +
  geom_boxplot(width=0.1) +
  guides(fill = guide_legend(title = "The frequency")) +
  scale_fill_discrete(labels = c("group 1",
                                 "group 2")) +
  scale_color_manual(values = c("lightblue", "red")) +
  theme_classic()

I'd appreciate your helps

I tried different functions, including "scale_fill_manual" and "scale_color_manual" but nothing changed


Solution

  • I created a similar dataset based on your code and made slight modifications to it.

       library(Hmisc)
       library(ggplot2)
    
      Final=data.frame(
      PSQI_group= rep(c("0", "1"), each = 16),
      Hot_group=rep(c("0", "1"), time = 16),
      PI.dAI=c(
        42.9, 41.6, 28.9, 30.8, 53.3, 69.6, 45.4, 35.1,
        62.3, 58.5, 44.6, 50.3, 75.4, 65.6, 54.0, 52.7,
        53.8, 58.5, 43.9, 46.3, 57.6, 69.6, 42.4, 51.9,
        63.4, 50.4, 45.0, 46.7, 70.3, 67.3, 57.6, 58.5,
        49.5, 53.8, 40.7, 39.4, 59.8, 65.8, 41.4, 45.4,
        64.5, 46.1, 62.6, 50.3, 68.8, 65.3, 45.6, 51.0,
        44.4, 41.8, 28.3, 34.7, 64.1, 57.4, 44.1, 51.6,
        63.6, 56.1, 52.7, 51.8, 71.6, 69.4, 56.6, 47.4)
    ) 
    
    ggplot(data=Final , aes(x=Hot_group, y=PI.dAI, fill=Hot_group)) + 
      geom_violin(scale="count", position="dodge", trim=FALSE) +
      geom_jitter(width=0.25, shape=21 , color="black") +
      geom_dotplot(binaxis="y" , binwidth=0.005, stackdir="center") +
      geom_boxplot(width=0.1) +
      labs(title="A violin chart", x="The frequency", y="correlation") +
      facet_wrap(~PSQI_group, labeller=labeller(PSQI_group=c("0"="No", "1"="Yes"))) +
      stat_summary(fun=mean, geom="point", shape=18, size=2 , color="black") +
      stat_summary(fun.data=mean_sdl, geom="errorbar", color="black", width=0.2 , size=0.5) +
      scale_x_discrete(labels=c("0"="group 1", "1"="group 2")) +
      scale_fill_manual(values=c("lightblue", "red")) +
      guides(fill=guide_legend(title="The frequency")) +
      theme_classic() +
      theme(plot.title= element_text(size=16, face = "bold"),
            axis.title= element_text(size=14),
            legend.title= element_text(size=12),
            legend.text= element_text(size=10),
            legend.position = "right")
    

    enter image description here