本次筆記內(nèi)容:
fill =的位置:ggplot(aes(fill = )) & geom_boxplot(fill = ) & geom_boxplot(aes(fill = ))- jitter(回避) 和 dodge(抖動)
scale_fill_manual()的設(shè)置
fill的位置
在ggplot()主體,或者geom_XX()中設(shè)置fill/color這樣的屬性參數(shù),需要注意在ggplot(aes(color=..., fill = ...))中為對整體圖像的屬性設(shè)置,在geom_XXX會繼承所有在ggplot()主體中的設(shè)置。
boxplot可以在ggplot(aes(fill = ...))中設(shè)置箱子填充情況,也可以在geom_boxplot(aes(fill = ...))中設(shè)置。如下兩個代碼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 = )指定按照什么分組來區(qū)分顏色。
下面兩個圖是一樣的output,fill不能重復(fù)指定。注意第二塊代碼,雖然在aes(fill=)里設(shè)置了按supp變量來設(shè)置顏色,但是因?yàn)間eom_boxplot(fill = )設(shè)置,不能按supp變量區(qū)分顏色。(...這是為啥??)
第三個會報(bào)錯。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也可以)中設(shè)置的,用于調(diào)節(jié)box之間的水平距離。注意如果不是如下所示這樣的分組,比方說每組只對應(yīng)一個box,那position的參數(shù)會被忽略。
# 一下使用position_dodge(), 在geom_boxplot中調(diào)節(jié)box的水平位置
ggplot(data = ToothGrowth,aes(x = dose, y = len)) +
geom_boxplot(aes(fill = supp), position=position_dodge(1))
# 以下使用了position_jitter(), 只調(diào)節(jié)點(diǎn)的抖動程度
ggplot(data = ToothGrowth,aes(x = dose, y = len)) +
+ geom_boxplot(position=position_dodge(1)) +
+ geom_point(position = position_jitter(0.1))
# 以下使用position_jitterdodge(), 在已經(jīng)有dodge屬性的boxplot中使用,
ggplot(data = ToothGrowth,aes(x = dose, y = len, fill = supp)) +
geom_boxplot() +
geom_point(position = position_jitterdodge())
# 如果沒有dodge屬性,如下所示,去掉主體中的fill,則報(bào)錯,提示需要至少一個dodge的依據(jù)
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調(diào)節(jié)抖動的幅度



另外position_jitterdodge()的參數(shù)可以設(shè)置,如jitter.width, jitter.height, dodge.width
scale_fill_manual()的設(shè)置
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 = ,設(shè)置哪個變量需要使用不同顏色區(qū)分。在
ggplot(aes(fill = ))主體部分使用似乎不錯,不容易弄混。在geom_boxplot()里只用設(shè)置箱子的屬性就好,注意不要重復(fù)賦值。 - 畫比較復(fù)雜的圖時,要小心分面可能對圖各種屬性的影響。
- 顏色最好也用scale_系列單獨(dú)設(shè)置
- ...寫完自己都覺得很傻,ggplot2雖然細(xì)節(jié)都可以照顧到,但是太折騰了...恨不得用AI手工調(diào)整=_=這么麻煩的話去看看ggpubr和python作圖算了...
參考:
https://ggplot2.tidyverse.org/reference/position_dodge.html
https://ggplot2.tidyverse.org/reference/position_jitterdodge.html