今天公眾號(hào)后臺(tái)有人留言作圖屏箍,示例圖如下
image.png
我選擇使用R語(yǔ)言的ggplot2來(lái)實(shí)現(xiàn),這個(gè)是箱線圖和熱圖的拼接,右側(cè)的熱圖可以借助
geom_point()
函數(shù)實(shí)現(xiàn)捶枢,將點(diǎn)的形狀改為正方塊,數(shù)值按照正負(fù)來(lái)映射顏色飞崖,按照一定的數(shù)值來(lái)映射大小烂叔。基本的思路有了固歪。今天的推文記錄一個(gè)小知識(shí)點(diǎn)是 如何將圖例遠(yuǎn)離主圖并且給放到右上角去
首先是構(gòu)造一份數(shù)據(jù)
df<-data.frame(x=LETTERS[1:4],y=1:4)
df
image.png
正常做一個(gè)柱形圖
library(ggplot2)
ggplot(data=df,aes(x=x,y=y))+
geom_col(aes(fill=x))
image.png
拉大圖例與主圖的距離
使用theme()
函數(shù)中的legend.box.margin
參數(shù)來(lái)調(diào)節(jié)
ggplot(data=df,aes(x=x,y=y))+
geom_col(aes(fill=x))+
theme(legend.box.margin = margin(0,0,0,5,unit = 'cm'))
image.png
將圖例放到右上角
通過 legend.justification
參數(shù)來(lái)實(shí)現(xiàn)
ggplot(data=df,aes(x=x,y=y))+
geom_col(aes(fill=x))+
theme(legend.box.margin = margin(0,0,0,5,unit = 'cm'),
legend.justification = c(0,1))
image.png
還有另外兩個(gè)知識(shí)點(diǎn)是調(diào)節(jié)圖例的標(biāo)題和圖例的距離以及圖例的文本和圖例的距離
分別需要用到legend.spacing.y
和legend.spacing.x
參數(shù)
ggplot(data=df,aes(x=x,y=y))+
geom_col(aes(fill=x))+
theme(legend.box.margin = margin(0,0,0,5,unit = 'cm'),
legend.justification = c(0,1),
legend.spacing.y = unit(5,'cm'),
legend.spacing.x = unit(5,'cm'))
image.png
歡迎大家關(guān)注我的公眾號(hào)
小明的數(shù)據(jù)分析筆記本
小明的數(shù)據(jù)分析筆記本 公眾號(hào) 主要分享:1蒜鸡、R語(yǔ)言和python做數(shù)據(jù)分析和數(shù)據(jù)可視化的簡(jiǎn)單小例子;2牢裳、園藝植物相關(guān)轉(zhuǎn)錄組學(xué)逢防、基因組學(xué)、群體遺傳學(xué)文獻(xiàn)閱讀筆記贰健;3胞四、生物信息學(xué)入門學(xué)習(xí)資料及自己的學(xué)習(xí)筆記!