溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

R語言可視化中多系列柱形圖與分面組圖美化技巧有哪些

發(fā)布時間:2021-11-22 10:37:32 來源:億速云 閱讀:213 作者:柒染 欄目:大數(shù)據(jù)

R語言可視化中多系列柱形圖與分面組圖美化技巧有哪些,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

今天跟大家分享多系列與分面組圖的美化技巧!

昨天講的關于多序列柱形圖與條形圖美化技巧,其實還漏掉了一些一點兒。

當數(shù)據(jù)序列比較多的時候,特別是超過四個以后,還用堆積柱形圖(條形圖)、或者簇狀柱形圖的話,圖表必然會因為系列太多而受到擠壓或者變形,整體就會不協(xié)調(diào)、不美觀。

還有ggplot不支持次坐標軸功能,它的作圖思維基本源于塔夫脫的可視化理念,而且作者個人的審美也接受次坐標軸(大牛任性),但是他留給大家解決多序列圖表的方案是——分面組圖~

data<-data.frame(Name = c("蘋果","谷歌","臉書","亞馬遜","騰訊"),Conpany = c("Apple","Google","Facebook","Amozon","Tencent"),Sale2013 = c(5000,3500,2300,2100,3100),Sale2014 = c(5050,3800,2900,2500,3300),Sale2015 = c(5050,3800,2900,2500,3300),Sale2016 = c(5050,3800,2900,2500,3300))

R語言可視化中多系列柱形圖與分面組圖美化技巧有哪些

數(shù)據(jù)重塑(寬轉(zhuǎn)長):

mydata<-melt(mydata,id.vars="Conpany",variable.name="Year",value.name="Sale")

R語言可視化中多系列柱形圖與分面組圖美化技巧有哪些

作圖函數(shù):

ggplot(mydata,aes(Conpany,Sale,fill=Year))+geom_bar(stat="identity",position="dodge")

R語言可視化中多系列柱形圖與分面組圖美化技巧有哪些

默認圖表的配色確實挺難看的,這里我們使用華爾街日報、經(jīng)濟學人的主題、及配色模板。

ggplot(mydata,aes(Conpany,Sale,fill=Year))+geom_bar(stat="identity",position="dodge")+theme_wsj()+scale_fill_wsj("rgby", "")+theme(axis.ticks.length=unit(0.5,'cm'))+guides(fill=guide_legend(title=NULL))+ggtitle("The Financial Performance of Five Giant")+theme(axis.title = element_blank())

R語言可視化中多系列柱形圖與分面組圖美化技巧有哪些

ggplot(mydata,aes(Conpany,Sale,fill=Year))+geom_bar(stat="identity",position="dodge")+theme_economist(base_size=14)+scale_fill_economist()+theme(axis.ticks.length=unit(0.5,'cm'))+guides(fill=guide_legend(title=NULL))+ggtitle("The Financial Performance of Five Giant")+theme(axis.title = element_blank())

R語言可視化中多系列柱形圖與分面組圖美化技巧有哪些

以上是我們使用傳統(tǒng)的方法通過將顏色映射到不同類別的年度收入變量上,達到了區(qū)分效果,可是這樣終究不是辦法,五個序列實在是有點多,已經(jīng)讓然有點兒眼花繚亂了,如果有8個序列、10個序列呢,那又該怎么辦呢~

下面跟大家將其中一種比較有效的解決辦法:通過分面組圖解決多序列圖表:

橫排分面:

柱形分面(橫排):

ggplot(mydata,aes(Conpany,Sale,fill=Year))+geom_bar(stat="identity",position="dodge")+theme_wsj()+scale_fill_wsj("rgby", "")+theme(axis.ticks.length=unit(0.5,'cm'))+guides(fill=guide_legend(title=NULL))+ggtitle("The Financial Performance of Five Giant")+theme(axis.title = element_blank(),legend.position='none')+ facet_grid(.~Year)

R語言可視化中多系列柱形圖與分面組圖美化技巧有哪些

ggplot(mydata,aes(Conpany,Sale,fill=Year))+geom_bar(stat="identity",position="dodge")+theme_economist(base_size=14)+scale_fill_economist()+theme(axis.ticks.length=unit(0.5,'cm'))+guides(fill=guide_legend(title=NULL))+ggtitle("The Financial Performance of Five Giant")+theme(axis.title = element_blank(),legend.position='none')+ facet_grid(.~Year)

R語言可視化中多系列柱形圖與分面組圖美化技巧有哪些

條形分面(橫排):

ggplot(mydata,aes(Conpany,Sale,fill=Year))+geom_bar(stat="identity",position="dodge")+theme_wsj()+scale_fill_wsj("rgby", "")+theme(axis.ticks.length=unit(0.5,'cm'))+guides(fill=guide_legend(title=NULL))+ggtitle("The Financial Performance of Five Giant")+theme(axis.title = element_blank(),legend.position='none')+ facet_grid(.~Year)+coord_flip()

R語言可視化中多系列柱形圖與分面組圖美化技巧有哪些

ggplot(mydata,aes(Conpany,Sale,fill=Year))+geom_bar(stat="identity",position="dodge")+theme_economist(base_size=14)+scale_fill_economist()+theme(axis.ticks.length=unit(0.5,'cm'))+guides(fill=guide_legend(title=NULL))+ggtitle("The Financial Performance of Five Giant")+theme(axis.title = element_blank(),legend.position='none')+ facet_grid(.~Year)+coord_flip()

R語言可視化中多系列柱形圖與分面組圖美化技巧有哪些

豎排分面:

柱形分面(豎排):

