[英]R plot: how to make a histogram with horizontal lines using 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")))
我想要的大概是這樣的,直方圖線是水平的:
我做了
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
之外。
更新感謝@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
這是由此產生的情節(不是很漂亮,但我想我可以從這里做其余的事情):
您可以使用-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軸標簽,請在plot命令中使用ylab=""
並使用text
手動添加
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.