2014-10-05 88 views
0

我剛剛開始使用R,我使用RStudio。Y軸不顯示R中的標籤名稱

現在我正在玩情節,看看它們是如何工作的。

這裏是我的數據:

https://www.dropbox.com/s/wp4gs8qamdo1irk/State_Zhvi_Summary_AllHomes.csv?dl=0

我一直在試圖繪製barplot爲$ Zhvi對$ RegionName

這裏是我的代碼:

labels <- allHomet$RegionName 
mp<-barplot(round((allHomet$Zhvi)/1000,digits=2)[order(labels)],horiz=TRUE,las=1,col=c(palette(heat.colors(6,alpha=1))), 
     border=NA, 
     main="Price of housing in United states", 
     xlab="Price", 
     las=2)! 

here is the plot I get

我不明白y軸上的離子名稱,它顯示爲空白。

任何人都可以告訴我如何讓區域名稱出現嗎?

+0

看一看在「names.arg」 – 2014-10-05 23:54:53

+0

是它填充名字由niw它搞砸了,這裏是plothttps://www.dropbox.com/s/g7fxxbzy50bhhsh/Rplot01.png?dl = 0 – Snedden27 2014-10-06 00:27:53

+0

嘗試使用'png'(或'jpeg','pdf',等等)函數並且調整尺寸(參數'hight'和'width')。設置較低的'cex'也可能有幫助。 – 2014-10-06 01:48:53

回答

1

這是一個ggplot2非常適合處理的問題。它的靈活性和模塊化可以很容易地頂起圖這樣在任何時間:「?barplot」

library(ggplot2) # load graphics package 
head(df) 
fl <- as.character(rep(1:6, length.out = nrow(df))) # fill variable - anything here 
ggplot(df) + 
    geom_bar(aes(x = RegionName, y = Zhvi, fill = fl), stat = "identity", position = "dodge") + 
    scale_fill_brewer(type = "qual", name = "") + # customise colours and legend title 
    coord_flip() # make bars horizontal 

enter image description here

+0

我試過這個給我這個錯誤: 錯誤:將變量映射到y,也使用stat =「bin」。 當stat =「bin」時,它將嘗試將y值設置爲每個組中的個案數。 這可能會導致意外的行爲,並且不會在未來版本的ggplot2中允許。 如果您希望y表示個案的計數,請使用stat =「bin」並且不要將變量映射到y。 如果您希望y表示數據中的值,請使用stat =「identity」。 請參閱?geom_bar以獲取示例。 (Defunct;最近在版本0.9.2中使用) – Snedden27 2014-10-05 23:53:12

+0

啊哈 - ggplot2最近跳到1.0版,導致上述代碼失敗:http://cran.r-project.org/web/packages/ggplot2/vignettes/release .html。簡單的解決方案:在'fill = fl'後面加''stat =「identity」,''。我已經更新了包含這個的答案。 – RobinLovelace 2014-10-06 08:32:38