简体   繁体   English

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

[英]Table with horizontal bar chart in ggplot2 or plotly

Is there a ggplot2 extension package that can produce a table/horizontal bar chart viz similar to one on Fivethirty Eight.是否有一个 ggplot2 扩展 package 可以生成一个表格/水平条形图,即类似于五三十八的一个。 Ideally I would like the ability to add a second or even third bar chart and additional text columns.理想情况下,我希望能够添加第二个甚至第三个条形图和其他文本列。

I've been Googlging and looking at ggplot chart galleries, but haven't come across anything yet.我一直在谷歌搜索并查看 ggplot 图表库,但还没有遇到任何东西。

在此处输入图像描述

A workaround would be to use the DT package.一种解决方法是使用DT package。 This renders an HTML table, but depending on your needs you could use this one.这会呈现一个HTML表,但根据您的需要,您可以使用这个表。 In RStudio you can also easily export the table to any picture format: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")

在此处输入图像描述

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

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