2015-10-30 50 views
0
library(plyr) 
library(ggplot2) 

period <- c("period 0","period 1","period 1","period 1","period 1","period 1","period 2","period 2","period 2","period 2","period 2","period 2","period 2", "period 2","period 2","period 3","period 3","period 3","period 3","period 3","period 3","period 3.5","period 3.5","period 3.5","period 4","period 4", "period 4","period 4","period 4","period 4","period 4","period 5","period 5","period 5","period 5","period 5","period 5","period 5","period 6","period 7") 

teacher <- c("17","14","17","20","22","23","13","15","17","19","20","22", "23","24","25","14","17","20","22","23","24","20","22","24","13","15","16","17", "20","21","22","14","15","18","19","20","22","23","18","20") 

qty <- c(2,1,2,2,1,1,4,1,1,1,1,5,4,1,1,2,13,2,2,1,1,2,1,3,4,1,1,7,2,1,1,1, 6,5,2,2,3,2,1,2) 

ref_by_period_df <- data.frame(Period=period,Teacher=teacher,Qty=qty) 
ref_by_period_df <- ddply(ref_by_period_df, .(Period), transform, pos = cumsum(Qty) - (0.5 * Qty)) 

ref_by_period <- ggplot(ref_by_period_df, aes(x=Period,y=Qty))+ 
        geom_bar(aes(fill=Teacher),stat="identity")+ 
        geom_text(aes(label=Qty,x=Period,y=pos),size=3)+ 
        labs(y="Referral Count",x=NULL,title="Frequency of referrals by period by teacher")+ 
        theme(axis.text.x = element_text(angle = 90, hjust = 1)) 

你好精彩的堆棧交換人員,這些數據和代碼讓我看到我想要的圖形。但是,我現在想要在6周的時間間隔內將這些數據與並排條形圖進行比較。我想保留這些堆棧,但是現在我會有更多的並排堆棧(總共有6個:H1,H2,H3,H4,H5,H6)。因此,對於第一階段,我希望看到6個並排堆疊。但我不知道如何告訴ggplot基於新的變量字段「Hex」進行並排。當我把position =「dodge」放到geom_bar中時,並排獲得H1,H2,但是在它的右邊得到0-7。結果是不理想的。任何幫助,將不勝感激。在R ggplot中並排堆疊的條形圖

所以我的新的數據是這樣的:

hex1 <- c("H1", "H1", "H1", "H1", "H1", "H1", "H1", "H1", "H1", "H1", "H1", "H1", "H1", "H1", "H1", "H1", "H1", "H1", "H1", "H1", "H1" ,"H1", "H1", "H1", "H1", "H1", "H1","H1", "H1", "H2", "H2", "H2", "H2", "H2", "H2", "H2", "H2", "H2", "H2", "H2", "H2", "H2", "H2", "H2", "H2", "H2", "H2", "H2") 

Period1 <- c("period 0", "period 1", "period 1", "period 1", "period 1", "period 2", "period 2", "period 2", "period 2", "period 2", "period 3", "period 3", "period 3", "period 3", "period 3", "period 3.5", "period 3.5", "period 4", "period 4", "period 4", "period 4", "period 4", "period 5", "period 5", "period 5", "period 5", "period 5", "period 5", "period 7", "period 0", "period 1", "period 2", "period 2", "period 2", "period 2" , "period 2", "period 3", "period 3", "period 3", "period 3.5", "period 4", "period 4" , "period 4", "period 5", "period 5", "period 5", "period 5", "period 6") 

Teacher1 <- c("17", "14", "17", "20", "23", "13", "19", "20", "22", "23", "14" ,"17", "20", "23", "24", "22", "24", "13", "16", "17", "20", "21", "14", "15", "18", "20", "22","23", "20", "17", "22", "13", "15", "17", "24", "25", "14", "17", "22", "20", "15", "17", "22", "15" ,"18", "19", "22", "18") 

Qty1 <- c(1, 1, 2, 2, 1, 3, 1, 1, 5, 4, 1, 9, 2, 1, 1, 1, 3, 4, 1, 5, 2, 1, 1, 5, 3, 2, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 4, 2, 2, 1, 2, 1, 1, 2, 2, 2, 1) 


ref_by_period_df <- data.frame(Hex=hex1,Period=Period1,Teacher=Teacher1,Qty=Qty1) 
ref_by_period_df <- ddply(ref_by_period_df, .(Period), transform, pos = cumsum(Qty) - (0.5 * Qty)) #Now let's calculate midpoint 
ref_by_period <- ggplot(ref_by_period_df, aes(x=Period,y=Qty))+ 
    geom_bar(aes(Hex,fill=Teacher),stat="identity",position="dodge")+ 
    geom_text(aes(label=Qty,x=Period,y=pos),size=3)+ 
    labs(y="Referral Count",x=NULL,title="Frequency of referrals by period by teacher")+ 
    theme(axis.text.x = element_text(angle = 90, hjust = 1)) 
    #This puts H1,H2 on the left, Period 0-7 on the right. Bars appear for H1,H2, but not for the Periods. The numerical values appear for the periods, but they are not correct. 
+0

不知[此帖](http://stackoverflow.com/questions/12592041/plotting-a-stacked-bar-plot/12592235#12592235)會幫助你。 – jazzurro

+0

我認爲可能是這樣,但我找不到那個帖子。非常感謝你爵士樂! – datadan

回答

4

車花將在這裏工作得很好。它看起來並不像你想要的那樣,但它應該傳達信息。

# Data 
Hex <- sample(c("H1","H2","H3","H4","H5","H6"),size = 100,replace = TRUE) 
Period <- sample(c("period 0", "period 1","period 2", "period 3","period 3.5", "period 4","period 5", "period 7"),size = 100, replace = TRUE) 
Teacher <- sample(c("13","14","15","16","17","18","19","20","21","22","23","24","25"), size = 100, replace = TRUE) 
Qty <- sample(c(1:9), size = 100, replace = TRUE) 

ref_by_period_df <- data.frame(Hex=Hex,Period=Period,Teacher=Teacher,Qty=Qty) 
ref_by_period_df <- ddply(ref_by_period_df, .(Period), transform, pos = cumsum(Qty) - (0.5 * Qty)) #Now let's calculate midpoint 


# Plotting 
ggplot(ref_by_period_df, aes(x = Hex, y = Qty, fill = Teacher)) + 
    geom_bar(stat="identity") + 
    facet_grid(.~Period) + 
    theme(axis.text.x = element_text(angle = 90, size = 8)) 

+0

這是一個美麗的方法,完美適用於我的目的。謝謝@Laura – datadan