I've seen several questions about the order of x-axis marks but still, none of them could solve my problem. I'm trying to do a plot that counts 10 variables in two different. My variables are a factor , and the structure is something like that:
crime_1 crime_2 ...... crime_10
Yes No Yes
Yes Yes No
No Yes No
I have used this code :
new_data %>%
pivot_longer(cols = starts_with("crime")) %>%
filter(value != 0) %>%
unite(crime,name, value) %>%
ggplot(aes(x = crime )) +
geom_bar(aes(fill = wave), position = position_dodge2(preserve = "single"))+ theme(axis.text.x=element_text(angle=90,hjust=1))+ggtitle("Crime")
The output is NOT crime_1, crime_2, ......., crime_10, and it is crime_1, crime_10, crime_2, ,,,, .
I have used scale_x_discrete(drop = FALSE )
, and fct_inorder().
I need to make the variables based on the order. Thank you