2017-06-17 95 views
0

我想绘制带有阴影区域(2个X值之间)的直方图,但没有垂直常见线条(断点之间的线条)和abline。我有以下示例代码:用阴影区域绘制直方图并在R中没有公共边框

x<-rnorm(n=100, m=0, sd=1) 
h<- hist(x, breaks=50) 
cuts<- cut(h$breaks, c(-1, 1)) 
plot(h, col="green"[cuts]) 
abline(v=mean(x), lty=2, lwd=2)` 

非常感谢你,提前。

+0

如果用'BORDER =“绿色” [削减]剧情'你会得到固体绿柱无明显分歧,但你会在的顶部失去了整体轮廓你可能想保留的直方图。我认为保持这一点的唯一方法是从bin数据中找出它并使用'lines' ... – Spacedman

回答

0

试试这个(从你的h创建如下):

np = length(h$breaks) 
x = c(h$breaks[1],rep(h$breaks[-c(1,np)],rep(2,(np-2))), h$breaks[np]) 
y=rep(h$counts,rep(2,length(h$counts))) 

现在x和y是 “天际线” 的坐标。试试这个:

plot(h) 
lines(x,y, col="red", lwd=2) 

因此,对于你的情节:

# make the plot 
plot(h) 
# fill the area with green fill and green outline 
plot(h, col="green"[cuts], border="green"[cuts],add=TRUE) 
# restore the skyline 
lines(x,y) 
abline(v=mean(x), lty=2, lwd=2) 

,并提供:

enter image description here

+0

非常感谢所有人! –

0

我用一个更粗糙的方法。我创建了两个直方图,然后我重叠它们。

h <- hist(x, breaks=50, plot=FALSE) 
cuts <- cut(h$breaks, c(-1, 1)) 
plot(h$breaks, c(h$counts,0) ,type="s",col="black", lwd=2) 
plot(h, col="gray"[cuts], lty="blank", add=T) 
abline(v=mean(x), lty=2, lwd=2) 

enter image description here