R使用笔记: ggplot2做boxplot的几个细节

本次笔记内容:

  • fill =的位置: ggplot(aes(fill = )) & geom_boxplot(fill = ) & geom_boxplot(aes(fill = ))
  • jitter(回避) 和 dodge(抖动)
  • scale_fill_manual() 的设置
fill的位置

ggplot()主体,或者geom_XX()中设置fill/color这样的属性参数,需要注意在ggplot(aes(color=..., fill = ...))中为对整体图像的属性设置,在geom_XXX会继承所有在ggplot()主体中的设置。

boxplot可以在ggplot(aes(fill = ...))中设置箱子填充情况,也可以在geom_boxplot(aes(fill = ...))中设置。如下两个代码Output图是一样的。

# 注意所有的分类变量要做成factor
ToothGrowth$dose = as.factor(ToothGrowth$dose)
ggplot(data = ToothGrowth, aes(x = dose, y = len, fill = supp)) +
  geom_boxplot() +
  scale_fill_manual(values = brewer.pal(3,"Set1")[c(1,2)])

ggplot(data = ToothGrowth,aes(x = dose, y = len)) +
  geom_boxplot(aes(fill = supp)) +
  scale_fill_manual(values = brewer.pal(3,"Set1")[c(1,2)])


geom_boxplot(fill = )的fill是用来指定具体使用什么颜色,也就是aes()外面的fill. 而aes(fill = )指定按照什么分组来区分颜色。

下面两个图是一样的output,fill不能重复指定。注意第二块代码,虽然在aes(fill=)里设置了按supp变量来设置颜色,但是因为geom_boxplot(fill = )设置,不能按supp变量区分颜色。(...这是为啥??)

第三个会报错。aes(fill = )和外面的fill不能同时使用。

ggplot(data = ToothGrowth,aes(x = dose, y = len)) +
  geom_boxplot(fill = brewer.pal(3,"Set1")) 

ggplot(data = ToothGrowth,aes(x = dose, y = len, fill = supp)) +
  geom_boxplot(fill = brewer.pal(3,"Set1")) 

ggplot(data = ToothGrowth,aes(x = dose, y = len)) +
  geom_boxplot(aes(fill = supp), fill = brewer.pal(3,"Set1")[c(1,2)])
# Error: Aesthetics must be either length 1 or the same as the data (3): fill

dodge(抖动)和jitter(回避)

dodge是在geom_boxplot()(其它比方说geom_bar也可以)中设置的,用于调节box之间的水平距离。注意如果不是如下所示这样的分组,比方说每组只对应一个box,那position的参数会被忽略。

# 一下使用position_dodge(), 在geom_boxplot中调节box的水平位置
ggplot(data = ToothGrowth,aes(x = dose, y = len)) +
  geom_boxplot(aes(fill = supp), position=position_dodge(1))

# 以下使用了position_jitter(), 只调节点的抖动程度
ggplot(data = ToothGrowth,aes(x = dose, y = len)) +
+     geom_boxplot(position=position_dodge(1)) +
+     geom_point(position = position_jitter(0.1))

# 以下使用position_jitterdodge(), 在已经有dodge属性的boxplot中使用,
ggplot(data = ToothGrowth,aes(x = dose, y = len, fill = supp)) +
  geom_boxplot() +
  geom_point(position = position_jitterdodge())

# 如果没有dodge属性,如下所示,去掉主体中的fill,则报错,提示需要至少一个dodge的依据
ggplot(data = ToothGrowth,aes(x = dose, y = len)) +
  geom_boxplot()  + 
  geom_point(position = position_jitterdodge())
# Error: `position_jitterdodge()` requires at least one aesthetic to dodge by

# 还有一种geom_jitter()
ggplot(...) +
  geom_boxplot(..) +
  geom_jitter(width=0.1)  # 用width调节抖动的幅度

另外position_jitterdodge()的参数可以设置,如jitter.width, jitter.height, dodge.width

scale_fill_manual()的设置
ggplot(data = ToothGrowth,aes(x = dose, y = len, fill = supp)) +
  geom_boxplot() +
  scale_fill_manual(values = brewer.pal(3,"Set1")[c(1,2)])

# scale_fill_maual(可以自定义fill变量的名称及其分类的名称。除了在theme()里修改legend, 也可以在这里修改)
ggplot(data = ToothGrowth,aes(x = dose, y = len, fill = supp)) +
  geom_boxplot() +
  scale_fill_manual(values = brewer.pal(3,"Set1")[c(1,2)], 
                    name = "my", 
                    labels = c("OJ" = "OJ_my", "VC" = "VC_1"))

总之:

  • 最好只在同一个地方使用fill = ,设置哪个变量需要使用不同颜色区分。在ggplot(aes(fill = ))主体部分使用似乎不错,不容易弄混。在geom_boxplot()里只用设置箱子的属性就好,注意不要重复赋值。
  • 画比较复杂的图时,要小心分面可能对图各种属性的影响。
  • 颜色最好也用scale_系列单独设置
  • ...写完自己都觉得很傻,ggplot2虽然细节都可以照顾到,但是太折腾了...恨不得用AI手工调整=_=这么麻烦的话去看看ggpubr和python作图算了...

参考:
https://ggplot2.tidyverse.org/reference/position_dodge.html
https://ggplot2.tidyverse.org/reference/position_jitterdodge.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容