Ggplot: how to define the breaks with a discrete y axis

I am able to define breaks for continous y axis, however applying the code for a discrete y axis is not working. I cannot seem to get the right script . I tried the epi handbook
The y axis shows percentages as discrete variables 1-15%, the y axis is plotted with 1% breaks, where I want to have it with 5% breaks.
the graph is geom_point

kim

1 Like

Hi Kim,

It would help if you could provide a reproducible example (reprex), or even the code you have been using so far, so that we can help diagnose the problem.

All the best,

Tim

the code is

summ_EV_prop_season$Positive ← factor(summ_EV_prop_season$Positive, levels=c(‘1%’, ‘2%’, ‘3%’,‘4%’, ‘5%’, ‘6%’,‘7%’, ‘8%’, ‘9%’,‘10%’, ‘11%’, ‘12%’,‘13%’, ‘14%’, ‘15%’,‘16%’, ‘17%’, ‘18%’,‘19%’))

summ_EV_prop_season %>% 
filter(!(sample_month == "nd")) %>% 
ggplot(mapping = aes(x = sample_month,
                     y= Positive))+
geom_point(size = 3, color = "black")+
scale_y_discrete(position = "right") +
labs(title = "Enterovirus testing", 
     subtitle = "all samples", 
     x= "years", 
     y= "positive %")+
theme(axis.text.x=element_text(angle=45, hjust=0.9, color = "red"),
      axis.ticks.y = element_line(color = "red"),
      axis.title.y = element_text(color = "red"),
      axis.line.y = element_line(color = "red"),
      axis.text.y = element_text(color = "red"),
      axis.ticks.x = element_line(color = "red"),
      axis.title.x = element_text(color = "red"),
      axis.line.x = element_line(color = "red"),
)+
facet_wrap(~ year) 

this is the data for 2015 in the dataframe

image

1 Like

Hi Kim,

This is how I would approach the problem, you should be using a continuous scale for numeric variables, such as proportions or percentages.

# loading packages
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)
library(tibble)

# creating fake data
fake_data <- tibble(year = 2015L, sample_month = c(paste(as.character(1L:12L), month.abb, sep = " "), "nd")) |>
    rowwise() |>
    mutate(
        count = rpois(n = 1, lambda = 1000),
        positive = rbinom(n = 1, size = count, prob = 0.1) / count
    ) |>
    ungroup()

# plotting data
fake_data |>
    ggplot() +
    geom_point(aes(x = sample_month, y = positive)) +
    scale_x_discrete(limits = c(paste(as.character(1L:12L), month.abb, sep = " "), "nd")) +
    scale_y_continuous(breaks = scales::extended_breaks(),
                                         labels = scales::percent_format()) +
    labs(x = "\nSample month", y = "Percentage of samples positive\n") +
    theme_minimal()

Created on 2024-12-13 with reprex v2.1.1

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.4.2 (2024-10-31)
#>  os       macOS Sequoia 15.1.1
#>  system   x86_64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       America/Toronto
#>  date     2024-12-13
#>  pandoc   3.2 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/x86_64/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  cli           3.6.3   2024-06-21 [1] RSPM (R 4.4.0)
#>  colorspace    2.1-1   2024-07-26 [1] RSPM (R 4.4.0)
#>  curl          6.0.1   2024-11-14 [1] RSPM (R 4.4.0)
#>  digest        0.6.37  2024-08-19 [1] RSPM (R 4.4.0)
#>  dplyr       * 1.1.4   2023-11-17 [1] RSPM (R 4.4.0)
#>  evaluate      1.0.1   2024-10-10 [1] RSPM (R 4.4.0)
#>  fansi         1.0.6   2023-12-08 [1] RSPM (R 4.4.0)
#>  farver        2.1.2   2024-05-13 [1] RSPM (R 4.4.0)
#>  fastmap       1.2.0   2024-05-15 [1] RSPM (R 4.4.0)
#>  fs            1.6.5   2024-10-30 [1] RSPM (R 4.4.1)
#>  generics      0.1.3   2022-07-05 [1] RSPM (R 4.4.0)
#>  ggplot2     * 3.5.1   2024-04-23 [1] RSPM (R 4.4.0)
#>  glue          1.8.0   2024-09-30 [1] RSPM (R 4.4.0)
#>  gtable        0.3.6   2024-10-25 [1] RSPM (R 4.4.0)
#>  htmltools     0.5.8.1 2024-04-04 [1] RSPM (R 4.4.0)
#>  knitr         1.49    2024-11-08 [1] RSPM (R 4.4.0)
#>  labeling      0.4.3   2023-08-29 [1] RSPM (R 4.4.0)
#>  lifecycle     1.0.4   2023-11-07 [1] RSPM (R 4.4.0)
#>  magrittr      2.0.3   2022-03-30 [1] RSPM (R 4.4.0)
#>  munsell       0.5.1   2024-04-01 [1] RSPM (R 4.4.0)
#>  pillar        1.9.0   2023-03-22 [1] RSPM (R 4.4.0)
#>  pkgconfig     2.0.3   2019-09-22 [1] RSPM (R 4.4.0)
#>  R6            2.5.1   2021-08-19 [1] RSPM (R 4.4.0)
#>  reprex        2.1.1   2024-07-06 [1] RSPM (R 4.4.0)
#>  rlang         1.1.4   2024-06-04 [1] RSPM (R 4.4.0)
#>  rmarkdown     2.29    2024-11-04 [1] RSPM (R 4.4.1)
#>  rstudioapi    0.17.1  2024-10-22 [1] RSPM (R 4.4.0)
#>  scales        1.3.0   2023-11-28 [1] RSPM (R 4.4.0)
#>  sessioninfo   1.2.2   2021-12-06 [1] RSPM (R 4.4.0)
#>  tibble      * 3.2.1   2023-03-20 [1] RSPM (R 4.4.0)
#>  tidyselect    1.2.1   2024-03-11 [1] RSPM (R 4.4.0)
#>  utf8          1.2.4   2023-10-22 [1] RSPM (R 4.4.0)
#>  vctrs         0.6.5   2023-12-01 [1] RSPM (R 4.4.0)
#>  withr         3.0.2   2024-10-28 [1] RSPM (R 4.4.0)
#>  xfun          0.49    2024-10-31 [1] RSPM (R 4.4.0)
#>  xml2          1.3.6   2023-12-04 [1] RSPM (R 4.4.0)
#>  yaml          2.3.10  2024-07-26 [1] RSPM (R 4.4.0)
#> 
#>  [1] /Users/timothychisamore/Library/R/x86_64/4.4/library
#>  [2] /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

All the best,

Tim