Home > Software engineering >  Changing label ggplot2 to display integers and not duplicates
Changing label ggplot2 to display integers and not duplicates

Time:08-10

library(ggplot2)
library(scales)
df = data.frame(n = as.integer(c(2, 1)), date = as.Date(c("2015-01-01", "2020-01-01")))
ggplot(df, aes(x  = date, y = n))  
geom_line()

How can I (in a generic way) change the y-label so it only displays integers and not duplicates of the label? For example if I use scale_y_continuous(labels = scales::label_comma(accuracy = 1)) I will get duplicate values of 1 and 2 in the label.

CodePudding user response:

You could simply tell it only to use integers as breaks and give it an arbitrarily high number to theoretically go up to:

library(ggplot2)
library(scales)

df <-  data.frame(n = as.integer(c(2, 1)), date = as.Date(c("2015-01-01", "2020-01-01")))

ggplot(df, aes(x  = date, y = n))  
  geom_line()  
  scale_y_continuous(breaks = 1:100)

Or sequence along the min:max values in your dataset by 1s (I think I'd recommend this approach - can also set bottom to 0 or 1 if preferred):

scale_y_continuous(breaks = seq(min(df$n), max(df$n), 1))
# same result

Or if you have every integer once as a value in the n variable you could have n.breaks set to number of rows:

scale_y_continuous(n.breaks = nrow(df))
# same result, unless repetition of values or gaps in values

Or pass a function to breaks to select all unique values after rounding:

scale_y_continuous(breaks = ~ unique(round(.x)))
# Again, same result only if no gaps in values
  • Related