图水平条形图中的排序列

时间:2018-11-13 16:10:01

标签: r r-plotly

我正在尝试改编Plotly文档中的this example,以显示针对两个人的问题的答案百分比(是/否)。例如,输入数据可以是:

x1 <- c(62.7, 89.2) # answered yes; can change, as values come from another function
x2 <- sapply(x1, function(x) 100-x) # answered no
y <- c("Class1", "Class2")
data <- data.frame(y, x1, x2)

library(plotly)
# ...generate chart according to example Plotly's in documentation

这将导致 something like this。但是,考虑到数据在向量y,x1和x2中的排列顺序,这些条形的顺序与预期的显示方式不同。

This question在Plotly中提到了条形图的类似问题,但是没有一种可能的变通方法表明这里似乎有什么不同-我不确定这与垂直是不相关的。 ,非堆叠条形图。此外,我正在寻找不需要预先知道x1和x2值的解决方案,因为其目的是动态生成这些图表。

1 个答案:

答案 0 :(得分:0)

重要的注意事项是plotly设置为按字母顺序对图进行排序。如果要更改它,只需尝试更改因子水平即可。这是我尝试按顺序排列条形的尝试。

x1 <- c(62.7, 89.2) # answered yes; can change, as values come from another function
x2 <- sapply(x1, function(x) 100-x) # answered no
y <- c("Class1", "Class2")
data <- data.frame(y, x1, x2)
# ------------------------------------------------
# PLEASE PROVIDE THE ORDER OF CLASSES HERE
# ------------------------------------------------
data$y <- factor(data$y, levels = c("Class2", "Class1")) 

library(plotly)

top_labels <- c('Yes', 'No')

p <- plot_ly(data, 
             x = ~x1, 
             y = ~y, 
             type = 'bar', 
             orientation = 'h',
             marker = list(color = 'rgba(38, 24, 74, 0.8)',
                           line = list(color = 'rgb(248, 248, 249)', width = 1))) %>%
  add_trace(x = ~x2, marker = list(color = 'rgba(71, 58, 131, 0.8)')) %>%
  layout(xaxis = list(title = "",
                      showgrid = TRUE,
                      showline = FALSE,
                      showticklabels = FALSE,
                      zeroline = FALSE,
                      domain = c(0.15, 1)),
         yaxis = list(title = "",
                      showgrid = FALSE,
                      showline = FALSE,
                      showticklabels = FALSE,
                      zeroline = FALSE),
         barmode = 'stack',
         paper_bgcolor = 'rgb(248, 248, 255)', 
         plot_bgcolor = 'rgb(248, 248, 255)',
         margin = list(l = 120, r = 10, t = 140, b = 80),
         showlegend = FALSE) %>%
  # labeling the y-axis
  add_annotations(xref = 'paper', 
                  yref = 'y', 
                  x = 0.14, 
                  y = y,
                  xanchor = 'right',
                  text = y,
                  font = list(family = 'Arial', size = 12,
                              color = 'rgb(67, 67, 67)'),
                  showarrow = FALSE, align = 'right') %>%

  # labeling the percentages of each bar (x_axis)
  add_annotations(xref = 'x', yref = 'y',
                  x = x1 / 2, y = y,
                  text = paste(data[,"x1"], '%'),
                  font = list(family = 'Arial', size = 12,
                              color = 'rgb(248, 248, 255)'),
                  showarrow = FALSE) %>%
  add_annotations(xref = 'x', yref = 'y',
                  x = x1 + x2 / 2, y = y,
                  text = paste(data[,"x2"], '%'),
                  font = list(family = 'Arial', size = 12,
                              color = 'rgb(248, 248, 255)'),
                  showarrow = FALSE) %>% 
  # labeling the first Likert scale (on the top)
  add_annotations(xref = 'x', yref = 'paper',
                  x = c(21 / 2, 21 +  120/ 2),
                  y = 1.10,
                  text = top_labels,
                  font = list(family = 'Arial', size = 12,
                              color = 'rgb(67, 67, 67)'),
                  showarrow = TRUE)

p

这给出了:

The Ordered Horizontal Bar Chart The Usual Plotly Example