I want to use the same code to run lots of graphs and to make data more readable I want to have two different scales. If max value of INIMEST is >= 500 then the x scales should run from 0 to 5000 and if not then from 0 to 500. I asked chatGPT how can I do it and it game me the following line:
scale_x_continuous(limits = ifelse(max(VOORTOOJOUD$INIMEST) >= 500, c(0, 5000), c(0, 500))) +
There is something wrong with it. It looks ok to me but the outcome is wrong. Now the scale is from -0.050 to 0.05.
VOORTOOJOUD <- data.frame(
AMETIALAGRUPP = c("Admin","Admin","Ametnik","Ametnik"),
TUNNUS = c("SISSERANNE","VALJARANNE","SISSERANNE","VALJARANNE"),
INIMEST = c(4000,3500,20,30)
)
VOORTOOJOUD <- VOORTOOJOUD %>%
filter(AMETIALAGRUPP == "Admin")
ggplot(VOORTOOJOUD ,aes(y=TUNNUS, x=INIMEST, fill=TUNNUS))+
geom_bar(stat = 'identity', width = 0.5)+
scale_fill_manual(values = c("VALJARANNE" = "#d30031",
"SISSERANNE" = "#ff9e16"),
name=" ",
labels=c("Väljaränne","Sisseränne"))+
theme_minimal(base_size = 20)+
xlab("Inimest")+
scale_x_continuous(limits = ifelse(max(VOORTOOJOUD$INIMEST) >= 500, c(0, 5000), c(0, 500))) +
geom_text(aes(label = INIMEST), hjust = -0.1, colour = "Black")+
theme(legend.position = "bottom",
axis.title.y=element_blank(),
axis.text.y=element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.minor = element_blank())