您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)如何用R語(yǔ)言畫(huà)堆積柱形圖以及時(shí)間格式數(shù)據(jù)做坐標(biāo)軸的操作,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
今天的推文內(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)注億速云行業(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)容。