ggplot(mydata,aes(Conpany,Sale,fill=Year))+geom_bar(stat="identity",position="dodge")+theme_wsj()+scale_fill_wsj("rgby", "")+theme(axis.ticks.length=unit(0.5,'cm'))+guides(fill=guide_legend(title=NULL))+ggtitle("The Financial Performance of Five Giant")+theme(axis.title = element_blank(),legend.position='none')+ facet_grid(Year~.)

R語言可視化中多系列柱形圖與分面組圖美化技巧有哪些

ggplot(mydata,aes(Conpany,Sale,fill=Year))+geom_bar(stat="identity",position="dodge")+theme_economist(base_size=14)+scale_fill_economist()+theme(axis.ticks.length=unit(0.5,'cm'))+guides(fill=guide_legend(title=NULL))+ggtitle("The Financial Performance of Five Giant")+theme(axis.title = element_blank(),legend.position='none')+ facet_grid(Year~.)

R語言可視化中多系列柱形圖與分面組圖美化技巧有哪些

條形分面(豎排):

ggplot(mydata,aes(Conpany,Sale,fill=Year))+geom_bar(stat="identity",position="dodge")+theme_wsj()+scale_fill_wsj("rgby", "")+theme(axis.ticks.length=unit(0.5,'cm'))+guides(fill=guide_legend(title=NULL))+ggtitle("The Financial Performance of Five Giant")+theme(axis.title = element_blank(),legend.position='none')+ facet_grid(Year~.)+coord_flip()

R語言可視化中多系列柱形圖與分面組圖美化技巧有哪些

ggplot(mydata,aes(Conpany,Sale,fill=Year))+geom_bar(stat="identity",position="dodge")+theme_economist(base_size=14)+scale_fill_economist()+theme(axis.ticks.length=unit(0.5,'cm'))+guides(fill=guide_legend(title=NULL))+ggtitle("The Financial Performance of Five Giant")+theme(axis.title = element_blank(),legend.position='none')+ facet_grid(Year~.)+coord_flip()

R語言可視化中多系列柱形圖與分面組圖美化技巧有哪些

關于簇狀、分面圖表數(shù)據(jù)標簽問題:

昨天在講解的時候忘記了圖表數(shù)據(jù)標簽這回事兒,而且當時確實也不太會處理這塊兒,后來突然找到了處理方法:

簇狀圖標簽數(shù)據(jù)處理:

ggplot(mydata,aes(Conpany,Sale,fill=Year,label =Sale))+geom_bar(stat="identity",position="dodge")+theme_wsj()+scale_fill_wsj("rgby", "")+theme(axis.ticks.length=unit(0.5,'cm'))+guides(fill=guide_legend(title=NULL))+ggtitle("The Financial Performance of Five Giant")+theme(axis.title = element_blank())+geom_text(aes(y = Sale + 0.05), position = position_dodge(0.9), vjust = -0.5)

R語言可視化中多系列柱形圖與分面組圖美化技巧有哪些

橫向分面柱圖數(shù)據(jù)標簽問題:

ggplot(mydata,aes(Conpany,Sale,fill=Year,label =Sale))+geom_bar(stat="identity",position="dodge")+theme_wsj()+scale_fill_wsj("rgby", "")+theme(axis.ticks.length=unit(0.5,'cm'))+guides(fill=guide_legend(title=NULL))+ggtitle("The Financial Performance of Five Giant")+theme(axis.title = element_blank(),legend.position='none')+ facet_grid(.~Year)+geom_text(aes(y = Sale + 0.05), position = position_dodge(0.9), vjust = -0.5)

R語言可視化中多系列柱形圖與分面組圖美化技巧有哪些

橫向分面條形圖數(shù)據(jù)標簽問題:

ggplot(mydata,aes(Conpany,Sale,fill=Year,label =Sale))+geom_bar(stat="identity",position="dodge")+theme_wsj()+scale_fill_wsj("rgby", "")+theme(axis.ticks.length=unit(0.5,'cm'))+guides(fill=guide_legend(title=NULL))+ggtitle("The Financial Performance of Five Giant")+theme(axis.title = element_blank(),legend.position='none')+ facet_grid(.~Year)+geom_text(aes(y = Sale + 0.05), position = position_dodge(0.9), vjust = -0.5)+coord_flip()

R語言可視化中多系列柱形圖與分面組圖美化技巧有哪些

豎向分面柱形圖數(shù)據(jù)標簽問題:

ggplot(mydata,aes(Conpany,Sale,fill=Year,label =Sale))+geom_bar(stat="identity",position="dodge")+theme_wsj()+scale_fill_wsj("rgby", "")+theme(axis.ticks.length=unit(0.5,'cm'))+guides(fill=guide_legend(title=NULL))+ggtitle("The Financial Performance of Five Giant")+theme(axis.title = element_blank(),legend.position='none')+ facet_grid(Year~.)+geom_text(aes(y = Sale + 0.05), position = position_dodge(0.9), vjust = -0.5)

R語言可視化中多系列柱形圖與分面組圖美化技巧有哪些

豎向分面條形圖數(shù)據(jù)標簽問題:

ggplot(mydata,aes(Conpany,Sale,fill=Year,label =Sale))+geom_bar(stat="identity",position="dodge")+theme_wsj()+scale_fill_wsj("rgby", "")+theme(axis.ticks.length=unit(0.5,'cm'))+guides(fill=guide_legend(title=NULL))+ggtitle("The Financial Performance of Five Giant")+theme(axis.title = element_blank(),legend.position='none')+ facet_grid(Year~.)+geom_text(aes(y = Sale + 0.05), position = position_dodge(0.9), vjust = -0.5)+coord_flip()

R語言可視化中多系列柱形圖與分面組圖美化技巧有哪些

好了,這樣分面組圖及其標簽問題算是列舉清楚了。

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI