堆积和分组的条形图

时间:2017-05-31 20:29:56

标签: r ggplot2 bar-chart

Haven没有看到这个问题的答案,但我觉得它应该相对简单。

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

我喜欢简单的(ish?)化妆品改变。而不是每个工作日有四个酒吧,我只想要两个。我喜欢堆叠在一起的酒吧。也就是说,我希望看到酒吧&#34; Block&#34;酒吧&#34;已添加&#34;堆叠在它上面,我想看看酒吧&#34;皮卡康拉德&#34;酒吧&#34;皮卡其他&#34;堆叠在它上面。(无需担心2013年)。

也许我可以为上面提到的两个条形中的每一个创建一个新变量total,然后用表示Block,Added,Pickup Conrad和Pickup Other的变量填充该变量以显示堆叠的外观?

1 个答案:

答案 0 :(得分: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