2013-08-20 3 views
1

다음 막대 차트를 어떻게 바꿀 수 있습니까?분할 막대 차트

1) 막대 사이에 공백이 없어야합니다. 2) 큰 막대가 작은 막대로 채워지면 누적 막대의 색상이 보일 것입니다.

tim.seq<-seq(as.POSIXlt("2013-07-01 00:00:00",origin = "1960-01-01",tz="GMT"), 
      as.POSIXlt("2013-07-8 00:00:00",origin = "1960-01-01",tz="GMT"),by="1 day") 

library(ggplot2) 
times<-rep(tim.seq,2) 
key<-rep(LETTERS[1:2],each=length(times)/2) 
times<-c(times,times) 
key<-c(key,key) 
ref<-rep(LETTERS[1:2],each=length(times)/2) 
value<-to<-sample(1:20,length(times),T) 
df<-data.frame(times=times,key=key,ref=ref,value=value) 

p<-ggplot(df, aes(x=times)) 
p<-p + geom_bar(subset = .(ref =="A"), aes(y = value, fill = key), stat = "identity") 
p<-p + geom_bar(subset = .(ref =="B"), aes(y = -value, fill = key), stat = "identity") 
p<-p + geom_hline(yintercept = 0, colour = "grey90") 
p 
+1

두 번째 사항을 이해할 수 없습니다. – csgillespie

+0

각각의 스택 된 막대는 녹색 막대와 빨간색 막대의 전체 양을 나타내는 막대와 빨간색 막대를 나타내는 작은 막대로 대체해야합니다. 막대는 작은 막대가 채워지는 형태로 오버레이되어야합니다 glas에있는 물 같이 더 키 큰 것. – Klaus

답변

1

그것은 당신이 스택 막대를 원하지 않는 것처럼 보이고, 대신 다른 폭의 막대를 원한다. 그걸 얻기 위해서는 서로 다른 width 매개 변수를 실험해야하고 position="identity"

매 시간마다 네 가지 조합이 가능합니다 : key = {A or B} and Ref = {A or B}

#create a new column to have Bar width be based on the Key 
df$w <- ifelse(key=="A", 5, 3) 

p <- ggplot(df, aes(x=times, y=value)) 
p <- p + geom_bar(data = subset(df, ref =="A"), aes(width=w*10000, fill=key), stat = "identity", position = "identity") 
p <- p + geom_bar(data = subset(df, ref =="B"), aes(width=w*5000, fill=key), colour="black", stat = "identity", position="identity") 

이 생산 : 봐, 여기 당신이 실험을 할 수있는 던져 - 멀리 그래프enter image description here 을 그러나 이것은 당신이 원하는 것을 확실히 하지입니다. position = "identity", position = "dodge"및 다양한 너비 값을 가지고 놀 수 있다는 것을 보여 주기만하면됩니다. ggplot에서 요약 데이터 프레임

만들기

, 당신은 거의 항상 데이터 프레임 자체를 처리하여 원하는 것을 얻을 수 있습니다. enter image description here

희망이 가까이 당신이 무슨에 당신을 가져옵니다 여기에 심판 생산 "A"

library(plyr) 

#Create a summary data frame with y-value Totals 
new.df <- ddply(df, .(times), summarize, total=sum(value)) 

#add a column to sum up values when Ref is "A" 
ref.df <- ddply(df, .(times, ref), summarize, reftotal = sum(value)) 
new.df$reftotal <- ref.df$reftotal[ref.df$ref=="A"] #filter to the Ref value we are interested in 

p2 <- ggplot(new.df, aes(x=times)) 
p2 <- p2 + geom_bar(aes(y=total, width=50000), stat = "identity", position = "identity", fill="darkgreen") 
p2 <- p2 + geom_bar(aes(y=reftotal, width=30000), stat = "identity", position = "identity", fill="red") 
p2 <- p2 + geom_hline(yintercept = 0, colour = "blue") 
p2 

이 때 값을 추가 또한 Y-합계를 보유하고 요약 데이터를 또 다른 시도이다 마음.