`ggplot`的散点图,用于两个分类变量,一个分类变量按颜色区分。

huangapple go评论58阅读模式
英文:

ggplot scatterplot for 2 categorical variables, 1 categorical variable by color

问题

我喜欢使用 ggboxplot 轻松将数据分成不同系列的能力。x轴标签保持易于阅读,同时通过相邻的彩色系列显示第二个分类变量。

p <- ggboxplot(df_dummy, x="Trt_Amend", y="Carbon_percent", color="Trt_CC",
               palette=c("red", "blue"),
               main="Great Plot Title",
               xlab="1st Categorical Variable",
               ylab="Continuous Variable") +
  theme(plot.title = element_text(hjust = 0.5)) + # 居中图标题.
  grids(linetype="dashed") +
  border("black")
ggpar(p, x.text.angle=45,
      legend.title="2nd Categorical Variable",
      font.main=14,
      ylim=c(0.6, 1.6))

使用箱线图并不总是合适,特别是当每个组的观察次数较少 (< 20) 时。有人可以帮助我使用 geom_pointggplot 中实现这个吗?

# 如何使用 geom_point 分离彩色系列?
ggplot(df_dummy, aes(Trt_Amend, Carbon_percent, color=Trt_CC)) +
  geom_point()

感谢阅读!

英文:

I like the ability to easily separate data into different series using ggboxplot. The x-axis labels can remain easy to read while a 2nd categorical variable is shown via adjacent colored series.

p &lt;- ggboxplot(df_dummy, x=&quot;Trt_Amend&quot;, y=&quot;Carbon_percent&quot;, color=&quot;Trt_CC&quot;,
               palette=c(&quot;red&quot;, &quot;blue&quot;),
               main=&quot;Great Plot Title&quot;,
               xlab=&quot;1st Categorical Variable&quot;,
               ylab=&quot;Continuous Variable&quot;) +
  theme(plot.title = element_text(hjust = 0.5)) + # Center plot title.
  grids(linetype=&quot;dashed&quot;) +
  border(&quot;black&quot;)
ggpar(p, x.text.angle=45,
      legend.title=&quot;2nd Categorical Variable&quot;,
      font.main=14,
      ylim=c(0.6, 1.6))

`ggplot`的散点图,用于两个分类变量,一个分类变量按颜色区分。

Using boxplots isn't always appropriate though, like when each group has a low number of observations (< 20). Can someone help me figure out how to do this in a ggplot using geom_point?

# How to separate colored series using geom_point?
ggplot(df_dummy, aes(Trt_Amend, Carbon_percent, color=Trt_CC)) +
  geom_point()

`ggplot`的散点图,用于两个分类变量,一个分类变量按颜色区分。

Thanks for reading!

答案1

得分: 1

以下是您要翻译的代码部分:

set.seed(123)

df_dummy &lt;- data.frame(
  Trt_Amend = paste0(&quot;Group&quot;, 1:5),
  Trt_CC = rep(factor(0:1), each = 5),
  Carbon_percent = rnorm(80, mean = 1, sd = .1)
)

library(ggplot2)

ggplot(df_dummy, aes(Trt_Amend, Carbon_percent, color = Trt_CC)) +
  geom_boxplot(width = .6, outlier.shape = NA) +
  geom_point(
    position = position_jitterdodge(jitter.width = .3)
  ) +
  scale_color_manual(values = c(&quot;red&quot;, &quot;blue&quot;)) +
  labs(
    title = &quot;Great Plot Title&quot;,
    x = &quot;1st Categorical Variable&quot;,
    y = &quot;Continuous Variable&quot;,
    color = &quot;2nd Categorical Variable&quot;
  ) +
  ylim(0.6, 1.6) + 
  theme_bw(base_size = 14) +
  theme(
    plot.title = element_text(hjust = 0.5),
    panel.grid = element_line(linetype = &quot;dashed&quot;),
    axis.text.x = element_text(angle = 45, hjust = 1),
    legend.position = &quot;top&quot;
  )

EDIT You could use two stat_summary layers to add errorbars and the mean:

