2017-05-31 159 views
2

還沒有看到這個問題的答案,但我覺得它應該是相對簡單的。堆積和分組條形圖

library(ggplot2) 
Hotels<-data.frame(Year=c(2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 
         2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 
         2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 
         2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 
         2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013), 
       Day=c('Monday', 'Monday', 'Monday', 'Monday', 'Tuesday', 'Tuesday', 'Tuesday', 'Tuesday', 'Wednesday', 'Wednesday', 'Wednesday', 'Wednesday', 'Thursday', 'Thursday', 'Thursday', 'Thursday', 'Friday', 'Friday', 'Friday', 'Friday', 'Saturday', 'Saturday', 'Saturday', 'Saturday', 
         'Monday', 'Monday', 'Monday', 'Monday', 'Tuesday', 'Tuesday', 'Tuesday', 'Tuesday', 'Wednesday', 'Wednesday', 'Wednesday', 'Wednesday', 'Thursday', 'Thursday', 'Thursday', 'Thursday', 'Friday', 'Friday', 'Friday', 'Friday', 'Saturday', 'Saturday', 'Saturday', 'Saturday', 
         'Monday', 'Monday', 'Monday', 'Monday', 'Tuesday', 'Tuesday', 'Tuesday', 'Tuesday', 'Wednesday', 'Wednesday', 'Wednesday', 'Wednesday', 'Thursday', 'Thursday', 'Thursday', 'Thursday', 'Friday', 'Friday', 'Friday', 'Friday', 'Saturday', 'Saturday', 'Saturday', 'Saturday', 
         'Monday', 'Monday', 'Monday', 'Monday', 'Tuesday', 'Tuesday', 'Tuesday', 'Tuesday', 'Wednesday', 'Wednesday', 'Wednesday', 'Wednesday', 'Thursday', 'Thursday', 'Thursday', 'Thursday', 'Friday', 'Friday', 'Friday', 'Friday', 'Saturday', 'Saturday', 'Saturday', 'Saturday', 
         'Monday', 'Monday', 'Tuesday', 'Tuesday', 'Wednesday', 'Wednesday', 'Thursday', 'Thursday', 'Friday', 'Friday', 'Saturday', 'Saturday'), 
       Code=c('Block', 'Added', 'Pickup Conrad', 'Pickup Other', 'Block', 'Added', 'Pickup Conrad', 'Pickup Other', 'Block', 'Added', 'Pickup Conrad', 'Pickup Other', 'Block', 'Added', 'Pickup Conrad', 'Pickup Other', 'Block', 'Added', 'Pickup Conrad', 'Pickup Other', 'Block', 'Added', 'Pickup Conrad', 'Pickup Other', 
         'Block', 'Added', 'Pickup Conrad', 'Pickup Other', 'Block', 'Added', 'Pickup Conrad', 'Pickup Other', 'Block', 'Added', 'Pickup Conrad', 'Pickup Other', 'Block', 'Added', 'Pickup Conrad', 'Pickup Other', 'Block', 'Added', 'Pickup Conrad', 'Pickup Other', 'Block', 'Added', 'Pickup Conrad', 'Pickup Other', 
         'Block', 'Added', 'Pickup Conrad', 'Pickup Other', 'Block', 'Added', 'Pickup Conrad', 'Pickup Other', 'Block', 'Added', 'Pickup Conrad', 'Pickup Other', 'Block', 'Added', 'Pickup Conrad', 'Pickup Other', 'Block', 'Added', 'Pickup Conrad', 'Pickup Other', 'Block', 'Added', 'Pickup Conrad', 'Pickup Other', 
         'Block', 'Added', 'Pickup Conrad', 'Pickup Other', 'Block', 'Added', 'Pickup Conrad', 'Pickup Other', 'Block', 'Added', 'Pickup Conrad', 'Pickup Other', 'Block', 'Added', 'Pickup Conrad', 'Pickup Other', 'Block', 'Added', 'Pickup Conrad', 'Pickup Other', 'Block', 'Added', 'Pickup Conrad', 'Pickup Other', 
         'Block', 'Pickup', 'Block', 'Pickup', 'Block', 'Pickup','Block', 'Pickup', 'Block', 'Pickup', 'Block', 'Pickup'), 
       Count=c(0,0,2,0,42,0,47,0,159,12,166,17,174,12,182,17,22,0,27,0,0,0,8,0, 
         0,0,3,0,10,30,39,0,127,30,174,18,142,30,203,21,20,0,25,9,0,0,13,0, 
         0,0,4,0,45,0,32,2,145,20,137,8,160,20,161,8,20,0,22,8,0,0,4,0, 
         0,0,5,0,0,8,21,1,59,45,110,25,109,8,126,28,19,2,16,0,0,0,9,0, 
         0,0,0,15,84,102,109,119,19,11,0,4)) 
HotelBar<-ggplot(data=Hotels, aes(x=Day, y=as.numeric(Count), fill=Code))+ 
     geom_bar(aes(fill=Code), position="dodge", stat="identity")+ 
     scale_x_discrete(limits=c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"))+ 
     facet_grid(Year~.) 

HotelBar 

Bar Chart

我想一個簡單的(英語?)表面上的變化。而不是每個工作日有四個酒吧,我只想要兩個。我想把酒吧堆疊在一起。也就是說,我希望看到「Block」欄上的「已添加」欄堆疊在上面,我希望看到「Pickup Conrad」欄中的「Pickup Other」欄堆疊在它上面(無需擔心2013年)。

也許我可以爲上面提到的兩個條形中的每一個創建一個新的變量total,然後用一個表示Block,Added,Pick Conrad和Pickup Other的變量填充這個總數來顯示堆疊的外觀?

+1

檢查這一個 - https://stackoverflow.com/a/18774972/4978581 –

+0

謝謝@邁克爾基什內爾。我看到那篇文章,我不確定它對我的問題有幫助嗎?我的數據已經很長。我想我需要添加一個變量,如'Hotels $ Category <-ifelse(酒店$%代碼%%c('Block','Added'),「Contract」, ifelse(酒店$代碼% Pickup Conrad','Pickup Other','Pickup'),「Actual」,''))',然後用它作爲填充變量? –

回答

3

這裏是我會怎麼做:

# Create a fake variable to group the columns into 2 stacks 
Hotels$code_stack = ifelse(Hotels$Code %in% c('Block','Added'), 'first_stack', 'second_stack') 

# Put the days in the correct order 
Hotels$Day = factor(Hotels$Day, c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")) 

# Display the result... 
# Note the `x=code_stack` to plot two columns 
# Also note `position="stack"` to stack the barcharts 
# The final change of theme removes the x axis 
ggplot(data=Hotels, aes(x=code_stack, y=as.numeric(Count), fill=Code))+ 
    geom_bar(position="stack", stat="identity")+ 
    facet_grid(Year ~ Day) + 
    theme(axis.title.x=element_blank(), 
     axis.text.x=element_blank(), 
     axis.ticks.x=element_blank()) 

其中給出:

enter image description here

+1

@ J.Collins如果有幫助,請考慮upvoting和接受作爲答案。 – zx8754