I have a question. Since i’m very new to R and data visualisation, I hope I can ask correctly.
I’m trying to visualize a 0 – 100% simple barchart but the part between 60 – 100% is the most interesting to zoom in to. I’ve tried a few things to change the steps of the Y axis like this:
0 , 20 , 40 , 60 , 70 , 80 , 90 , 100 .
The goal is to see like the whole graph from 0 – 100 %, but just to visualize the (in the images RED) difference between Rejected
by Year
more. Since the difference is only 4%.
I did try with scale_y_continuous
to change the breaks and it did change, but not the way i’m looking for:
The result is R just showing the labels like I asked, but without changing the scales of the axis.
It’s giving me this:
What I want to achieve is this:
I also did try with scale_y_break(c(0, 60))
but then the 0 – 60% disappears.
Dummy data:
status count % year
Rejected 1049 14% 2024
Approved 6621 86% 2024
Rejected 1963 18% 2023
Approved 8781 82% 2023
Is there a way to customize the Y axis labels by also changing the scales?
Many thanks in advance from this newbie.
Rosemarie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
This is how I would do it:
library(tidyverse)
df <- tribble(
~status, ~count, ~pct, ~year,
"Rejected", 1049, 0.14, 2024,
"Approved", 6621, 0.86, 2024,
"Rejected", 1963, 0.18, 2023,
"Approved", 8781, 0.82, 2023
) %>%
mutate(
year = factor(year, levels = c(2023, 2024)),
status = factor(status, levels = c("Rejected", "Approved"))
)
df %>%
ggplot(aes(y = year, x = pct, fill = status)) +
geom_bar(stat = "identity", orientation = "y", position = position_stack(reverse = FALSE))+
scale_x_continuous(
breaks = c(seq(0, .5, 0.1), seq(0.6, 1, .2)),
labels = scales::label_percent(accuracy = 1, scale = 100)
)+
scale_fill_manual(values = c("Approved" = "#016599", "Rejected" = "#990001"))