ggplot2畫(huà)箱線圖默認(rèn)情況下所有的線都是實(shí)線,如下
ggplot(HMP,aes(x=country,y=log10(rel_crAss)))+
geom_boxplot()

image.png
可以通過(guò)加參數(shù)linetype更改線的類(lèi)型,比如改成虛線
ggplot(HMP,aes(x=country,y=log10(rel_crAss)))+
geom_boxplot(linetype="dashed")

image.png
但是現(xiàn)在遇到一個(gè)問(wèn)題是所有的線都變成虛線了,我想要矩形的邊框變成實(shí)線,那該如何實(shí)現(xiàn)呢?經(jīng)過(guò)搜索找到了一個(gè)解決辦法
鏈接是 https://stackoverflow.com/questions/53170465/how-to-make-a-base-r-style-boxplot-using-ggplot2
原來(lái)有一個(gè)函數(shù)stat_boxplot()可以只畫(huà)箱線圖的矩形,實(shí)現(xiàn)代碼是
ggplot(HMP,aes(x=country,y=log10(rel_crAss)))+
stat_boxplot(aes(ymin=..lower..,ymax=..upper..))

image.png
直接把這個(gè)圖形疊加到全是虛線邊框的箱線圖上就可以了
ggplot(HMP,aes(x=country,y=log10(rel_crAss)))+
geom_boxplot(linetype="dashed")+
stat_boxplot(aes(ymin=..lower..,ymax=..upper..))

image.png
添加最大值最小值的小橫線
ggplot(HMP,aes(x=country,y=log10(rel_crAss)))+
geom_boxplot(linetype="dashed",color="blue")+
stat_boxplot(aes(ymin=..lower..,ymax=..upper..,
fill=country),
color="red")+
stat_boxplot(geom = "errorbar",aes(ymin=..ymax..),
width=0.2,color="red")+
stat_boxplot(geom = "errorbar",aes(ymax=..ymin..),
width=0.2,color="green")

image.png
這個(gè)配色也是沒(méi)誰(shuí)了,哈哈哈!
歡迎大家關(guān)注我的公眾號(hào)
小明的數(shù)據(jù)分析筆記本