繁体   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