[英]How to overlay density plots in R?
我想用 R 在同一台设备上叠加 2 个密度图。我该怎么做? 我在网上搜索,但没有找到任何明显的解决方案。
我的想法是从文本文件(列)中读取数据,然后使用
plot(density(MyData$Column1))
plot(density(MyData$Column2), add=T)
或者本着这种精神的东西。
使用第二lines
:
plot(density(MyData$Column1))
lines(density(MyData$Column2))
不过,请确保第一个图的限制是合适的。
添加处理 y 轴限制的基本图形版本,添加颜色并适用于任意数量的列:
如果我们有一个数据集:
myData <- data.frame(std.nromal=rnorm(1000, m=0, sd=1),
wide.normal=rnorm(1000, m=0, sd=2),
exponent=rexp(1000, rate=1),
uniform=runif(1000, min=-3, max=3)
)
然后绘制密度:
dens <- apply(myData, 2, density)
plot(NA, xlim=range(sapply(dens, "[", "x")), ylim=range(sapply(dens, "[", "y")))
mapply(lines, dens, col=1:length(dens))
legend("topright", legend=names(dens), fill=1:length(dens))
这使:
只是为了提供一个完整的集合,这是 Chase 使用lattice
的答案的一个版本:
dat <- data.frame(dens = c(rnorm(100), rnorm(100, 10, 5))
, lines = rep(c("a", "b"), each = 100))
densityplot(~dens,data=dat,groups = lines,
plot.points = FALSE, ref = TRUE,
auto.key = list(space = "right"))
这产生了这样的情节:
这就是我在 base 中的做法(实际上在第一个答案评论中提到过,但我将在此处显示完整代码,包括图例,因为我还不能评论......)
首先,您需要从密度图中获取 y 轴最大值的信息。 所以你需要首先分别计算密度
dta_A <- density(VarA, na.rm = TRUE)
dta_B <- density(VarB, na.rm = TRUE)
然后根据第一个答案绘制它们并为您刚刚获得的 y 轴定义最小值和最大值。 (我将最小值设置为 0)
plot(dta_A, col = "blue", main = "2 densities on one plot"),
ylim = c(0, max(dta_A$y,dta_B$y)))
lines(dta_B, col = "red")
然后在右上角添加图例
legend("topright", c("VarA","VarB"), lty = c(1,1), col = c("blue","red"))
我拿上面的格子例子做了一个漂亮的函数。 可能有更好的方法通过熔化/铸造重塑。 (如果您看到改进,请发表评论或编辑。)
multi.density.plot=function(data,main=paste(names(data),collapse = ' vs '),...){
##combines multiple density plots together when given a list
df=data.frame();
for(n in names(data)){
idf=data.frame(x=data[[n]],label=rep(n,length(data[[n]])))
df=rbind(df,idf)
}
densityplot(~x,data=df,groups = label,plot.points = F, ref = T, auto.key = list(space = "right"),main=main,...)
}
用法示例:
multi.density.plot(list(BN1=bn1$V1,BN2=bn2$V1),main='BN1 vs BN2')
multi.density.plot(list(BN1=bn1$V1,BN2=bn2$V1))
每当出现轴限制不匹配的问题时, base
图形中的正确工具是使用matplot
。 关键是利用from
和to
参数来传递给density.default
。 这有点hackish,但自己滚动相当简单:
set.seed(102349)
x1 = rnorm(1000, mean = 5, sd = 3)
x2 = rnorm(5000, mean = 2, sd = 8)
xrng = range(x1, x2)
#force the x values at which density is
# evaluated to be the same between 'density'
# calls by specifying 'from' and 'to'
# (and possibly 'n', if you'd like)
kde1 = density(x1, from = xrng[1L], to = xrng[2L])
kde2 = density(x2, from = xrng[1L], to = xrng[2L])
matplot(kde1$x, cbind(kde1$y, kde2$y))
根据需要添加花里胡哨( matplot
接受所有标准的plot
/ par
参数,例如lty
、 type
、 col
、 lwd
,...)。
您可以使用ggjoy
包。 假设我们有三个不同的beta
分布,例如:
set.seed(5)
b1<-data.frame(Variant= "Variant 1", Values = rbeta(1000, 101, 1001))
b2<-data.frame(Variant= "Variant 2", Values = rbeta(1000, 111, 1011))
b3<-data.frame(Variant= "Variant 3", Values = rbeta(1000, 11, 101))
df<-rbind(b1,b2,b3)
您可以获得三种不同的分布,如下所示:
library(tidyverse)
library(ggjoy)
ggplot(df, aes(x=Values, y=Variant))+
geom_joy(scale = 2, alpha=0.5) +
scale_y_discrete(expand=c(0.01, 0)) +
scale_x_continuous(expand=c(0.01, 0)) +
theme_joy()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.