瀑布图
Waterfall graph
示例数据
Country State City Currency Tasks Accidents
IND KR BLR INR 1000 500
IND WB CCU INR 2500 200
SL SL COL SLR 500 400
JAP JAP TOK YEN 400 300
AUS MB MB AD 200 4000
AUS SY SY AD 3000 400
AUS AD AD AD 5000 300
需要如下图,类似于瀑布模型
![![图表要求][1]][1]
Y 轴 - 国家/地区 IND SL JAP AUS
X 轴 - 任务事故
尝试了以下两个代码
ggplot(df,aes(Country,fill=Value))+
geom_rect(aes( x = Country,xmin = Current_no, xmax = Current_no , ymin = Current_no, ymax = Current_no))
示例数据
Country state City flag value
IND KA BLR . Tasks 8200
IND WB CCU Tasks 2500
IND KA BLR . Accidents 700
p <- plot_ly(
df_melt, name = "20", type = "waterfall", measure = ~value,
x = ~Flag, textposition = "outside", y= ~Country,
connector = list(line = list(color= "rgb(63, 60, 50, 40, 44, 34, 21)"))) %>%
layout(title = "Exp Output",
xaxis = list(title = ""),
yaxis = list(title = ""),
autosize = TRUE,
showlegend = TRUE)
p
没有得到预期的输出
[1]: https://i.stack.imgur.com/6Mxdo.png
不确定我是否称它为瀑布图,但试试这个尺寸:
library(dplyr)
library(tidyr)
library(ggplot2)
df2 <- df %>%
gather(stat, val, Tasks, Accidents) %>%
mutate(
stat=factor(stat, levels=c('Tasks','Accidents')),
Country = factor(Country, levels=c('IND','SL','JAP','AUS'))
)
x1 <- df2 %>% group_by(Country, stat) %>% summarise(val=sum(val))
x2 <- df2 %>% group_by(stat) %>% summarise(val=sum(val))
ggplot(x1, aes(x=stat, y=val)) +
geom_col(aes(fill=Country)) +
geom_label(data=x2, aes(label=val), nudge_y = 100) +
theme(legend.position='top')
尝试省略将 stat
和 Country
转换为因数的 mutate
命令,看看情况如何变化。
试试这个
ggplot(x1, aes(x=stat, y=val),fill=Type) +
geom_bar(aes(fill=Country),stat = "identity") +
geom_label(data=x2, aes(label=val), nudge_y = 10,label.padding = unit(0.4, "lines")) +
scale_y_continuous(labels = scales::percent_format())+
theme(legend.position='top')+
geom_label(data=x1, aes(label=val), nudge_y = 10,label.padding = unit(0.1, "lines"))
示例数据
Country State City Currency Tasks Accidents
IND KR BLR INR 1000 500
IND WB CCU INR 2500 200
SL SL COL SLR 500 400
JAP JAP TOK YEN 400 300
AUS MB MB AD 200 4000
AUS SY SY AD 3000 400
AUS AD AD AD 5000 300
需要如下图,类似于瀑布模型 ![![图表要求][1]][1] Y 轴 - 国家/地区 IND SL JAP AUS X 轴 - 任务事故
尝试了以下两个代码
ggplot(df,aes(Country,fill=Value))+
geom_rect(aes( x = Country,xmin = Current_no, xmax = Current_no , ymin = Current_no, ymax = Current_no))
示例数据
Country state City flag value
IND KA BLR . Tasks 8200
IND WB CCU Tasks 2500
IND KA BLR . Accidents 700
p <- plot_ly(
df_melt, name = "20", type = "waterfall", measure = ~value,
x = ~Flag, textposition = "outside", y= ~Country,
connector = list(line = list(color= "rgb(63, 60, 50, 40, 44, 34, 21)"))) %>%
layout(title = "Exp Output",
xaxis = list(title = ""),
yaxis = list(title = ""),
autosize = TRUE,
showlegend = TRUE)
p
没有得到预期的输出
[1]: https://i.stack.imgur.com/6Mxdo.png
不确定我是否称它为瀑布图,但试试这个尺寸:
library(dplyr)
library(tidyr)
library(ggplot2)
df2 <- df %>%
gather(stat, val, Tasks, Accidents) %>%
mutate(
stat=factor(stat, levels=c('Tasks','Accidents')),
Country = factor(Country, levels=c('IND','SL','JAP','AUS'))
)
x1 <- df2 %>% group_by(Country, stat) %>% summarise(val=sum(val))
x2 <- df2 %>% group_by(stat) %>% summarise(val=sum(val))
ggplot(x1, aes(x=stat, y=val)) +
geom_col(aes(fill=Country)) +
geom_label(data=x2, aes(label=val), nudge_y = 100) +
theme(legend.position='top')
尝试省略将 stat
和 Country
转换为因数的 mutate
命令,看看情况如何变化。
试试这个
ggplot(x1, aes(x=stat, y=val),fill=Type) +
geom_bar(aes(fill=Country),stat = "identity") +
geom_label(data=x2, aes(label=val), nudge_y = 10,label.padding = unit(0.4, "lines")) +
scale_y_continuous(labels = scales::percent_format())+
theme(legend.position='top')+
geom_label(data=x1, aes(label=val), nudge_y = 10,label.padding = unit(0.1, "lines"))