简体   繁体   English

R:情节中的百分比堆栈条形图

[英]R: Percentage stack bar chart in plotly

I have a dataset like below 我有一个像下面的数据集

City<-c("X","Y","Z","X","Z","X","Y")
House_Unit_Id<-c("H1","H2","H3","H4","H5","H6","H7")
Adult<-c(50,100,60,40,50,80,60)
Child<-c(40,0,40,20,50,20,30)
Baby<-c(10,0,0,40,0,0,10)
data<-data.frame(City,House_Unit_Id,Adult,Child,Baby)

 City House_Unit_Id Adult   Child   Baby
  X       H1         50      40     10
  Y       H2        100       0      0
  Z       H3         60      40      0
  X       H4         40      20     40
  Z       H5         50      50      0
  X       H6         80      20      0
  Y       H7         60      30     10 

I need a percentage column stack chart like below 我需要如下所示的百分比列堆栈图 我需要的

I tried the below code but the required output is not appearing as a percentage stack bar chart. 我尝试了以下代码,但所需的输出未显示为百分比堆栈条形图。

Chart <- plot_ly(data,x = ~City, y = ~Adult, type = 'bar',name= 'Adult') %>% 
  add_trace(y = ~Child, name = 'Child') %>% 
  add_trace(y = ~Baby, name = 'Baby') %>% 
  layout(yaxis = list(title = 'Percentage (%)'),barmode = "stack")

This is what I currently get 这就是我目前得到的 我现在得到什么

I could not find any answers for this type of chart for plotly package in R. Can anyone please help me with this? 我在R中无法找到这种图表的答案,请问有人可以帮助我吗?

The following code gives you the plot you describe, with the total number of type (Adult/Children/Baby) in each City in the hovertext (If you also want it on the plot itself you can try add_annotations) 以下代码为您提供了您所描述的情节,在悬停文本中每个城市的类型总数(成人/儿童/婴儿)(如果您还希望在情节本身上尝试添加,请尝试add_annotations)

  City<-c("X","Y","Z","X","Z","X","Y")
  House_Unit_Id<-c("H1","H2","H3","H4","H5","H6","H7")
  Adult<-c(50,100,60,40,50,80,60)
  Child<-c(40,0,40,20,50,20,30)
  Baby<-c(10,0,0,40,0,0,10)
  data<-data.frame(City,House_Unit_Id,Adult,Child,Baby)

  library(plyr)
  # Changing the data frame before plotting ... there is propably an easier way to do this!
  newdata <- ldply(3:5,function(n){tempdata <- data[,c(1,n)]
                               colnames(tempdata)[2] <- "Number"
                               tempdata$type <- colnames(data[n])
                               return(tempdata)})
  newdata <- ddply(newdata,.(City,type),summarize,Number=sum(Number))
  # Total for each city
  datatotal <- ddply(newdata,~City,summarize,n=sum(Number))
  # Merge the data frames together
  newdata <- merge(newdata,datatotal)
  # Calc the percentages
  newdata$perc <- newdata$Number/newdata$n

plot_ly(newdata,x = ~City, y = ~perc*100, type = 'bar',color = ~type,text=~Number,hoverinfo = 'text') %>% 
   layout(yaxis = list(title = 'Percentage (%)'),barmode = "stack") 

Since you want stacked percentage barplots across "City" not "House_Unit_Id", there are two things you can do. 由于您希望跨“城市”而不是“ House_Unit_Id”的堆叠百分比条形图,您可以做两件事。 Either use the proportions you now have and create a combination of stacked and grouped barplot, where you are stacking by House_Unit_Id and grouping by City. 要么使用您现在拥有的比例,要么创建堆叠和分组条形图的组合,然后在其中按House_Unit_Id进行堆叠,然后按City进行分组。 Currently, I don't think the plotly package (correct me if I am wrong) is capable of doing this. 目前,我认为plotly软件包(如果我做错了,请纠正我)不能做到这一点。 You can use ggplot to do this. 您可以使用ggplot执行此操作。

However, if you really want to only compare percentages across City, you should aggregate the counts in Adult, Child and Baby with something like this: 但是,如果您只想比较整个城市的百分比,则应使用以下内容汇总“成人”,“儿童”和“婴儿”中的计数

newData = aggregate(. ~ City, data = data, FUN = sum)

Then calculate the proportions by City . 然后按城市计算比例。 After that, you can plot the percentages as stacked bars for each City. 之后,您可以将百分比绘制为每个城市的堆积条形图。

Chart <- plot_ly(newData, x = ~City, y = ~Adult, type = 'bar', name= 'Adult') %>% 
  add_trace(y = ~Child, name = 'Child') %>% 
  add_trace(y = ~Baby, name = 'Baby') %>% 
  layout(yaxis = list(title = 'Percentage (%)'), barmode = "stack")

This will work if you aggregate your counts data by City and calculate the proportions for each City. 如果您按城市汇总计数数据并计算每个城市的比例,这将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM