r - ggplot2 或plotly 中带有水平条形图的表格

标签 r ggplot2 plotly

是否有一个 ggplot2 扩展包可以生成类似于五三八上的表格/水平条形图。理想情况下,我希望能够添加第二个甚至第三个条形图和其他文本列。

我一直在谷歌搜索并查看 ggplot 图表库,但还没有发现任何东西。

enter image description here

最佳答案

解决方法是使用DT包。这将呈现一个 HTML 表,但根据您的需要,您可以使用这个表。在RStudio中,您还可以轻松地将表格导出为任何图片格式:

library(DT)

dat <- data.frame(CANDIDATE = c("Biden", "Sanders", "Gabbard", "O'Rourke",
                                "Warren", "Yang", "Buttigieg", "Castro", 
                                "Harris", "Klobuchar", "Booker", "Steyer"),
                  EXCLUSIVE = c(218, 100, 11, 29, 107, 14, 27, 6, 23, 8, 4, 1), 
                  TOTAL     = c(996, 683, 83, 245, 917, 187, 437, 105, 433, 180, 201, 84), 
                  SHARE     = c(21.9, 14.6, 13.1, 11.7, 11.6, 7.4, 6.2, 
                                5.7, 5.2, 4.2, 2.2, 1.4) / 100)

datatable(dat, options = list(
  columnDefs = list(list(className = "dt-left", targets = 4)))) %>%
  formatPercentage("SHARE", 1) %>%
  formatStyle("SHARE",
              background = styleColorBar(dat$SHARE, "steelblue", -90), backgroundPosition = "left")

enter image description here

关于r - ggplot2 或plotly 中带有水平条形图的表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58821644/

相关文章:

python - 使用时间戳绘制甘特图

r - 如何将 docx 转换为 PDF?

r - 计算每组唯一列值的数量

r - 做 arrangeGrob 时是否可以裁剪图?

R ggplot突出显示季度数字表已取代数字

python - 在python plotly包中的散点图中操纵图例

R openNLP 找不到函数 sentDetect()

r - 如何增加 ggplot R 中的轴标签文本大小?

r - 如何将自定义 ggplot 别名存储为 .R 脚本中的 `list` 对象,并沿着所需的标签向量获取它们

r - 更新R Shiny 的绘图数据(叶绿体),无需重新渲染整个 map