在Facebook研究中,我发现这些漂亮的条形图通过线条连接来表示等级变化:

https://research.fb.com/do-jobs-run-in-families/
我想用ggplot2创建它们.条形图部分很简单:
library(ggplot2)
library(ggpubr)
state1 <- data.frame(state=c(rep("ALABAMA",3), rep("CALIFORNIA",3)),
value=c(61,94,27,10,30,77),
type=rep(c("state","local","fed"),2),
cumSum=c(rep(182,3), rep(117,3)))
state2 <- data.frame(state=c(rep("ALABAMA",3), rep("CALIFORNIA",3)),
value=c(10,30,7,61,94,27),
type=rep(c("state","local","fed"),2),
cumSum=c(rep(117,3), rep(182,3)))
fill <- c("#40b8d0", "#b2d183", "#F9756D")
p1 <- ggplot(data = state1) +
geom_bar(aes(x = reorder(state, value), y = value, fill = type), stat="identity") +
theme_bw() +
scale_fill_manual(values=fill) +
labs(x="", y="Total budget in 1M$") +
theme(legend.position="none",
legend.direction="horizontal",
legend.title = element_blank(),
axis.line = element_line(size=1, colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(), panel.background = element_blank()) +
coord_flip()
p2 …Run Code Online (Sandbox Code Playgroud) 我想使用Sankey流程图可视化美国各州的等级变化(即相对顺序的变化)。我正在使用networkd3软件包,并提出了以下内容:
library(dplyr)
library(networkD3)
df <- data_frame(origins=state.name[1:10], destinations=state.name[1:10])
lab <- c(df$origins, df$destinations)
nodes <- data.frame(node=c(0:9), name=lab)
links <- data.frame(source=c(0:9), target=c(10:19), value=rep(1,10))
sankeyNetwork(Links = links, Nodes = nodes, Source = 'source',
Target = 'target', Value = 'value', NodeID = 'name')
Run Code Online (Sandbox Code Playgroud)
我现在可以手动更改相对顺序。但是,我想知道是否有可能将订单固定在右侧并将阿拉巴马州置于第3级,加利福尼亚州置于第1级,等等...