簡體   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