Home > Software design >  Chance number of digits on the x-axis
Chance number of digits on the x-axis

Time:01-18

I try to plot price agains year. year is formated as integer, but is ploted with one digit. How can I change that? x-axis without digit.

df looks like this:

enter image description here

now I use this code to plot:

ggplot(df, aes(Jahr, Energiepreis))   geom_line()

plot looks like this

enter image description here

I tried with scale_x_continous() but with no success so far

CodePudding user response:

By default ggplot2 will choose approx. 5 breaks for a continuous variable, which quite often works fine but as your values are years I would opt for setting the desired breaks explicitly using the breaks argument, e.g. to add a break for every second year you could do:

df <- data.frame(
  Jahr = 2020:2030,
  Energiepreis = 1:11
)

library(ggplot2)

ggplot(df, aes(Jahr, Energiepreis))   
  geom_line()  
  scale_x_continuous(breaks = seq(2020, 2030, 2))

  • Related