2

Is there a ggplot2 extension package that can produce a table/horizontal bar chart viz similar to one on Fivethirty Eight. 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.

enter image description here

Mutuelinvestor
  • 3,384
  • 10
  • 44
  • 75
  • Package requests are off-topic, but e.g. see here: https://stats.stackexchange.com/questions/8242/plotting-sparklines-in-r. Or a dupe on SO: https://stackoverflow.com/questions/32841221/add-sparkline-graph-to-a-table – Axeman Nov 12 '19 at 20:45
  • Or see this paper: https://journal.r-project.org/archive/2015-1/templ-kowarik-meindl.pdf – Axeman Nov 12 '19 at 20:49
  • https://glin.github.io/reactable/articles/building-twitter-followers.html – Maad scientist Oct 24 '20 at 12:19

1 Answers1

5

A workaround would be to use the DT package. This renders an HTML table, but depending on your needs you could use this one. In RStudio you can also easily export the table to any picture format:

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

thothal
  • 16,690
  • 3
  • 36
  • 71