ggplot2繪制流圖
使用的是ggstream中的blockbusters數據集。
library(ggstream)
blockbusters
該包的geom_stream功能ggstrean允許 在 ggplot2 中創(chuàng)建流圖。
library(ggstream)
#?install.packages("ggplot2")
library(ggplot2)
ggplot(blockbusters,?aes(x?=?year,?y?=?box_office,?fill?=?genre))?+
??geom_stream()

該包包含一個名為的附加函數geom_stream_label,可用于將標簽添加到流圖的每個區(qū)域。
ggplot(blockbusters,?aes(x?=?year,?y?=?box_office,?fill?=?genre))?+
??geom_stream()?+
??geom_stream_label(aes(label?=?genre))

默認情況下,該geom_stream函數使用"mirror"類型,它圍繞 X 軸對稱地堆疊流。其他替代方案是"ridge",它從 X 軸堆疊,并且"proportional",它使流總和為 1。
ggplot(blockbusters,?aes(x?=?year,?y?=?box_office,?fill?=?genre))?+
??geom_stream(type?=?"ridge")

scale_fill_manual可以使用或等效函數更改流圖的填充顏色。
cols?<-?c("#FFB400",?"#FFC740",?"#C20008",?"#FF020D",?"#13AFEF")
ggplot(blockbusters,?aes(x?=?year,?y?=?box_office,?fill?=?genre))?+
??geom_stream()?+
??scale_fill_manual(values?=?cols)

默認情況下,這些區(qū)域沒有邊框,但可以使用函數的color參數添加邊框顏色geom_stream并使用 修改其寬度lwd。
cols?<-?c("#FFB400",?"#FFC740",?"#C20008",?"#FF020D",?"#13AFEF")
ggplot(blockbusters,?aes(x?=?year,?y?=?box_office,?fill?=?genre))?+
??geom_stream(color?=?1,?lwd?=?0.25)?+
??scale_fill_manual(values?=?cols)

請注意,還可以更改主題以修改圖表的外觀。
cols?<-?c("#FFB400",?"#FFC740",?"#C20008",?"#FF020D",?"#13AFEF")
ggplot(blockbusters,?aes(x?=?year,?y?=?box_office,?fill?=?genre))?+
??geom_stream(color?=?1,?lwd?=?0.25)?+
??scale_fill_manual(values?=?cols)?+
??theme_minimal()

評論
圖片
表情
