2

I would like to display a data table at the bottom of the graph similar to this:

Sample Graph

I have been trying the code in this post but it does not run in my RStudio version 1.1.383.

The dataframe for the graph is:

Pay.Quartiles = rep(c("lower", "lower.mid", "upper.mid", "upper"),2)
Gender = rep (c("Female", "Male"), each = 4)
Percentage = c(65.1,57.5,47.4, 41.3, 34.9, 42.5, 52.6, 58.7)

df = data.frame (Pay.Quartiles, Gender, Percentage)
1
  • 1
    The RStudio version is somewhat irrelevant (the versions of R & ggplot2 are likely more important). Ideally, you'd include the code you tried with the post so folks who might want to answer can use the same code you tried (humans are imperfect and you may have transcribed the code errantly) and not have to hit another site to try to render assistance, Commented Sep 17, 2018 at 11:12

1 Answer 1

4

A combination of some data wrangling (to get better labels) and faceting with some strip placement tweaks should do the trick:

library(hrbrthemes) # gitlab.com/hrbrmstr/hrbrthemes or github
library(ggplot2)

Pay.Quartiles <-  rep(c("Lower", "Lower Middle", "Upper Middle", "Upper"), 2)
Gender <-  rep(c("Female", "Male"), each = 4)
Percentage <-  c(65.1,57.5,47.4, 41.3, 34.9, 42.5, 52.6, 58.7)/100

xdf <- data.frame (Pay.Quartiles, Gender, Percentage, stringsAsFactors=FALSE)

xdf$lab <- sprintf("%s\n%s", Gender, scales::percent(Percentage))

ggplot(xdf) +
  geom_col(
    aes(lab, Percentage, fill = Gender), 
    width = 0.5, show.legend = FALSE
  ) +
  facet_wrap(
    ~Pay.Quartiles, scales = "free_x", nrow = 1, strip.position = "bottom"
  ) +
  hrbrthemes::scale_y_percent() +
  hrbrthemes::scale_fill_ipsum() +
  labs(x = NULL, y = NULL) +
  hrbrthemes::theme_ipsum_rc(grid = "Y") +
  theme(strip.placement = "outside") +
  theme(strip.text = element_text(hjust=0.5)) +
  theme(panel.spacing.x = unit(0.33, "lines"))

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much. I didn't know the package "hrbrthemes". This works great!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.