如何在3级层次结构上应用sunburstR?
我们如何表示表中的所有不同层次结构?i、 e如何在3级层次结构上应用sunburstR?,r,ggplot2,bar-chart,sunburst-diagram,R,Ggplot2,Bar Chart,Sunburst Diagram,我们如何表示表中的所有不同层次结构?i、 e 佩鲁库伊 根据 佩鲁苏伊 某种条形图/堆叠条形图没有帮助 此外,sunburstR可能太过恭维而无法实现 您能建议我们如何在一个绘图/图表中表示所有这些层次结构吗 library(tidyverse) df <- data.frame(item = c("apple","orange","apple","banana","apple","apple"), + cust = c(rep("john",2),"
- 佩鲁库伊
- 根据
- 佩鲁苏伊
library(tidyverse)
df <- data.frame(item = c("apple","orange","apple","banana","apple","apple"),
+ cust = c(rep("john",2),"papa","john",rep("jerry",2)),
+ shop = c("shop1","shop2",rep("shop1",2),rep("shop2",2)),
+ sales = c(12,13,6,8,10,9)
+ )
df_m <- df %>%
+ group_by(cust,item) %>%
+ mutate(per_c_i = 100*sales/sum(sales)) %>%
+ group_by(item) %>%
+ mutate(per_c = 100* per_c_i / sum(per_c_i))
df_c <- merge(df,df_m, by = c("cust","item","shop","sales")) %>%
+ group_by(shop,item) %>%
+ mutate(per_s_i = 100*per_c/sum(per_c))
df_c
# A tibble: 6 x 7
# Groups: shop, item [4]
cust item shop sales per_c_i per_c per_s_i
<fct> <fct> <fct> <dbl> <dbl> <dbl> <dbl>
1 jerry apple shop2 10 52.6 17.5 52.6
2 jerry apple shop2 9 47.4 15.8 47.4
3 john apple shop1 12 100 33.3 50
4 john banana shop1 8 100 100 100
5 john orange shop2 13 100 100 100
6 papa apple shop1 6 100 33.3 50
# Attempt via ggplot
df_c %>%
ggplot(aes(x = cust,
y = per_s_i,
fill = item)) +
geom_bar(stat = "identity"#, position=position_dodge()
) +
geom_col() +
coord_flip()
df_c %>% knitr::kable()
库(tidyverse)
df%
+变异(每单位销售额=100*销售额/总额(销售额))%>%
+分组依据(项目)%>%
+变异(每立方=100*每立方/总和(每立方))
df_c%
+分组依据(车间、项目)%>%
+变异(每平方米=100*每平方米/总和(每平方米))
df_c
#一个tibble:6x7
#分组:商店,项目[4]
客户商品商店每_c_i每_c每_s_i的销售额
1杰瑞苹果店2 10 52.6 17.5 52.6
2杰瑞苹果店2 9 47.4 15.8 47.4
3 john apple Shop112 100 33.3 50
4约翰香蕉店1 8 100 100
5约翰橙店2 13 100 100
6爸爸苹果店16 100 33.3 50
#通过ggplot尝试
df_c%>%
ggplot(aes(x=cust,
y=每平方米,
填充=项目)+
geom_条(stat=“identity”#,position=position_道奇()
) +
geom_col()+
coord_flip()
df_c%>%knitr::kable()
应用SunburstR但不渲染图像
库(d3r)
图书馆(sunburstR)
df_s%
重命名(“级别1”=“客户”,
“level2”=“物品”,
“三级”=“车间”)
tree数据可视化本质上是主观的,有许多方法可以可视化数据,“最佳”方法将在很大程度上取决于数据上下文和研究者偏好
由于您基本上使用的是三向设计(“cust”、“item”和“per”),因此很难在单个绘图中有效地捕获所有这些信息。一种选择是使用刻面,这样您就可以在不同的面板中显示每个变量。为了做到这一点,您需要将数据从宽格式转换为长格式,以便您感兴趣的变量(“per”变量)位于单个列中,宽列名作为一个新的因素
# Reshape the data from wide to long
library(reshape2)
df_l <- melt(df_c[colnames(df_c) %in% c("cust", "item", "per_c_i", "per_c", "per_s_i")],
id.vars = c("cust", "item"))
#plot the data, faceting by 'hierarchy'
ggplot(df_l, aes(x = cust, y = value, fill = item)) +
geom_bar(stat = "identity", position = position_dodge()) +
facet_grid(rows = vars(variable))
#将数据从宽改为长
图书馆(E2)
东方舟
# Reshape the data from wide to long
library(reshape2)
df_l <- melt(df_c[colnames(df_c) %in% c("cust", "item", "per_c_i", "per_c", "per_s_i")],
id.vars = c("cust", "item"))
#plot the data, faceting by 'hierarchy'
ggplot(df_l, aes(x = cust, y = value, fill = item)) +
geom_bar(stat = "identity", position = position_dodge()) +
facet_grid(rows = vars(variable))