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

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

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

问题

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

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

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

  1. # 如何使用 geom_point 分离彩色系列?
  2. ggplot(df_dummy, aes(Trt_Amend, Carbon_percent, color=Trt_CC)) +
  3. 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.

  1. p &lt;- ggboxplot(df_dummy, x=&quot;Trt_Amend&quot;, y=&quot;Carbon_percent&quot;, color=&quot;Trt_CC&quot;,
  2. palette=c(&quot;red&quot;, &quot;blue&quot;),
  3. main=&quot;Great Plot Title&quot;,
  4. xlab=&quot;1st Categorical Variable&quot;,
  5. ylab=&quot;Continuous Variable&quot;) +
  6. theme(plot.title = element_text(hjust = 0.5)) + # Center plot title.
  7. grids(linetype=&quot;dashed&quot;) +
  8. border(&quot;black&quot;)
  9. ggpar(p, x.text.angle=45,
  10. legend.title=&quot;2nd Categorical Variable&quot;,
  11. font.main=14,
  12. 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?

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

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

Thanks for reading!

答案1

得分: 1

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

  1. set.seed(123)
  2. df_dummy &lt;- data.frame(
  3. Trt_Amend = paste0(&quot;Group&quot;, 1:5),
  4. Trt_CC = rep(factor(0:1), each = 5),
  5. Carbon_percent = rnorm(80, mean = 1, sd = .1)
  6. )
  7. library(ggplot2)
  8. ggplot(df_dummy, aes(Trt_Amend, Carbon_percent, color = Trt_CC)) +
  9. geom_boxplot(width = .6, outlier.shape = NA) +
  10. geom_point(
  11. position = position_jitterdodge(jitter.width = .3)
  12. ) +
  13. scale_color_manual(values = c(&quot;red&quot;, &quot;blue&quot;)) +
  14. labs(
  15. title = &quot;Great Plot Title&quot;,
  16. x = &quot;1st Categorical Variable&quot;,
  17. y = &quot;Continuous Variable&quot;,
  18. color = &quot;2nd Categorical Variable&quot;
  19. ) +
  20. ylim(0.6, 1.6) +
  21. theme_bw(base_size = 14) +
  22. theme(
  23. plot.title = element_text(hjust = 0.5),
  24. panel.grid = element_line(linetype = &quot;dashed&quot;),
  25. axis.text.x = element_text(angle = 45, hjust = 1),
  26. legend.position = &quot;top&quot;
  27. )

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

  1. ggplot(df_dummy, aes(Trt_Amend, Carbon_percent, color = Trt_CC)) +
  2. geom_point(
  3. position = position_jitterdodge(jitter.width = .3)
  4. ) +
  5. stat_summary(
  6. fun.data = &quot;mean_sdl&quot;, fun.args = list(mult = 1), position = position_dodge(width = .75),
  7. geom = &quot;errorbar&quot;,
  8. width = .3
  9. ) +
  10. stat_summary(
  11. fun = &quot;mean&quot;, position = position_dodge(width = .75),
  12. geom = &quot;point&quot;, size = 4
  13. ) +
  14. scale_color_manual(values = c(&quot;red&quot;, &quot;blue&quot;)) +
  15. labs(
  16. title = &quot;Great Plot Title&quot;,
  17. x = &quot;1st Categorical Variable&quot;,
  18. y = &quot;Continuous Variable&quot;,
  19. color = &quot;2nd Categorical Variable&quot;
  20. ) +
  21. ylim(0.6, 1.6) +
  22. theme_bw(base_size = 14) +
  23. theme(
  24. plot.title = element_text(hjust = 0.5),
  25. panel.grid = element_line(linetype = &quot;dashed&quot;),
  26. axis.text.x = element_text(angle = 45, hjust = 1),
  27. legend.position = &quot;top&quot;
  28. )

请注意,我已经移除了 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:

  1. set.seed(123)
  2. df_dummy &lt;- data.frame(
  3. Trt_Amend = paste0(&quot;Group&quot;, 1:5),
  4. Trt_CC = rep(factor(0:1), each = 5),
  5. Carbon_percent = rnorm(80, mean = 1, sd = .1)
  6. )
  7. library(ggplot2)
  8. ggplot(df_dummy, aes(Trt_Amend, Carbon_percent, color = Trt_CC)) +
  9. geom_boxplot(width = .6, outlier.shape = NA) +
  10. geom_point(
  11. position = position_jitterdodge(jitter.width = .3)
  12. ) +
  13. scale_color_manual(values = c(&quot;red&quot;, &quot;blue&quot;)) +
  14. labs(
  15. title = &quot;Great Plot Title&quot;,
  16. x = &quot;1st Categorical Variable&quot;,
  17. y = &quot;Continuous Variable&quot;,
  18. color = &quot;2nd Categorical Variable&quot;
  19. ) +
  20. ylim(0.6, 1.6) +
  21. theme_bw(base_size = 14) +
  22. theme(
  23. plot.title = element_text(hjust = 0.5),
  24. panel.grid = element_line(linetype = &quot;dashed&quot;),
  25. axis.text.x = element_text(angle = 45, hjust = 1),
  26. legend.position = &quot;top&quot;
  27. )

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

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

  1. ggplot(df_dummy, aes(Trt_Amend, Carbon_percent, color = Trt_CC)) +
  2. geom_point(
  3. position = position_jitterdodge(jitter.width = .3)
  4. ) +
  5. stat_summary(
  6. fun.data = &quot;mean_sdl&quot;, fun.args = list(mult = 1), position = position_dodge(width = .75),
  7. geom = &quot;errorbar&quot;,
  8. width = .3
  9. ) +
  10. stat_summary(
  11. fun = &quot;mean&quot;, position = position_dodge(width = .75),
  12. geom = &quot;point&quot;, size = 4
  13. ) +
  14. scale_color_manual(values = c(&quot;red&quot;, &quot;blue&quot;)) +
  15. labs(
  16. title = &quot;Great Plot Title&quot;,
  17. x = &quot;1st Categorical Variable&quot;,
  18. y = &quot;Continuous Variable&quot;,
  19. color = &quot;2nd Categorical Variable&quot;
  20. ) +
  21. ylim(0.6, 1.6) +
  22. theme_bw(base_size = 14) +
  23. theme(
  24. plot.title = element_text(hjust = 0.5),
  25. panel.grid = element_line(linetype = &quot;dashed&quot;),
  26. axis.text.x = element_text(angle = 45, hjust = 1),
  27. legend.position = &quot;top&quot;
  28. )

`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:

确定