您好,登錄后才能下訂單哦!
如何用ggplot2實(shí)現(xiàn)一幅叫不上來(lái)名字的圖,相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
在論文里看到了一張圖如下:
最近可能會(huì)用到,就琢磨了一下如何實(shí)現(xiàn)。不知道這種圖叫什么名字,沒(méi)辦法搜索。但是感覺(jué)R語(yǔ)言里應(yīng)該有現(xiàn)成的包來(lái)做這幅圖。這幅圖和ggplot2做的熱圖有點(diǎn)像。試著用ggplot2來(lái)實(shí)現(xiàn)這張圖。通常用ggplot2做熱圖會(huì)用geom_tile()函數(shù)
參考 https://www.r-bloggers.com/how-to-make-a-simple-heatmap-in-ggplot2/構(gòu)造數(shù)據(jù)集
df<-expand.grid(teams=paste0("Team",LETTERS[1:4]),
metrics=paste0("Metric",1:4))
df$performance<-rnorm(nrow(df))
head(df)
library(ggplot2)
ggplot(data=df,aes(x=teams,y=metrics))+
geom_tile(aes(fill=performance))+
theme_bw()+
scale_fill_continuous(low="red",high="blue")
這里遇到的問(wèn)題是:如何實(shí)現(xiàn)Metric4,3,2,1添加不同的顏色,比如Metric4是紅藍(lán)漸變色,Metric3我想填充黃綠漸變色。
想到一個(gè)解決辦法是將Metric4,3,2,1 分成四份數(shù)據(jù)集,分別使用geom_tile()函數(shù)作圖,然后在將圖拼接起來(lái)。
df1<-data.frame(A=paste("var",1:100),
B=rep("TeamA",100),
D=sample(c("Yes","No"),100,replace = T))
head(df1)
p1<-ggplot(df1,aes(x=A,y=B))+
geom_tile(aes(fill=D))+
scale_y_discrete(expand = c(0,0))+
labs(x="",y="")+theme_bw()+
scale_fill_manual(values = c("purple","grey"))
p1
接下來(lái)調(diào)整圖片的一些細(xì)節(jié):去掉x軸的文字標(biāo)簽;去掉x軸和y軸的小短線;去掉邊框
p1<-ggplot(df1,aes(x=A,y=B))+
geom_tile(aes(fill=D))+
scale_y_discrete(expand = c(0,0))+
labs(x="",y="")+theme_bw()+
scale_fill_manual(values = c("purple","grey"))+
theme(axis.text.x = element_blank(),
axis.ticks = element_blank(),
panel.border = element_blank())
p1
df2<-data.frame(A=paste("var",1:100),
B=rep("TeamA",100),
D=sample(c("Yes","No"),100,replace = T))
p2<-ggplot(df2,aes(x=A,y=B))+
geom_tile(aes(fill=D))+
scale_y_discrete(expand = c(0,0))+
labs(x="",y="")+theme_bw()+
scale_fill_manual(values = c("red","grey"))+
theme(axis.text.x = element_blank(),
axis.ticks = element_blank(),
panel.border = element_blank())
df3<-data.frame(A=paste("var",1:100),
B=rep("TeamA",100),
D=sample(c("Yes","No"),100,replace = T))
p3<-ggplot(df3,aes(x=A,y=B))+
geom_tile(aes(fill=D))+
scale_y_discrete(expand = c(0,0))+
labs(x="",y="")+theme_bw()+
scale_fill_manual(values = c("green","grey"))+
theme(axis.text.x = element_blank(),
axis.ticks = element_blank(),
panel.border = element_blank())
找到了 https://github.com/wilkelab/cowplot/issues/31可以使用 plot.margin = unit(c(0,0,0,0),'cm')
加到主題theme()
設(shè)置里
p1.1<-p1+theme(plot.margin = unit(c(0,0,0,0),'cm'))
p2.1<-p2+theme(plot.margin = unit(c(0,0,0,0),'cm'))
p3.1<-p3+theme(plot.margin = unit(c(0,0,0,0),'cm'))
plot_grid(p1.1,p2.1,p3.1,ncol = 1)
變化不是太大??纯?https://github.com/wilkelab/cowplot/issues/31這篇文章發(fā)現(xiàn)theme(plot.margin = unit(c(0,0,0,0),'cm'))
里面的數(shù)值可以設(shè)置為負(fù)數(shù)
p1.2<-p1+theme(plot.margin = unit(c(-0.5,-0.5,-0.5,-0.5),'cm'))
p2.2<-p2+theme(plot.margin = unit(c(-0.5,-0.5,-0.5,-0.5),'cm'))
p3.2<-p3+theme(plot.margin = unit(c(-0.5,-0.5,-0.5,-0.5),'cm'))
plot_grid(p1.2,p2.2,p3.2,ncol = 1)
這樣三幅圖就挨到一起去了。每個(gè)單獨(dú)的小圖有些高,可以輸出圖片時(shí)壓縮整體的高
p1.2<-p1+theme(plot.margin = unit(c(0,-0.3,-0.3,-0.3),'cm'))
p2.2<-p2+theme(plot.margin = unit(c(-0.3,-0.3,-0.3,-0.3),'cm'))
p3.2<-p3+theme(plot.margin = unit(c(-0.3,-0.3,-0.3,-0.3),'cm'))
png("Rplot18.png",height = 120)
plot_grid(p1.2,p2.2,p3.2,ncol = 1)
dev.off()
圖例有些被蓋住和,可以改變圖例的大小
p1.3<-p1+theme(plot.margin = unit(c(0,-0.3,-0.3,-0.3),'cm'),
legend.key.size = unit(0.2,'cm'),
legend.title=element_text(size=3),
legend.text=element_text(size=2))
p2.3<-p2+theme(plot.margin = unit(c(-0.3,-0.3,-0.3,-0.3),'cm'),
legend.key.size = unit(0.2,'cm'),
legend.title=element_text(size=3),
legend.text=element_text(size=2))
p3.3<-p3+theme(plot.margin = unit(c(-0.3,-0.3,-0.3,-0.3),'cm'),
legend.key.size = unit(0.2,'cm'),
legend.title=element_text(size=3),
legend.text=element_text(size=2))
png("Rplot19.png",height = 120)
plot_grid(p1.3,p2.3,p3.3,ncol = 1)
dev.off()
#修改圖例大小還可以用
legend.key.height = unit(0.2,'cm'),
legend.key.width = unit(0.2,'cm')
#還有一個(gè)小知識(shí)點(diǎn)是:plot.margin = unit(c(-0.3,-0.3,-0.3,-0.3)四個(gè)數(shù)字控制的位置分別是上右下左
看完上述內(nèi)容,你們掌握如何用ggplot2實(shí)現(xiàn)一幅叫不上來(lái)名字的圖的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。