使用 Plotly 解决堆积条形图的问题

问题描述 投票:0回答:1

我正在尝试绘制具有百分比结构的堆积条形图。下面你可以看到我的数据的一个小例子。

df<-structure(list(Year = c("2013", "2013", "2013", "2013", "2013", 
                        "2013", "2013", "2013", "2013", "2013", "2013", "2013"), Age = c("Children", 
                                                                                         "Adults", "Children", "Adults", "Children", 
                                                                                         "Adults", "Children", "Adults", "Children", 
                                                                                         "Adults", "Children", "Adults"), Country = structure(c(6L, 
                                                                                                                                                                            6L, 2L, 2L, 5L, 5L, 3L, 3L, 4L, 4L, 1L, 1L), levels = c("Aruba", 
                                                                                                                                                                                                                                    "Benin", "Burundi", "Kongo", "Mali", 
                                                                                                                                                                                                                                    "Nigeria"), class = "factor"), value = c(0.421052631578947, 0.578947368421053, 
                                                                                                                                                                                                                                                                            0.666666666666667, 0.333333333333333, 0, 0, 0.8, 0.2, 0.25, 0.75, 
                                                                                                                                                                                                                                                                            0.705263157894737, 0.294736842105263), color = structure(c(2L, 
                                                                                                                                                                                                                                                                                                                                       1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), levels = c("red", 
                                                                                                                                                                                                                                                                                                                                                                                                    "royalblue"), class = "factor")), row.names = c(NA, 12L), class = "data.frame")
                                                                                                                                                                                                                                                                                                                                                                                          

每个国家大人小孩的结构都是1,现在想用Plotly做一个条形图。我尝试使用下面的代码:

plot_ly(df, x = ~list(Year,Country), y = ~value, 
                color = ~Age, colors = ~as.character(color), type = "bar") %>% 
  layout(barmode = "stack",
         title = 'Title',
         xaxis = list(title = 'Percentage'), 
         yaxis = list(title = ' '))

此代码提供了一些不同的东西,而不是带有百分比的堆叠条形图。下面你可以看到一个例子。

所以可能错误在数据中的某个地方,但我找不到哪里(例如因素等)。那么有人可以帮助解决这个问题并为每个国家/地区制作 100% 百分比的条形图吗?

r plotly
1个回答
0
投票

按照 docs 的一个选项是将数据重塑为宽格式并使用第二个跟踪:

library(plotly)

df <- df %>%
  dplyr::select(-color)  %>%
  tidyr::pivot_wider(names_from = Age, values_from = value)

plot_ly(df,
  x = ~ list(Year, Country), y = ~Children,
  type = "bar", marker = list(
    color = "royalblue"
  ), name = "Children"
) %>%
  add_bars(y = ~Adults, marker = list(
    color = "red"
  ), name = "Adults")  %>%
  layout(
    barmode = "stack",
    title = "Title",
    xaxis = list(title = "Percentage"),
    yaxis = list(title = " ")
  )

© www.soinside.com 2019 - 2024. All rights reserved.