1

Before I ask the question, I just want to say that I have looked every thread in here, but i couldn't find an easy solution for my easy question.

vs <- data.frame(Gelen = c(677042, 577457, 466333, 380921),
                 Giden = c(330289, 323918, 253640, 177960),
                 Zaman = c(2019, 2018, 2017, 2016)) %>% 
  pivot_longer(cols = c(Gelen, Giden), names_to = "group", values_to = "count")

## Ref: https://5harad.com/mse125/r/visualization_code.html
addUnits <- function(n) {
  labels <- ifelse(n < 1000, n,  # less than thousands
                   ifelse(n < 1e6, paste0(round(n/1e3), 'k'),  # in thousands
                          ifelse(n < 1e9, paste0(round(n/1e6), 'M'),  # in millions
                                 ifelse(n < 1e12, paste0(round(n/1e9), 'B'), # in billions
                                        ifelse(n < 1e15, paste0(round(n/1e12), 'T'), # in trillions
                                               'too big!'
                                        )))))
  return(labels)
}

vs %>% 
  ggplot(aes(x=Zaman, y=count, color = group, fill = group)) +
  geom_glowing_line() +
  theme_cyberpunk() +
  scale_color_linesaber(reverse = T) +
  scale_y_continuous(labels = addUnits) +
  ggtitle("Türkiye'ye gelen ve Türkiye'den giden göç", subtitle = "2016-2019") +
  geom_text(data = vs, label = vs$count, vjust = -1) 
  

As you see in the chunk, I am using adjusted labels (cf. addUnits() function) for my y-axis and this allows me to display numeric values ​​as 100k, 200k so on. But how can I extend the y-axis top limit to 800k,900k or whatever? Because the ylim function doesn't work with adjusted addUnits(). So my graph showing the values at the breakpoints doesn't look good:

enter image description here

lovalery
  • 4,524
  • 3
  • 14
  • 28
Ali Osman
  • 17
  • 4
  • You could set the limits via the `limits` argument of the scale, i.e. `scale_y_continuous(labels = addUnits, limits = c(NA, 8e5))` and if you want different breaks then use the `breaks` argument – stefan Jan 29 '22 at 10:00
  • See e.g. [How to set limits for axes in ggplot2 R plots?](https://stackoverflow.com/questions/3606697/how-to-set-limits-for-axes-in-ggplot2-r-plots) – stefan Jan 29 '22 at 10:03

0 Answers0