0

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.

enter image description here

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())

Picataro
  • 151
  • 6
  • 1
    Use `if`, not `ifelse`, i.e. `scale_x_continuous(limits = if(max(VOORTOOJOUD$INIMEST) >= 500) c(0, 5000) else c(0, 500))`. – user2554330 Mar 05 '23 at 09:58

2 Answers2

1

I would recommend you having a look at this post about what you should do to return a vector with ifelse. Your test should have the same length as your results of your ifelse like this:

library(dplyr)
library(ggplot2)
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(c(TRUE, 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())


Example with data less than 500:

VOORTOOJOUD <- data.frame(
  AMETIALAGRUPP = c("Admin","Admin","Ametnik","Ametnik"),
  TUNNUS = c("SISSERANNE","VALJARANNE","SISSERANNE","VALJARANNE"),
  INIMEST = c(400,350,20,30)
)

library(dplyr)
library(ggplot2)
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(c(TRUE, 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())

Created on 2023-03-05 with reprex v2.0.2

Quinten
  • 35,235
  • 5
  • 20
  • 53
1

For this kind of condition I would suggest to ximply use an if instead of ifelse. Perosnally I also prefer to do this outside of ggplot():

library(ggplot2)

xlims <- if (max(VOORTOOJOUD$INIMEST) >= 500) c(0, 5000) else c(0, 500)

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 = xlims) +
  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()
  )

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51