library(tidyverse)
df <- data.frame(x = c(1, 2, 3, 4),
y1 = c(1, 3, 2, 4),
y2 = c(3000, 2000, 4000, 1000))
label_1 = c("1", "2", "3") %>% format(width = 5, justify = "right")
label_2 = c("1000", "2000", "3000") %>% format(width = 5, justify = "right")
p1 <- df %>% ggplot(aes(x = x, y = y1))+
geom_line()+
scale_y_continuous(breaks = c(1, 2, 3),
labels = label_1)
p2 <- df %>% ggplot(aes(x = x, y = y2))+
geom_line()+
scale_y_continuous(breaks = c(1000, 2000, 3000),
labels = label_2)
p1
p2
In the above code, I try to make p1
and p2
to have the same length of y-axis
label, by format(width = 5)
. In the actual graph, labels in p1
are still shorter than those in p2
.
By trial and error, I get the right length when setting width =8
.
label_3 = c("1", "2", "3") %>% format(width = 8, justify = "right")
p1 <- df %>% ggplot(aes(x = x, y = y1))+
geom_line()+
scale_y_continuous(breaks = c(1, 2, 3),
labels = label_3)
p1
Could someone please explain this or guide me to the relevant previous posts?