I'm having some weird issues with ggplot in RStudio. This isn't the first time this has happened either.
I tried to create a reprex but wasn't able to reproduce the behaviour that I am getting in the figure below. (I've blanked out the y-axis labels for privacy)
Here's the code: pretty basic...
AED_screen_plot <- AED_num %>%
ggplot(aes(x = reorder(DHB,SCREENING.RATE), y = SCREENING.RATE, fill = AUDIT.YEAR)) +
geom_bar(position = 'dodge', stat = 'identity') +
ylab('Screening Rate (%)') +
coord_flip()
print(AED_screen_plot)
And since I can't provide a simple reprex, here's the data...
data <- tibble('AUDIT.YEAR' = c(2019,2019,2019,2019,2019,2019,2019,2019,2019,2019,2019,2019,2019,2019,2019,2019,2019,2019,2019,2019,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018), 'DHB' = c('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t'), 'SCREENING.RATE' = c(0,36,44,28,8,0,0,20,40,40,24,52,16,45,52,28,48,28,44,56,4,16,44,12,56,16,20,28,32,32,4,44,80,40,52,20,8,12,52,24))
What the hell am I doing wrong???? Why are the bar colours swapping around? Why is the x-axis munted? (Same thing happens without coord_flip().) Is it a data type problem or something? I've already tried converting everything to factors. Taking out the reorder() creates an identical graph.
Thanks very much. It's late Friday afternoon and I'm buggered.
I have used the following code and it works fine for me using the sample data provided by you. The only change I have made is that I have converted your year as a factor variable in place of default numeric or double.
library(tidyverse)
data <- tibble('AUDIT.YEAR' = c(2019,2019,2019,2019,2019,2019,2019,2019,2019,2019,2019,2019,2019,2019,2019,2019,2019,2019,2019,2019,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018), 'DHB' = c('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t'), 'SCREENING.RATE' = c(0,36,44,28,8,0,0,20,40,40,24,52,16,45,52,28,48,28,44,56,4,16,44,12,56,16,20,28,32,32,4,44,80,40,52,20,8,12,52,24))
data %>%
ggplot(aes(x = reorder(DHB,SCREENING.RATE), y = SCREENING.RATE, fill = factor(AUDIT.YEAR))) +
geom_bar(position = 'dodge', stat = 'identity') +
ylab('Screening Rate (%)') + xlab("DHB")+
labs(fill='Year') + coord_flip()
If I you use year as such (numeric or continuous) variable it will give you the following plot
data %>%
ggplot(aes(x = reorder(DHB,SCREENING.RATE), y = SCREENING.RATE, fill = AUDIT.YEAR)) +
geom_bar(position = 'dodge', stat = 'identity') +
ylab('Screening Rate (%)') + xlab("DHB")+
labs(fill='Year') + coord_flip()
Hope that helps you out.