繁体   English   中英

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

[英]Change labels in X axis using plot() in 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')

新要求是在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))

我还尝试了以下方法-

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

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

关于如何获得X轴上所有12个月的名称的任何建议?

谢谢!

我希望它对你有用

month <- c("Jan", "Feb", "Mar",
           "Apr", "May", "Jun",
           "Jul", "Agu","Sep", 
           "Oce", "Nov", "Dec")
sales <- c(50, 60, 80, 50, 40, 30, 35, 55, 70, 60, 50, 40)
dt<-data.frame( Month<-month, Sales=sales   )


library(ggplot2)
ggplot(dt ,aes( Month,Sales)) + 
  geom_bar(stat="identity",aes(fill = "Month"), data =dt , alpha = 0.5 )  

只要确保您的系统中有ggplot2

最后会是这样的:

在此处输入图片说明

我用-解决了

axis(1, at=1:12, labels=month.name, cex.axis=0.5)

'cex.axis'参数完成了在X轴上调整所有月份名称的技巧。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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