How to change the colors of the dots in the graph? ggpubr package

Viewed 546

I am using the ggerrorplot () function of the ggpubr package to create the graph below. My question is whether there is any way to change the colors of the dots without changing the color of the point that represents the mean and standard deviation? Observe the image:

enter image description here

My code:

# loading packages
library(ggpubr)

# Create data frame
GROUP <- c()
TEST <- c()
VALUE <- c()
for (i in 0:100) {
  gp <- c('Group1','Group2','Group1 and Group2')
  ts <- c('Test1','Test2') 
  GROUP <- append(GROUP, sample(gp, 1))
  TEST <- append(TEST, sample(ts, 1))
  VALUE <-  append(VALUE, sample(1:200, 1))
}
df <- data.frame(GROUP, TEST, VALUE)

# Seed
set.seed(123)

# Plot
ggerrorplot(df, x = "GROUP", y = "VALUE",
            desc_stat = "mean_sd",
            add = c("jitter"),
            color = "TEST", 
            palette = "jco",
            add.params = list(size = 0.2),
            order = c('Group1','Group2','Group1 and Group2')
            ) +
  labs(x = '', y = 'Values\n') +
  theme(legend.title = element_blank())
2 Answers

Can you accomplish this by simply passing in color to add.params?

# loading packages
library(ggpubr)
#> Loading required package: ggplot2

# Create data frame
GROUP <- c()
TEST <- c()
VALUE <- c()
for (i in 0:100) {
  gp <- c('Group1','Group2','Group1 and Group2')
  ts <- c('Test1','Test2') 
  GROUP <- append(GROUP, sample(gp, 1))
  TEST <- append(TEST, sample(ts, 1))
  VALUE <-  append(VALUE, sample(1:200, 1))
}
df <- data.frame(GROUP, TEST, VALUE)

# Seed
set.seed(123)

# Plot
ggerrorplot(df, x = "GROUP", y = "VALUE",
            desc_stat = "mean_sd",
            add = c("jitter"),
            color = "TEST", 
            palette = "jco",
            add.params = list(size = 0.2, color = "red"),
            order = c('Group1','Group2','Group1 and Group2')
            ) +
  labs(x = '', y = 'Values\n') +
  theme(legend.title = element_blank())

Created on 2021-03-10 by the reprex package (v0.3.0)

Another potential workaround - replicate the plot using ggplot() and geom_linerange(), e.g.

library(ggpubr)
library(ggsci)
library(cowplot)

# Create data frame
GROUP <- c()
TEST <- c()
VALUE <- c()
for (i in 0:100) {
  gp <- c('Group1','Group2','Group1 and Group2')
  ts <- c('Test1','Test2') 
  GROUP <- append(GROUP, sample(gp, 1))
  TEST <- append(TEST, sample(ts, 1))
  VALUE <-  append(VALUE, sample(1:200, 1))
}
df <- data.frame(GROUP, TEST, VALUE)

# Seed
set.seed(123)

data_summary <- function(data, varname, groupnames){
  require(plyr)
  summary_func <- function(x, col){
    c(mean = mean(x[[col]], na.rm=TRUE),
      sd = sd(x[[col]], na.rm=TRUE))
  }
  data_sum<-ddply(data, groupnames, .fun=summary_func,
                  varname)
  data_sum <- rename(data_sum, c("mean" = varname))
  return(data_sum)
}

df2 <- data_summary(df, varname = "VALUE", groupnames = c("TEST", "GROUP"))

# Plot
p1 <- ggplot(df, aes(x = factor(GROUP, levels = c('Group1','Group2','Group1 and Group2')),
               y = VALUE, color = TEST)) +
  geom_jitter(shape = 21, fill = "black", stroke = 0,
              position = position_jitterdodge(jitter.width = 0.2)) +
  geom_linerange(data = df2, aes(ymin=VALUE-sd, ymax=VALUE+sd),
                position=position_dodge(width = .75)) +
  geom_point(data = df2, aes(y = VALUE), size = 3,
             position = position_dodge(width = 0.75)) +
  scale_color_jco() +
  labs(x = '', y = 'Values\n') +
  theme_classic(base_size = 14) +
  theme(legend.title = element_blank(),
        legend.position = "top")

p2 <- ggerrorplot(df, x = "GROUP", y = "VALUE",
                  desc_stat = "mean_sd",
                  add = c("jitter"),
                  color = "TEST", 
                  palette = "jco",
                  add.params = list(size = 0.2),
                  order = c('Group1','Group2','Group1 and Group2')
) +
  labs(x = '', y = 'Values\n') +
  theme(legend.title = element_blank())

cowplot::plot_grid(p1, p2, nrow = 1, ncol = 2, labels = "AUTO")

When you plot them side-by-side you can see that they aren't exactly the same, but this might work for you nonetheless.

example.png

Edit

An advantage of this approach is that you can adjust the 'fill' scale separately if you don't want all the dots to be the same colour, but you do want them to be different to the lines, e.g.

p1 <- ggplot(df, aes(x = factor(GROUP, levels = c('Group1','Group2','Group1 and Group2')),
               y = VALUE, color = TEST)) +
  geom_jitter(aes(fill = TEST), shape = 21, stroke = 0,
              position = position_jitterdodge(jitter.width = 0.2)) +
  geom_linerange(data = df2, aes(ymin=VALUE-sd, ymax=VALUE+sd),
                position=position_dodge(width = .75)) +
  geom_point(data = df2, aes(y = VALUE), size = 3,
             position = position_dodge(width = 0.75)) +
  scale_color_jco() +
  scale_fill_npg() +
  labs(x = '', y = 'Values\n') +
  theme_classic(base_size = 14) +
  theme(legend.title = element_blank(),
        legend.position = "top")

p2 <- ggerrorplot(df, x = "GROUP", y = "VALUE",
                  desc_stat = "mean_sd",
                  add = c("jitter"),
                  color = "TEST", 
                  palette = "jco",
                  add.params = list(size = 0.2),
                  order = c('Group1','Group2','Group1 and Group2')
) +
  labs(x = '', y = 'Values\n') +
  theme(legend.title = element_blank())

cowplot::plot_grid(p1, p2, nrow = 1, ncol = 2, labels = "AUTO")

example_2.png

Related