buhtz buhtz - 1 year ago 102
R Question

Modify the steps of the x-axis in R

The question is simple and I found some similar question around the web. But in my special case I can not get the point. ;)

See this example

df = data.frame(Year=c(2012,2013,2014), N=c(61175,52477,48699))
plot(df[,c('Year', 'N')], type='o')

The problem in this resulting plot-picture is the stepping on the x-axis. e.g. I don't want "2012.5".


Ok, I know that to draw the plot without a x-axis description.

plot(df[,c('Year', 'N')], xaxt='n', type='o')

But now I have the problem how to use
to add correct values there.
I tried a lot of variants.

axis(1, xaxp=c('2012','2013','2014'))
axis(1, 1:3, xaxp=c('2012','2013','2014'))
axis(1, 1:3, xaxp=df$Year)

Answer Source

You want:

plot(df[,c('Year', 'N')], xaxt='n', type='o')
axis(1, at = df$Year, labels = df$Year)

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download