0

I'd like to make a plot of workers' salary grades and compare people who are home based with people who are office based.

I would like one set of bars (in colour) represent the people in my data frame that are home based, and the other set of bars (in black) represent the people that are office based.

The problem is that I'd like the orientation of the plot to be the opposite of ggplots()'s default (see drawing of desired plot below). When I try to address the labeling with scale_y_continuous() my code breaks (I use scale_y_continuous() because I've first used coord_flip()).

Any help would be appreciated!

enter image description here

# Data 
df <- data.frame(question = c("work", "work", "work", "work", "work", "work"),
                 location = c("homebased", "homebased", "homebased", "officebased", "officebased", "officebased"),
                 salary = c("grade 1/2", "grade 3", "grade 4/5", " grade 1/2", "grade 3", "grade 4/5"),
                 freq = c(41.3, 53.5, 5.2, 37.2, 54.3, 9.5))

# Prep data to change direction of default plot
df$freq <- df$freq*-1

# Plot
library(ggplot2)
ggplot(mapping = aes(x = question, y = freq, fill = factor(salary))) +
  geom_col(data = df[df$location == "homebased", ], position = "dodge") +
  geom_col(data = df[df$location == "officebased", ], aes(group = salary),
           fill = "black", width = 0.45, position = position_dodge(width = 0.9))  + 
  coord_flip () +  theme_classic() +
  scale_x_discrete(name = "", position = "top") +      
  scale_y_continuous("freq", limits = c(0, -60), breaks =c(-60, -40, -20, 0),
                     labels = c(60, 40, 20, 0))
Henrik
  • 65,555
  • 14
  • 143
  • 159
LLL
  • 723
  • 1
  • 9
  • 27
  • Change to `limits = c(-60, 0)`. "_If the larger value comes first, the scale will be reversed_" seems to cause trouble. – Henrik Jun 14 '18 at 20:32
  • 1
    Or easier: skip `df$freq <- df$freq*-1` and the `scale_y_continuous` stuff, and use `scale_y_reverse(limits = c(60, 0))` instead. – Henrik Jun 14 '18 at 20:45

0 Answers0