r - 在 ggplot2 : How to add an extra variable with a different scale? 中绘制多个密度

标签 r ggplot2 scaling

我将尝试使用一个简化的示例来描述我的问题。我正在尝试使用 ggplot2 绘制多个密度,例如:

library(reshape2)
library(ggplot2)

set.seed(1)
x <- replicate(5, rnorm(100)) 
colnames(x) <- paste0("x", 1:5)

ggplot(melt(x), aes(value, color = Var2)) + geom_density()
density plot
这按预期工作。现在,我想添加一个具有非常不同比例的新变量:
z <- cbind(x, y = rnorm(100) * 50)
ggplot(melt(z), aes(value, color = Var2)) + geom_density()
density plot with exrta variable
这产生了预期的图表,但不幸的是我不想制作的图表。我想保留两个轴的原始比例,以便前五个密度之间的差异仍然可见,并且新变量的密度将显示为平坦的。
是否有可能以简单的方式做到这一点?就像告诉 ggplot 在不改变缩放比例的情况下覆盖新密度一样?或者在计算轴的限制时告诉 ggplot 忽略变量?
我可以考虑一个手动解决方案,在第一步中保存轴的限制,然后在使用新变量制作图形时指定它们。但这可能不是最优雅的解决方案,并且可能需要大量额外的代码。如果可能的话,我宁愿避免这种解决方案(特别是因为我的情况实际上要复杂得多,并且暗示多个图形 facet_wrap() )
任何建议或提示将是最受欢迎的。提前谢谢了!
PS:为了给你更多的背景信息,我试图根据它们的先验分布(平坦的分布)绘制几个后验分布。我只从先验中提取,而不是它的确切密度函数 - 否则我只会用 stat_function() 覆盖这个先验.

最佳答案

这是改变获得第二个 Y 轴的一种很酷的方法:

首先,让我们以更通用的格式重新创建图表。我将创建一个数据框 X(注意:它是大写的)

X <- data.frame(x)

g <- ggplot()
g <- g + geom_density(data = X, aes(x1, colour= "X1"))
g <- g + geom_density(data = X, aes(x2, colour= "x2"))
g <- g + geom_density(data = X, aes(x3, colour= "x3"))
g <- g + geom_density(data = X, aes(x3, colour= "x4"))

g

产生密度图

enter image description here

好的,现在很酷,让我们重新创建您提到的 z 中的问题
Z <- data.frame(z)

g <- ggplot()
g <- g + geom_density(data = Z, aes(x1, colour= "X1"))
g <- g + geom_density(data = Z, aes(x2, colour= "x2"))
g <- g + geom_density(data = Z, aes(x3, colour= "x3"))
g <- g + geom_density(data = Z, aes(x3, colour= "x4"))
g <- g + geom_density(data = Z, aes(x3, colour= "x4"))
g <- g + geom_density(data = Z, aes(y, colour= "y"))
g

这使

enter image description here

好的,现在我们可以通过中给出的差异对第二个轴进行子集化
z <- cbind(x, y = rnorm(100) * 50)

也就是说,50。但你真的可以按照你想要的比例来制作。
Z <- data.frame(z)

g <- ggplot()
g <- g + geom_density(data = Z, aes(x1, colour= "x1"))
g <- g + geom_density(data = Z, aes(x2, colour= "x2"))
g <- g + geom_density(data = Z, aes(x3, colour= "x3"))
g <- g + geom_density(data = Z, aes(x3, colour= "x4"))
g <- g + geom_density(data = Z, aes(y/50, colour= "y"))
g <- g + scale_y_continuous(sec.axis = sec_axis(~.*50, name= "Y Second Axis"))
g

这给了我们想要的双 y 轴!

enter image description here

你也可以做一个双 x 轴。
g <- ggplot()
g <- g + geom_density(data = Z, aes(x1, colour= "x1"))
g <- g + geom_density(data = Z, aes(x2, colour= "x2"))
g <- g + geom_density(data = Z, aes(x3, colour= "x3"))
g <- g + geom_density(data = Z, aes(x3, colour= "x4"))
g <- g + geom_density(data = Z, aes(y/50, colour= "y"))
g <- g + scale_x_continuous(sec.axis = sec_axis(~.*50, name= "x Second Axis"))
g

enter image description here

希望有帮助!

编辑:

澄清后,看起来想要的结果只是保持原始的 x 轴限制,但添加新的更大的密度。我们可以按照我的风格和您使用的熔化格式来做到这一点。
g <- ggplot()
g <- g + geom_density(data = Z, aes(x1, colour= "x1"))
g <- g + geom_density(data = Z, aes(x2, colour= "x2"))
g <- g + geom_density(data = Z, aes(x3, colour= "x3"))
g <- g + geom_density(data = Z, aes(x3, colour= "x4"))
g <- g + geom_density(data = Z, aes(y, colour= "y"))
g <- g + xlim(range(-4:4))
g

或者
ggplot(melt(z), aes(value, color = Var2)) + geom_density() + xlim(range(-4, 4)

enter image description here

编辑 2:这为我们提供了一个带有正确轴的图,但它从密度图中删除了值(如 Remek 所指出的,以及 Jon Spring 在评论中给出的正确解决方案)。我们能得到一个保持密度值不变的图吗?是的!我们需要 coord_cartesian() 这也将使我们执行“放大”而不是散发超出我们限制的值。

这是两种风格的解决方案:
g <- ggplot()
g <- g + geom_density(data = Z, aes(x1, colour= "X1"))
g <- g + geom_density(data = Z, aes(x2, colour= "x2"))
g <- g + geom_density(data = Z, aes(x3, colour= "x3"))
g <- g + geom_density(data = Z, aes(x3, colour= "x4"))
g <- g + geom_density(data = Z, aes(x3, colour= "x4"))
g <- g + geom_density(data = Z, aes(y, colour= "y"))
g <- g + coord_cartesian(xlim=c(-4, 4))
g

ggplot(melt(z), aes(value, color = Var2)) + geom_density() + coord_cartesian(xlim=c(-4, 4))

两者都会产生预期的结果!
enter image description here

关于r - 在 ggplot2 : How to add an extra variable with a different scale? 中绘制多个密度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52973132/

相关文章:

r - 如何增加ggplot的大小 - 挤压水平条形图

html - div 的高度随缩放级别而变化

Swift-Scale SKAction 不起作用

r - 如何使用 ggplot2 将两个 geom_tile 彼此相邻绘制,以便它们像在热图中一样对齐?

r - 如何从文件中导入保存的 mice/mids 对象

r - 对 R 中的 map 函数感到困惑

r - R语言: How do I print/see summary statistics for sample subset?

r - 添加水平线,按因素分组

R从多列绘制x轴标签

http - 面向服务的架构 - AMQP 或 HTTP