2012-01-05 210 views
1

如何使用plot將這個繪圖橫向調整,使得直方圖條是水平的?R plot:如何使用plot繪製水平線的直方圖()

size<- abs(rnorm(20,10,10)) 
age<-c(seq(1, 20, by=2)) 
plot(size~age, type=("n")); lines(size~age, type=c("l"), lines(size~age, type=c("h"))) 

enter image description here

我要的是大致這樣的事情,與直方圖線的水平:

enter image description here

我與

plot(size~age, type=("n"), yaxt="n", xaxt="n", ylab=""); lines(size~age, type=c("l"), lines(size~age, type=c("h"))); axis(4); axis(1,las=2) 

,然後旋轉做在其他軟件中輸出圖像。

我想知道我怎麼可以使用plot函數來獲取輸出情節橫盤,所以我可以讓他們的小組在R不必的R外旋轉它們。

UPDATE感謝來自@csgillespie非常有幫助的建議,我已經得到了這一點,這也讓我對我的方式:

size<- abs(rnorm(20,10,10)) 
age<-c(seq(1, 40, by=2)) # sorry for the typo in the first set of example data above 
plot(-age~size, type="n",yaxt="n", ylab="Age", xlab="Size") 
lines(-age~size) 
segments(0, -age, size, -age) 
axis(2, labels=c(seq(0,max(age), by=5)), at=-c(seq(0,max(age), by=5)), las=1) # this is a more general approach to labelling the axis ticks 

,這裏是導致情節(不漂亮,但我想我從這裏可以做其他):

enter image description here

+0

手動添加它進一步說明您可能會發現在這裏:http://stackoverflow.com/questions/ 3792803/is-it-it-to-rotate-a-plot-in-r-base-graphics – Seb 2012-01-05 10:26:02

回答

3

你可以得到你想要的東西通過-age然後手動添加的規模。

plot(-age~size, type="n",yaxt="n", xlab="", ylab="Age") 
lines(-age~size) 
segments(0, -age, size, -age) 
axis(2, labels=c(0,5,10,15,20), at=-c(0,5,10,15,20), las=1) 

上面的代碼產生了一個與您示例圖相同的圖,除了y軸標籤已被旋轉。如果你想y軸標籤旋轉,然後在繪圖命令使用ylab=""text

enter image description here

+0

非常感謝!我不知道這是一個簡單的改變功能的標誌。我之前也沒有遇到'segments',所以這非常有幫助。我採用了更爲通用的方法來標記刻度標記(請參閱我的更新),這對我的真實數據更有效。再次感謝 – Ben 2012-01-05 18:46:30