ggplot(df_dummy, aes(Trt_Amend, Carbon_percent, color = Trt_CC)) +
  geom_point(
    position = position_jitterdodge(jitter.width = .3)
  ) +
  stat_summary(
    fun.data = &quot;mean_sdl&quot;, fun.args = list(mult = 1), position = position_dodge(width = .75),
    geom = &quot;errorbar&quot;,
    width = .3
  ) +
  stat_summary(
    fun = &quot;mean&quot;, position = position_dodge(width = .75),
    geom = &quot;point&quot;, size = 4
  ) + 
  scale_color_manual(values = c(&quot;red&quot;, &quot;blue&quot;)) +
  labs(
    title = &quot;Great Plot Title&quot;,
    x = &quot;1st Categorical Variable&quot;,
    y = &quot;Continuous Variable&quot;,
    color = &quot;2nd Categorical Variable&quot;
  ) +
  ylim(0.6, 1.6) + 
  theme_bw(base_size = 14) +
  theme(
    plot.title = element_text(hjust = 0.5),
    panel.grid = element_line(linetype = &quot;dashed&quot;),
    axis.text.x = element_text(angle = 45, hjust = 1),
    legend.position = &quot;top&quot;
  )

请注意,我已经移除了 HTML 实体编码以及图像链接,因为这些部分不需要翻译。

英文:

The first step would be to dodge your points using position = position_dodge(.75) or to add some jitter using position_jitterdodge() as I do below. The rest of the code is - similar to ggpubr:: ggboxplot - just styling.

Using some fake random example data:

set.seed(123)

df_dummy &lt;- data.frame(
  Trt_Amend = paste0(&quot;Group&quot;, 1:5),
  Trt_CC = rep(factor(0:1), each = 5),
  Carbon_percent = rnorm(80, mean = 1, sd = .1)
)

library(ggplot2)

ggplot(df_dummy, aes(Trt_Amend, Carbon_percent, color = Trt_CC)) +
  geom_boxplot(width = .6, outlier.shape = NA) +
  geom_point(
    position = position_jitterdodge(jitter.width = .3)
  ) +
  scale_color_manual(values = c(&quot;red&quot;, &quot;blue&quot;)) +
  labs(
    title = &quot;Great Plot Title&quot;,
    x = &quot;1st Categorical Variable&quot;,
    y = &quot;Continuous Variable&quot;,
    color = &quot;2nd Categorical Variable&quot;
  ) +
  ylim(0.6, 1.6) + 
  theme_bw(base_size = 14) +
  theme(
    plot.title = element_text(hjust = 0.5),
    panel.grid = element_line(linetype = &quot;dashed&quot;),
    axis.text.x = element_text(angle = 45, hjust = 1),
    legend.position = &quot;top&quot;
  )

`ggplot`的散点图,用于两个分类变量,一个分类变量按颜色区分。<!-- -->

EDIT You could use two stat_summary layers to add errorbars and the mean:

ggplot(df_dummy, aes(Trt_Amend, Carbon_percent, color = Trt_CC)) +
  geom_point(
    position = position_jitterdodge(jitter.width = .3)
  ) +
  stat_summary(
    fun.data = &quot;mean_sdl&quot;, fun.args = list(mult = 1), position = position_dodge(width = .75),
    geom = &quot;errorbar&quot;,
    width = .3
  ) +
  stat_summary(
    fun = &quot;mean&quot;, position = position_dodge(width = .75),
    geom = &quot;point&quot;, size = 4
  ) + 
  scale_color_manual(values = c(&quot;red&quot;, &quot;blue&quot;)) +
  labs(
    title = &quot;Great Plot Title&quot;,
    x = &quot;1st Categorical Variable&quot;,
    y = &quot;Continuous Variable&quot;,
    color = &quot;2nd Categorical Variable&quot;
  ) +
  ylim(0.6, 1.6) + 
  theme_bw(base_size = 14) +
  theme(
    plot.title = element_text(hjust = 0.5),
    panel.grid = element_line(linetype = &quot;dashed&quot;),
    axis.text.x = element_text(angle = 45, hjust = 1),
    legend.position = &quot;top&quot;
  )

`ggplot`的散点图,用于两个分类变量,一个分类变量按颜色区分。

huangapple
  • 本文由 发表于 2023年4月4日 12:20:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75925491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定