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

时间:2016-11-26 04:12:42

标签: r charts plotly

我有一个如下所示的数据集

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 

我需要一个百分比列堆栈图,如下所示 What I need

我尝试了以下代码,但所需的输出并未显示为百分比堆栈条形图。

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")

这是我目前得到的 What I currently get

我找不到这种类型的图表在R中的情节包装的任何答案。任何人都可以帮我这个吗?

2 个答案:

答案 0 :(得分:1)

以下代码为您提供了您描述的图表,其中包含了hovertext中每个城市的类型(成人/儿童/婴儿)的总数(如果您还想在图表上使用,则可以尝试添加注释)

  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") 

答案 1 :(得分:0)

由于您希望在#34; City&#34;不是&#34; House_Unit_Id&#34;,你可以做两件事。使用您现在拥有的比例并创建堆叠和分组条形图的组合,您可以按House_Unit_Id堆叠并按城市分组。目前,我不认为情节包(如果我错了,请纠正我)能够做到这一点。您可以使用ggplot来执行此操作。

但是,如果你真的只想比较整个城市的百分比,你应该在成人,儿童和婴儿中汇总计数,如下所示:

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

然后按城市计算比例 。之后,您可以将百分比绘制为每个城市的堆积条形。

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")

如果按城市汇总计数数据并计算每个城市的比例,这将有效。