E. Williamson
E. Williamson

Reputation: 77

How to properly format percentage plot axis labels and bars in ggplot2

I've got a dataframe I imported from SPSS into R, and I'm having trouble properly formatting the y axis in a ggplot2 percentage bar chart.

I need to constrain the axis range to a smaller amount and also lengthen those bars. This is what I keep getting:

enter image description here

Here's the code for the above visualization:

#import packages
library(foreign)
library(ggthemes)
library(stringr)
library(ggplot2)
library(scales)

#read in data
WBGC <- read.spss("2019.07.14_Cleaned.Data.sav", use.value.label=TRUE, to.data.frame=TRUE)
#define member/non-member datasets
WBGC_members <- subset(WBGC, Freq.Of.Attendance == 'Once a month' | Freq.Of.Attendance == 'A few times a month' | Freq.Of.Attendance == 'Once or twice a week' | Freq.Of.Attendance == '3-5 days a week')

#visualization of race
student_race <- ggplot(data = WBGC_members, aes(x = Race, fill = Gender)) 
+ theme_hc()
+ geom_bar(colour = "black", stat = "count", aes(y = prop.table(stat(count))), position = position_dodge(), size = 0.5) 
+ labs(title = "Student Race", y = "Frequency") 
+ scale_y_continuous(labels = percent) 
+ geom_label(data = WBGC_members, stat = 'count', aes(label = scales::percent(prop.table(stat(count))), vjust = -0.4, fontface = 'bold'), size = 6, position = position_dodge(0.9), alpha = 1.0, show.legend = FALSE) 
+ theme(
  plot.title = element_text(size = 16, face = 'bold', family = '', color = 'black', hjust = 0.5, lineheight = 1.2), 
  axis.title.x = element_blank(), 
  axis.text.x = element_text(size = 12, angle = 45, vjust = 0.5),
  axis.title.y = element_text(size = 14, margin = margin(t = 0, r = 8, b = 0, l = 0)),
  axis.text.y = element_text(size = 12),
  legend.title = element_text(size = 14, color = "black", face="bold", hjust = 1, lineheight = 4),
  legend.text = element_text(size = 13),
  legend.position = 'right',
  legend.box.background = element_rect(colour = 'black')
  )
student_race

Since it looks like the labels are doing OK, I added scales::percent to the aes y= argument in geom_bar and had to delete the scale_y_continuous function. I wound up with this:

percentage plot attempt 2

Any help is much appreciated. Thanks!

Upvotes: 0

Views: 1282

Answers (1)

E. Williamson
E. Williamson

Reputation: 77

Fixed by adding y = prop.table(stat(count)) to the geom_label function call.

Here's the result:

enter image description here

And final code for reference:

student_race <- ggplot(data = WBGC_members, aes(x = Race, fill = Gender)) +
  theme_hc()+
  geom_bar(colour = "black", stat = "count", aes(y = prop.table(stat(count))), position = position_dodge(), size = 0.5) +
  geom_label(data = WBGC_members, stat = 'count', aes(label = scales::percent(prop.table(stat(count))), y = prop.table(stat(count)), vjust = -0.4, fontface = 'bold'), size = 6, position = position_dodge(0.9), alpha = 1.0, show.legend = FALSE) +
  labs(title = "Student Race", y = "Frequency") +
  scale_y_continuous(labels = scales::percent, limits = c(0,0.2)) + 
  theme(plot.title = element_text(size = 16, face = 'bold', family = '', color = 'black', hjust = 0.5, lineheight = 1.2),
        axis.title.x = element_blank(),
        axis.text.x = element_text(size = 12, angle = 45, vjust = 0.5),
        axis.title.y = element_text(size = 14, margin = margin(t = 0, r = 8, b = 0, l = 0)),
        axis.text.y = element_text(size = 12),
        legend.title = element_text(size = 14, color = "black", face="bold", hjust = 1, lineheight = 4),
        legend.text = element_text(size = 13),
        legend.position = 'right',
        legend.box.background = element_rect(colour = 'black')
  )
student_race

Upvotes: 1

Related Questions