简体   繁体   中英

X axis labels begin at the middle and do not match data points

I either seem to be running in circles or there is something wrong with my data. I want to plot some data and use ?axis to modify the labels on the X axis.

I have two issues however:

  1. The axis labels begin in the center of the x-axis instead of at the beginning
  2. The axis labels do not match the data points in the plot

I would like to have X axis labels ranging from 10 to 90 by 5.

This is the code that I use and came up with so far:

values <- cbind(1:180,1)

l <-    list(1:10,11:20,21:30,31:40,41:50,51:60,61:70,71:80,81:90,91:100,101:110,111:120,121:130,131:140,141:150,151:160,161:170,171:180)
# compute mean across the intervals in l
meanqual <- sapply(l, function(x) mean(values[x,1]))
meanqual
plot <- plot(meanqual, type="o", xlab="% Size of Wave", ylab="Values",xaxt='n', lty=1)
legend('bottomright', c("Values"),pch=21, lty=1, cex=1)
axis(side=1, at= seq(10,90,5))

我从中心开始用x轴标签得到的图

If you only give one numeric vector to plot it "assumes" you meant to use the position or index of the values in that vector as the x values, so the plot call plotted meanqual against 1:length(meanqual) . If you wanted to plot against the seq() argument you latter used in the axis call you should supply it (or rather something similar in scale with the same length as meanqual ) to plot :

plot <- plot(x=seq(5,90,5), y=meanqual, type="o", 
             xlab="% Size of Wave", ylab="Values",xaxt='n', lty=1)
legend('bottomright', c("Values"),pch=21, lty=1, cex=1)
axis(side=1, at= seq(10,90,5), labels=seq(10,90,5))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM