我想在R中旋转直方图,由hist()绘制 . 这个问题并不新鲜,在一些论坛上我发现这是不可能的 . 但是,所有这些答案可以追溯到2010年甚至更晚 .
有没有人找到解决方案?
解决问题的一种方法是通过barplot()绘制直方图,提供选项“horiz = TRUE” . 该情节工作正常但我未能在条形图中覆盖密度 . 问题可能在于x轴,因为在垂直图中,密度以第一个仓为中心,而在水平图中,密度曲线混乱 .
很感谢任何形式的帮助!
谢谢,
尼尔斯
码:
require(MASS)
Sigma <- matrix(c(2.25, 0.8, 0.8, 1), 2, 2)
mvnorm <- mvrnorm(1000, c(0,0), Sigma)
scatterHist.Norm <- function(x,y) {
zones <- matrix(c(2,0,1,3), ncol=2, byrow=TRUE)
layout(zones, widths=c(2/3,1/3), heights=c(1/3,2/3))
xrange <- range(x) ; yrange <- range(y)
par(mar=c(3,3,1,1))
plot(x, y, xlim=xrange, ylim=yrange, xlab="", ylab="", cex=0.5)
xhist <- hist(x, plot=FALSE, breaks=seq(from=min(x), to=max(x), length.out=20))
yhist <- hist(y, plot=FALSE, breaks=seq(from=min(y), to=max(y), length.out=20))
top <- max(c(xhist$counts, yhist$counts))
par(mar=c(0,3,1,1))
plot(xhist, axes=FALSE, ylim=c(0,top), main="", col="grey")
x.xfit <- seq(min(x),max(x),length.out=40)
x.yfit <- dnorm(x.xfit,mean=mean(x),sd=sd(x))
x.yfit <- x.yfit*diff(xhist$mids[1:2])*length(x)
lines(x.xfit, x.yfit, col="red")
par(mar=c(0,3,1,1))
plot(yhist, axes=FALSE, ylim=c(0,top), main="", col="grey", horiz=TRUE)
y.xfit <- seq(min(x),max(x),length.out=40)
y.yfit <- dnorm(y.xfit,mean=mean(x),sd=sd(x))
y.yfit <- y.yfit*diff(yhist$mids[1:2])*length(x)
lines(y.xfit, y.yfit, col="red")
}
scatterHist.Norm(mvnorm[,1], mvnorm[,2])
scatterBar.Norm <- function(x,y) {
zones <- matrix(c(2,0,1,3), ncol=2, byrow=TRUE)
layout(zones, widths=c(2/3,1/3), heights=c(1/3,2/3))
xrange <- range(x) ; yrange <- range(y)
par(mar=c(3,3,1,1))
plot(x, y, xlim=xrange, ylim=yrange, xlab="", ylab="", cex=0.5)
xhist <- hist(x, plot=FALSE, breaks=seq(from=min(x), to=max(x), length.out=20))
yhist <- hist(y, plot=FALSE, breaks=seq(from=min(y), to=max(y), length.out=20))
top <- max(c(xhist$counts, yhist$counts))
par(mar=c(0,3,1,1))
barplot(xhist$counts, axes=FALSE, ylim=c(0, top), space=0)
x.xfit <- seq(min(x),max(x),length.out=40)
x.yfit <- dnorm(x.xfit,mean=mean(x),sd=sd(x))
x.yfit <- x.yfit*diff(xhist$mids[1:2])*length(x)
lines(x.xfit, x.yfit, col="red")
par(mar=c(3,0,1,1))
barplot(yhist$counts, axes=FALSE, xlim=c(0, top), space=0, horiz=TRUE)
y.xfit <- seq(min(x),max(x),length.out=40)
y.yfit <- dnorm(y.xfit,mean=mean(x),sd=sd(x))
y.yfit <- y.yfit*diff(yhist$mids[1:2])*length(x)
lines(y.xfit, y.yfit, col="red")
}
scatterBar.Norm(mvnorm[,1], mvnorm[,2])
#
具有边缘直方图的散点图的来源(点击“改编自......后”的第一个链接):
散点图中的密度来源:
5 回答
知道
hist()
函数使用更简单的绘图函数(如rect()
)无形地返回重现其所需信息的所有信息可能会有所帮助 .您可以手动创建相同的直方图,如下所示:
使用这些部件,您可以随意翻转轴:
对于与
density()
类似的自己动手,请参阅:Axis-labeling in R histogram and density plots; multiple overlays of density plots我不确定它是否有意义,但我有时想要使用没有任何包装的水平直方图,并且能够在图形的任何位置书写或绘图 .
这就是我编写以下函数的原因,下面提供了示例 . 如果有人知道这个包适合的包,请写信给我:berry-b at gmx.de
请确保不要在工作区中使用变量hpos,因为它将被函数覆盖 . (是的,对于包,我需要在函数中插入一些安全部件) .
举些例子
一个缺点:该功能不适用于作为具有不同宽度的条形的矢量提供的断点 .
谢谢蒂姆和保罗 . 你让我更加思考并使用hist()实际提供的东西 .
这是我的解决方案(在Alex Pl的帮助下):
举些例子:
不对称的Sigma导致相应轴的稍微更大的直方图 .
代码留下故意“不雅”,以增加可理解性(当我稍后再次访问时,我自己......) .
尼尔斯
使用ggplot时,翻转轴非常有效 . 例如,参见this example,它显示了如何为箱形图执行此操作,但它对于我假设的直方图同样有效 . 在ggplot中,可以很容易地在ggplot2术语中叠加不同的绘图类型或几何 . 因此,结合密度图和直方图应该很容易 .