1

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

jrcalabrese
  • 2,184
  • 3
  • 10
  • 30
  • 2
    Can you make your post [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and provide your data using `dput()`? Please avoid posting images of data. – jrcalabrese Jan 17 '23 at 17:21

1 Answers1

1

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))

stefan
  • 90,330
  • 6
  • 25
  • 51