归一化ggplot中覆盖密度图的x比例 [英] Normalising the x scales of overlaying density plots in ggplot
问题描述
在覆盖具有相同长度但比例尺不同的数据的ggplot密度图时,是否可以将这些图的x比例尺归一化,以使密度匹配?另外,有没有一种方法可以对密度y标度进行归一化?
When overlaying ggplot density plots that feature data of same length but different scales is it possible to normalise the x scale for the plots so the densities match up? Alternatively is there a way to normalise the density y scale?
library(ggplot2)
data <- data.frame(x = c('A','B','C','D','E'), y1 = rnorm(100, mean = 0, sd = 1),
y2 = rnorm(100, mean = 0, sd = 50))
p <- ggplot(data)
# Overlaying the density plots is a fail
p + geom_density(aes(x=y1), fill=NA) + geom_density(aes(x=y2), alpha=0.3,col=NA,fill='red')
# You can compress the xscale in the aes() argument:
y1max <- max(data$y1)
y2max <- max(data$y2)
p + geom_density(aes(x=y1), fill=NA) + geom_density(aes(x=y2*y1max/y2max), alpha=0.3,col=NA,fill='red')
# But it doesn't fix the density scale. Any solution?
# And will it work with facet_wrap?
p + geom_density(aes(x=y1), col=NA,fill='grey30') + facet_wrap(~ x, ncol=2)
谢谢!
推荐答案
这是否符合您的期望?
p + geom_density(aes(x=scale(y1)), fill=NA) +
geom_density(aes(x=scale(y2)), alpha=0.3,col=NA,fill='red')
仅具有单个数据参数的scale
函数将经验分布的中心放在0上,然后将结果值除以样本标准偏差,因此结果的标准偏差为1.您可以更改位置的默认值以及压缩"或扩展"的程度.您可能需要研究为y1和y2设置适当的x_scales.这可能需要进行一些规模化的预处理.比例因子记录在返回对象的属性中.
The scale
function with only a single data argument will center an empiric distribution on 0 and then divide the resulting values by the sample standard deviation so the result has a standard deviation of 1. You can change the defaults for the location and the degree of "compression" or "expansion". You will probably need to investigate putting in appropriate x_scales for y1 and y2. This may take some preprocessing with scale. The scaling factor is recorded in an attribute of the returned object.
attr(scale(data$y2), "scaled:scale")
#[1] 53.21863
这篇关于归一化ggplot中覆盖密度图的x比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!