Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Axis labels for two variables in ggplot?

I have a ggplot with three variables: office, level, and gender. The plot should be factored by office and level and shows the levels and gender representation for each office. I have no problem faceting the plot the way I want, but I haven’t been able to create x axis text to show the levels in each office — all that shows up under the x-axis are the office names, which makes it impossible to distinguish among the bars for the different levels (the gender I handle with different colors and isn’t a problem, unless someone is colorblind …). I’ve edited out the lines for the title, xLab, yLab, etc. to focus on the issue. Grateful for your help!

Here is my data:

#A tibble: 20 x 4

   office     level   gender     count
 1 office1      A     Female     3
 2 office1      A     Male       1
 3 office1      B     Female     11
 4 office1      B     Male       9
 5 office1      C     Female     16
 6 office1      C     Male       13
 7 office1      C     Other      2
 8 office1      D     Female     16
 9 office1      D     Male       11
10 office1      D     Female     6
11 office1      D     Male       10
12 office1      D     Other      1
13 office2      A     Male       1
14 office2      A     Female     3
15 office2      B     Male       1
16 office2      B     Female     12
17 office2      B     Male       3
18 office2      B     Female     1
19 office2      D     Male       5
20 office2      D     Male       1

And here is my code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

myBarChart <- function(data,var1,var2,var3,count,factor1, factor2) {
  
# the following code creates the tibble above when I run the function:
  fields <- c(var1,var2,var3)
  data <- df[ ,fields]
  data[[var1]] <- factor(data[[var1]], levels = factor1)
  data[[var2]] <- factor(data[[var2]], levels = factor2)
  data <- data %>% group_by_at(fields) %>% summarize(count = n()) 
  data <- filter(data,count >= 1)
  data <- na.omit(data) %>% 

# here is the plot code and this is where I need help: 
  ggplot(data, aes_string(x=var1, y=count, fill=var3)) +
    geom_bar(stat = 'identity',width=1.25,position = position_dodge2(padding=0.25,reverse=FALSE,preserve=c("single"))) +
    geom_text(aes(label=count),vjust=-.5,position=position_dodge2(width=1.25,preserve="single"),size=2.5) +
    facet_wrap(var1, strip.position="bottom", scales = "free_x", nrow=1)  +

# I was hoping that the code for strip.text.x would give me the values for var2 (the levels) within each office, but it doesn't.
    theme(strip.text.x = element_text(aes(label=data[[var2]]), size=12)) + 
    theme(strip.placement = "outside")

} # end function

# here is how I invoke the function, which runs fine except for the axis labels always reflecting only 'office': 
     myBarChart(data,'office','level','gender',c('office1','office2'),c("A","B","C","D"),'count')

>Solution :

I think you need to use level for the x-axis, count for the y-axis, facet by office and fill by gender ?

Something like this:

data %>% 
  ggplot(aes(level, count, fill = gender)) + 
  geom_col(position = position_dodge2(preserve = "single")) + 
  facet_wrap(~office, strip.position = "bottom") + 
  theme(strip.placement = "outside")

Result:

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading