R 瀑布图
样本数据R 瀑布图,r,ggplot2,r-plotly,R,Ggplot2,R Plotly,样本数据 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
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
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][1]
Y轴-国家/地区/日本/澳大利亚
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 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
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
国家/州/城市国旗值
IND KA BLR。任务8200
IND WB CCU任务2500
IND KA BLR。事故700
p%
布局(title=“Exp Output”,
xaxis=列表(title=“”),
yaxis=列表(title=“”),
autosize=TRUE,
showlegend=TRUE)
P
没有得到预期的输出
[1] :我不确定是否将其称为瀑布图,但请试着穿上这件衣服,看看大小:
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')
库(dplyr)
图书馆(tidyr)
图书馆(GG2)
df2%
收集(统计、val、任务、事故)%>%
变异(
统计=系数(统计,等级=c(“任务”,“事故”),
国家=系数(国家,级别=c('IND','SL','JAP','AUS'))
)
x1%按(国家、统计)分组%>%汇总(val=总和(val))
x2%按(统计)分组%>%总结(val=总和(val))
ggplot(x1,aes(x=stat,y=val))+
地理坐标(aes(填充=国家))+
几何图形标签(数据=x2,aes(标签=val),微移y=100)+
主题(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"))
最好的方法是包括一张你想要达到的目标的图片;要么在互联网上画一幅图像,要么自己用画笔(或类似工具)画一些东西。如果你用钢笔和纸画出图,使用样本数据中的实际数字,看看结果是否可行,那就更容易了。@MrGumble添加了图片。有没有可能使任务和事故栏成比例?与什么成比例?就像底部问题中提到的图片一样