简体   繁体   English

R plot:如何使用plot()制作带水平线的直方图

[英]R plot: how to make a histogram with horizontal lines using plot()

How can I use plot to turn this plot sideways so the histogram bars are horizontal? 如何使用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")))

在此输入图像描述

What I want is roughly something like this, with the histogram lines horizontal: 我想要的大概是这样的,直方图线是水平的:

在此输入图像描述

which I did with 我做了

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)

and then rotating the image output in other software. 然后在其他软件中旋转图像输出。

I'd like to know how I can use the plot function to get the output plot sideways so I can make groups of them in R without having to rotate them outside of R . 我想知道如何使用plot功能来获取输出图,因此我可以在R制作它们的组,而不必将它们旋转到R之外。

UPDATE Thanks to the very helpful suggestion from @csgillespie I've got this, which has got me on my way: 更新感谢@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

and here's resulting plot (not pretty yet, but I think I can do the rest from here): 这是由此产生的情节(不是很漂亮,但我想我可以从这里做其余的事情):

在此输入图像描述

You can get what you want by using -age then adding the scale manually. 您可以使用-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)

The code above produces an identical plot to you example figure, except, the y-axis label has been rotated. 上面的代码为您的示例图生成了相同的图,但y轴标签已旋转。 If you want the y-axis label to be rotated, then use ylab="" in the plot command and add it manually with text 如果要旋转y轴标签,请在plot命令中使用ylab=""并使用text手动添加

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM