使用 R 中的plot()更改X轴上的标签

Aru*_*run 2 plot r

我是 R 的初学者,正在处理如下一些数据-

月份 <- 1 2 3 4 5 6 7 8 9 10 11 12

销售额 <- 50 60 80 50 40 30 35 55 70 60 50 40

我必须使用plot()函数来绘制这些数据,我可以通过对其进行某些编辑来完成,如下所示-

plot(Month, Coffee, type='l', ylim=c(0, 100))
abline(h=max(Coffee), col='red')
Run Code Online (Sandbox Code Playgroud)

新的要求是将 X 轴上的“月份”名称绘制为实际月份名称,即一月、二月、三月……、十二月。

我尝试使用-

n_month <- format(ISOdate(2017,1:12,1),"%B")
plot(n_month, Coffee, type='l', ylim=c(0, 100))
plot(n_month, Coffee, type='l', ylim=c(0, 100), xlim=(1, 12))
Run Code Online (Sandbox Code Playgroud)

我还尝试了以下方法 -

# 'xaxt' to suppress labels on x-axis
plot(Month, Coffee, ylim=c(0,100), xlab="Month", ylab="Coffee", xaxt="n", type='l')

# 'axis' to add in my own labels
axis(1, at=1:12, labels=month.name)
Run Code Online (Sandbox Code Playgroud)

然而,最终图表并未提及所有名称,而是提及了某些名称,例如一月、三月、五月、六月、七月、九月和十一月。

关于如何获取 X 轴上所有 12 个月的名称,有什么建议吗?

谢谢!

Aru*_*run 6

我用-解决了它

axis(1, at=1:12, labels=month.name, cex.axis=0.5)
Run Code Online (Sandbox Code Playgroud)

'cex.axis' 参数起到了调整 X 轴上所有月份名称的作用。