如何用ggplot2實(shí)現(xiàn)一幅叫不上來名字的圖

如何用ggplot2實(shí)現(xiàn)一幅叫不上來名字的圖,相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

成都創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站建設(shè)、做網(wǎng)站、通化縣網(wǎng)絡(luò)推廣、微信平臺(tái)小程序開發(fā)、通化縣網(wǎng)絡(luò)營銷、通化縣企業(yè)策劃、通化縣品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);成都創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供通化縣建站搭建服務(wù),24小時(shí)服務(wù)熱線:13518219792,官方網(wǎng)址:bm7419.com

在論文里看到了一張圖如下:如何用ggplot2實(shí)現(xiàn)一幅叫不上來名字的圖

最近可能會(huì)用到,就琢磨了一下如何實(shí)現(xiàn)。不知道這種圖叫什么名字,沒辦法搜索。但是感覺R語言里應(yīng)該有現(xiàn)成的包來做這幅圖。這幅圖和ggplot2做的熱圖有點(diǎn)像。試著用ggplot2來實(shí)現(xiàn)這張圖。通常用ggplot2做熱圖會(huì)用geom_tile()函數(shù)

 首先是geom_tile()函數(shù)的一個(gè)例子

參考 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")
 
如何用ggplot2實(shí)現(xiàn)一幅叫不上來名字的圖  
image.png

這里遇到的問題是:如何實(shí)現(xiàn)Metric4,3,2,1添加不同的顏色,比如Metric4是紅藍(lán)漸變色,Metric3我想填充黃綠漸變色。

想到一個(gè)解決辦法是將Metric4,3,2,1 分成四份數(shù)據(jù)集,分別使用geom_tile()函數(shù)作圖,然后在將圖拼接起來。

 實(shí)現(xiàn)上面提到的想法
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
 

如何用ggplot2實(shí)現(xiàn)一幅叫不上來名字的圖接下來調(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
 
如何用ggplot2實(shí)現(xiàn)一幅叫不上來名字的圖  
image.png
 
接下來同樣的思路再做2幅,然后使用cowplot包的plot_grid()函數(shù)將圖片拼起來
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())
 
如何用ggplot2實(shí)現(xiàn)一幅叫不上來名字的圖  
image.png
 接下來發(fā)現(xiàn)一個(gè)問題:圖片之間的空白部分有一點(diǎn)大,如何調(diào)整讓他們緊挨著呢?

找到了 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)
 
如何用ggplot2實(shí)現(xiàn)一幅叫不上來名字的圖  
image.png

變化不是太大??纯?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)
 
如何用ggplot2實(shí)現(xiàn)一幅叫不上來名字的圖  
image.png

這樣三幅圖就挨到一起去了。每個(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()
 

如何用ggplot2實(shí)現(xiàn)一幅叫不上來名字的圖圖例有些被蓋住和,可以改變圖例的大小

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ù)字控制的位置分別是上右下左
 
如何用ggplot2實(shí)現(xiàn)一幅叫不上來名字的圖  
image.png

看完上述內(nèi)容,你們掌握如何用ggplot2實(shí)現(xiàn)一幅叫不上來名字的圖的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

當(dāng)前名稱:如何用ggplot2實(shí)現(xiàn)一幅叫不上來名字的圖
網(wǎng)頁鏈接:http://bm7419.com/article38/pssjsp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、虛擬主機(jī)外貿(mào)建站、營銷型網(wǎng)站建設(shè)、企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)