午夜无码人妻aⅴ大片色欲张津瑜,国产69久久久欧美黑人A片,色妺妺视频网,久久久久国产综合AV天堂

如何用R語言畫堆積柱形圖以及時間格式數(shù)據(jù)做坐標軸的操作

這期內容當中小編將會給大家?guī)碛嘘P如何用R語言畫堆積柱形圖以及時間格式數(shù)據(jù)做坐標軸的操作,文章內容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

站在用戶的角度思考問題,與客戶深入溝通,找到官渡網(wǎng)站設計與官渡網(wǎng)站推廣的解決方案,憑借多年的經驗,讓設計與互聯(lián)網(wǎng)技術結合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網(wǎng)站制作、網(wǎng)站建設、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、域名與空間、虛擬空間、企業(yè)郵箱。業(yè)務覆蓋官渡地區(qū)。

今天的推文內容我們來學習一下論文中的 Extended Data Fig. 3a ,堆積柱形圖

如何用R語言畫堆積柱形圖以及時間格式數(shù)據(jù)做坐標軸的操作  
image.png

這個圖是使用R語言的ggplot2包實現(xiàn),用到的函數(shù)是geom_bar(),數(shù)據(jù)如果是離散變量,通常只需要一列數(shù)據(jù)就可以,出圖以后柱子的高度展示的是這個變量出現(xiàn)的次數(shù),下面我們構造一份數(shù)據(jù)

df<-data.frame(axis.x=c(rep("A",3),
                        rep("B",5),
                        rep("D",4)))
df
 
如何用R語言畫堆積柱形圖以及時間格式數(shù)據(jù)做坐標軸的操作  
image.png

ggplot2畫圖

ggplot(data=df,aes(x=axis.x))+
  geom_bar()
 
如何用R語言畫堆積柱形圖以及時間格式數(shù)據(jù)做坐標軸的操作  
image.png

如果要搞成堆積柱形圖的形式,在添加一列新的變量用來填充顏色

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))
 
如何用R語言畫堆積柱形圖以及時間格式數(shù)據(jù)做坐標軸的操作  
image.png

以上是基本內容,接下來我們看一下論文中的數(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()
 
如何用R語言畫堆積柱形圖以及時間格式數(shù)據(jù)做坐標軸的操作  
image.png

這里學習到了一個新的知識點:ggplot2作圖x軸如果是時間格式的數(shù)據(jù)默認顯示的是 日加月份,這個時候如果要更改x軸的標簽需要用到scale_x_date()函數(shù)

 接下來使用R語言里的economics數(shù)據(jù)集畫一個折線圖
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))
 
如何用R語言畫堆積柱形圖以及時間格式數(shù)據(jù)做坐標軸的操作  
image.png

breaks的參數(shù)可選

  • day week month year

日期的顯示格式如何用R語言畫堆積柱形圖以及時間格式數(shù)據(jù)做坐標軸的操作

如果只想顯示年

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))
 

這里我遇到的問題是:我的月份默認顯示的是中文,如何將他改成英文呢?

還可以只選取一定的范圍

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語言畫堆積柱形圖以及時間格式數(shù)據(jù)做坐標軸的操作  
image.png

上述就是小編為大家分享的如何用R語言畫堆積柱形圖以及時間格式數(shù)據(jù)做坐標軸的操作了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)頁標題:如何用R語言畫堆積柱形圖以及時間格式數(shù)據(jù)做坐標軸的操作
路徑分享:http://www.ekvhdxd.cn/article48/podphp.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供外貿建站、微信小程序、、網(wǎng)頁設計公司、全網(wǎng)營銷推廣、網(wǎng)站排名

廣告

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

h5響應式網(wǎng)站建設