Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table with horizontal bar chart in ggplot2 or plotly

Tags:

r

ggplot2

plotly

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

like image 659
Mutuelinvestor Avatar asked Aug 04 '26 09:08

Mutuelinvestor


1 Answers

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

like image 87
thothal Avatar answered Aug 07 '26 01:08

thothal