![](/img/trans.png)
[英]how to deal with NA values (connect gaps in a geomline plot ) ggplot
[英]How to plot histogram with extremely widely spread values/gaps in plotly or ggplot
我正在绘制一些观察结果,其中两个或两个以上模式相距很远。 我想有一个情节,可以自动忽略差距。 观察的简化示例是
obs= c(rnorm(100, 0, 1), rnorm(100, sample(c(-1e6, 1e6), 1), 1))
我注意到plotrix
库中的gap.plot()
可以像这样做,但是有什么方法可以在plotly
/ ggplot
而无需手动指定间隙范围? 由于样本均值的随机平均值,我的差距是随机的。
像这样拆分集合怎么样:
obs= c(rnorm(100, 0, 1), rnorm(100, sample(c(-1e6, 1e6), 1), 1))
histN <- function(x, n){
clustRes <- kmeans(x,centers = n)
par(mfrow=c(1,n))
for(i in 1:n){
hist(x[clustRes$cluster==i], xlab="", main = sprintf("subset = %d",i))
}
}
dev.new()
histN(obs,2)
或作为更漂亮但不太灵活的替代方法:
hist2 <- function(x){
clustRes <- kmeans(x,centers = 2)
parDefault <- par()
layout(matrix(c(1,1,1,2,3,3,3),nrow=1))
idx1 <- which.min(clustRes$centers)
idx2 <- which.max(clustRes$centers)
h1 <- hist(x[clustRes$cluster==idx1],plot=FALSE)
h2 <- hist(x[clustRes$cluster==idx2],plot=FALSE)
yRange <- c(min(c(h1$counts,h2$counts)),max(c(h1$counts,h2$counts)))
par(mai = c(parDefault$mai[1:3],0))
par(cex = parDefault$cex)
hist(x[clustRes$cluster==idx1],ylim= yRange, xlab="", ylab ="", main = sprintf("subset = %d",1),axes=FALSE)
axis(1)
axis(2)
par(mai = c(parDefault$mai[1],0,parDefault$mai[3],0))
par(cex = 3)
plot(1:10,type="n", axes=FALSE, xlab="...", ylab ="")
text(x= 5, y= 5, "...")
axis(1, tick=FALSE,labels = FALSE)
par(mai = c(parDefault$mai[1],0,parDefault$mai[3:4]))
par(cex = parDefault$cex)
hist(x[clustRes$cluster==idx2],ylim= yRange, xlab="", ylab ="", main = sprintf("subset = %d",2),axes=FALSE)
axis(1)
axis(4)
}
dev.new()
hist2(obs)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.