這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)如何用R語(yǔ)言畫(huà)堆積柱形圖以及時(shí)間格式數(shù)據(jù)做坐標(biāo)軸的操作,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到官渡網(wǎng)站設(shè)計(jì)與官渡網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類(lèi)型包括:網(wǎng)站制作、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名與空間、虛擬空間、企業(yè)郵箱。業(yè)務(wù)覆蓋官渡地區(qū)。
今天的推文內(nèi)容我們來(lái)學(xué)習(xí)一下論文中的 Extended Data Fig. 3a ,堆積柱形圖
這個(gè)圖是使用R語(yǔ)言的ggplot2包實(shí)現(xiàn),用到的函數(shù)是geom_bar(),數(shù)據(jù)如果是離散變量,通常只需要一列數(shù)據(jù)就可以,出圖以后柱子的高度展示的是這個(gè)變量出現(xiàn)的次數(shù),下面我們構(gòu)造一份數(shù)據(jù)
df<-data.frame(axis.x=c(rep("A",3),
rep("B",5),
rep("D",4)))
df
ggplot2畫(huà)圖
ggplot(data=df,aes(x=axis.x))+
geom_bar()
如果要搞成堆積柱形圖的形式,在添加一列新的變量用來(lái)填充顏色
df<-data.frame(axis.x=c(rep("A",3),
rep("B",5),
rep("D",4)),
axis.y=c(sample(c("apple","orange","banana"),
12,replace=T)))
df
library(ggplot2)
ggplot(data=df,aes(x=axis.x))+
geom_bar(aes(fill=axis.y))
以上是基本內(nèi)容,接下來(lái)我們看一下論文中的數(shù)據(jù)和代碼
bar_data <- readr::read_csv("Single_Cell/covid-19-sse-master/data/bar_data.csv")
bar_data
ggplot(data=bar_data) +
geom_bar(aes(x = epi.date, fill = cluster.generation), width = 0.9) +
scale_x_date(name = "Onset Date",
date_breaks = "2 days",
date_labels = "%d %b",
minor_breaks = NULL) +
scale_y_continuous("Case Count", expand = c(0,0), breaks = seq(0,16, by = 2), limits = c(0,16)) +
theme_classic() +
theme(#aspect.ratio = 0.3,
legend.position = 'none',
axis.text.x = element_text(angle = 45, hjust = 1 )) +
scale_fill_viridis_d()
這里學(xué)習(xí)到了一個(gè)新的知識(shí)點(diǎn):ggplot2作圖x軸如果是時(shí)間格式的數(shù)據(jù)默認(rèn)顯示的是 日加月份,這個(gè)時(shí)候如果要更改x軸的標(biāo)簽需要用到
scale_x_date()
函數(shù)
ggplot(data = economics, aes(x = date, y = psavert)) +
geom_line(color = "steelblue")+
theme_bw()+
scale_x_date(breaks = '1 year')+
theme(axis.text.x = element_text(hjust=1,vjust=0.5,angle=90))
breaks的參數(shù)可選
日期的顯示格式
如果只想顯示年
ggplot(data = economics, aes(x = date, y = psavert)) +
geom_line(color = "steelblue")+
theme_bw()+
scale_x_date(breaks = '1 year',
date_labels = "%Y")+
theme(axis.text.x = element_text(hjust=1,vjust=0.5,angle=90))
還可以更改年月日之間的分隔符
ggplot(data = economics, aes(x = date, y = psavert)) +
geom_line(color = "steelblue")+
theme_bw()+
scale_x_date(breaks = '1 year',
date_labels = "%Y,%B,%d")+
theme(axis.text.x = element_text(hjust=1,vjust=0.5,angle=90))
這里我遇到的問(wèn)題是:我的月份默認(rèn)顯示的是中文,如何將他改成英文呢?
還可以只選取一定的范圍
min <- as.Date("2002-1-1")
max <- NA
ggplot(data = economics, aes(x = date, y = psavert)) +
geom_line(color = "steelblue")+
theme_bw()+
scale_x_date(breaks = '1 year',
date_labels = "%Y,%B,%d",
limits = c(min,max))+
theme(axis.text.x = element_text(hjust=1,vjust=0.5,angle=90))
上述就是小編為大家分享的如何用R語(yǔ)言畫(huà)堆積柱形圖以及時(shí)間格式數(shù)據(jù)做坐標(biāo)軸的操作了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁(yè)標(biāo)題:如何用R語(yǔ)言畫(huà)堆積柱形圖以及時(shí)間格式數(shù)據(jù)做坐標(biāo)軸的操作
路徑分享:http://jinyejixie.com/article48/podphp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、微信小程序、、網(wǎng)頁(yè)設(shè)計(jì)公司、全網(wǎng)營(yíng)銷(xiāo)推廣、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)