如何更改图表中点的颜色? ggpubr 包

Posted

技术标签:

【中文标题】如何更改图表中点的颜色? ggpubr 包【英文标题】:How to change the colors of the dots in the graph? ggpubr package 【发布时间】:2021-06-08 22:02:58 【问题描述】:

我正在使用 ggpubr 包的 ggerrorplot() 函数来创建下图。我的问题是是否有任何方法可以改变点的颜色而不改变代表平均值和标准差的点的颜色?观察图像:

我的代码:

# 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())

【问题讨论】:

【参考方案1】:

另一个潜在的解决方法 - 使用 ggplot() 和 geom_linerange() 复制绘图,例如

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")

当您将它们并排绘制时,您会发现它们并不完全相同,但这可能对您有用。

编辑

这种方法的一个优点是,如果您不希望所有点的颜色相同,但您确实希望它们与线条不同,则可以单独调整“填充”比例,例如

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")

【讨论】:

【参考方案2】:

您可以通过简单地将color 传递给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())

由 reprex package (v0.3.0) 于 2021-03-10 创建

【讨论】:

以上是关于如何更改图表中点的颜色? ggpubr 包的主要内容,如果未能解决你的问题,请参考以下文章

如何在我的 Python 的 cv2 图表上更改颜色

如何更改ColorDialog的标题?

如何更改图表的颜色。R中PerformanceAnalytics库中的相关性?

我如何创建图表和图像以显示在 R 的同一面板上

当数据表输入来自服务器的 JSON 数据时更改 Google 图表栏颜色

如何修改 sencha 图表中轴文本的字体颜色和字体大小