簡體   English   中英

R plot:如何使用plot()制作帶水平線的直方圖

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM