2016-04-05 53 views
0

如何在R中做這種直方圖? expected特定直方圖R

我試圖

plot(df$Value, type = 'l') 
polygon(df$Value, col='red') 

但結果不是要緊

result

+0

請重複的例子'積(X < - C(O,runif(100,-1,1),0),類型= 'N');多邊形(x,col ='red',border = NA)' – rawr

回答

1

只是爲了提供了另一種可能的方法,你可以BU使用基本R圖形相當容易地繪製該圖。我通常更喜歡使用基本R圖形手動構建圖,因爲它可以精確控制圖的所有方面。此外,我認爲,當你花時間考慮從原始圖形元素到如何構建這些情節時,它會完全揭開它們的神祕面紗,我的意思是,你發展了一種理解,即他們在概念和圖形結構。在這種特殊情況下,我們可以使用向segments()的單個矢量化調用創建主要圖形元素(參考直方圖條本身)。

## generate data 
set.seed(5468L); 
N <- 7700L; 
df <- data.frame(Value=round(cumsum(c(0.07,runif(N-1L,-0.001,0.001))),5L)); 

## precompute plot parameters 
xlim <- c(1L,N); 
ylim <- c(-0.08,0.1); 
xticks <- seq(xlim[1L],xlim[2L],276L); 
yticks <- seq(ylim[1L],ylim[2L],0.02); 

## plot 
plot(NA,xlim=xlim,ylim=ylim,xaxs='i',yaxs='i',axes=F,ann=F); ## set range, nothing else 
abline(h=yticks,col='lightgrey'); ## horizontal grid lines 
segments(seq_len(N),0,y1=df$Value,lwd=0.3,col='#5599CC'); ## histogram bars 
text(xticks,-0.005,xticks,adj=c(1,0.5),col='#666666',srt=90,xpd=T); ## custom x-axis in plot 
mtext(yticks,2L,0.5,at=yticks,adj=1,las=1L,col='#666666'); ## custom y-axis on margin 

plot

0

兩個解決方案GGPLOT2

a <- arima.sim(list(ar=.9), n = 200)#simulated data 
df <- data.frame(x = 1:200, a = as.vector(a)) 

require(ggplot2) 
ggplot(df, aes(x = x, y = a)) + geom_area() 
ggplot(df, aes(x = x, y = a)) + geom_bar(stat = "identity", width = 1, position = "dodge") 
+0

它的工作原理。謝謝! –