带嵌套分组变量的多行轴标签,用于-R中的堆叠条形图

时间:2018-09-20 11:33:27

标签: r ggplot2 categories axis-labels stacked-chart

我想使用ggplot制作带有嵌套标签X轴的多个类别的堆叠条形图,类似于我使用Excel制作的图,如下所示。

enter image description here

我尝试使用给出的示例here来使用facet_wrap()进行未堆积的条形图

enter image description here

但是如您所见,此代码将列分散得很远,并且不会将类别级别(即2017、2030、2040)彼此相邻的堆叠条形群集。

我还尝试使用示例here,但这仅允许一个X类别级别,而不是两个。

我在下面添加了一些示例代码(与原始代码相比有所减少,但具有相同的意图)。

一些帮助会很棒!

data1 <- read.table(text = "Property Category Datetime Value
               PoolRevenue Nuclear 2017 73
               PoolRevenue Nuclear 2030 75
               PoolRevenue CCGT 2017 57
               PoolRevenue CCGT 2030 45
               PoolRevenue Hydro 2017 23
               PoolRevenue Hydro 2030 28
               ReservesRevenue Nuclear 2017 34
               ReservesRevenue Nuclear 2030 37
               ReservesRevenue CCGT 2017 34
               ReservesRevenue CCGT 2030 9
               ReservesRevenue Hydro 2017 23
               ReservesRevenue Hydro 2030 36
               OtherRevenue Nuclear 2017 96
               OtherRevenue Nuclear 2030 98
               OtherRevenue CCGT 2017 55
               OtherRevenue CCGT 2030 53
               OtherRevenue Hydro 2017 60
               OtherRevenue Hydro 2030 65", header=TRUE)
data2 <- read.table(text = "Property Category Datetime Value
                GenCost Nuclear 2017 -10
                GenCost Nuclear 2030 -20
                GenCost CCGT 2017 -15
                GenCost CCGT 2030 -20
                GenCost Hydro 2017 -10
                GenCost Hydro 2030 -15
                FixedCosts Nuclear 2017 -13
                FixedCosts Nuclear 2030 -11
                FixedCosts CCGT 2017 -30
                FixedCosts CCGT 2030 -12
                FixedCosts Hydro 2017 -15
                FixedCosts Hydro 2030 -12", header=TRUE)
data1$Datetime <- as.factor(data1$Datetime)
data2$Datetime <- as.factor(data2$Datetime)
p1 <-ggplot()
p2 <-  p1+ geom_bar(data=data1,aes_string(x="Datetime",y="Value",fill="Property"),stat="identity", position = position_stack(reverse = TRUE))+guides(fill = guide_legend(reverse=TRUE))
p3 <-  p2+ geom_bar(data=data2,aes_string(x="Datetime",y="Value",fill="Property"),stat="identity", position = position_stack(reverse = TRUE))+guides(fill = guide_legend(reverse=TRUE))
p4 <- p3 +facet_wrap(c("Category","Datetime"), strip.position = "bottom", scales = "free_x") + theme(panel.spacing = unit(0, "lines"), strip.background = element_blank(), strip.placement = "outside")

(请注意,由于我在堆叠的条形图中同时具有正值和负值,因此将它们应用为单独的图层,但我认为这不会影响轴。)

1 个答案:

答案 0 :(得分:0)

你的意思是这样吗?

# merge your data   
data_x <- rbind(data1,data2)

    p1 <-ggplot()
 p1+ geom_bar(data=data_x,aes_string(x="Datetime",y="Value", fill ="Property"),stat="identity")+
   facet_wrap(vars(Category), strip.position = "bottom", scales = "free_x")+ 
   theme(panel.spacing = unit(0, "lines"),
         strip.background = element_blank(),
         axis.line = element_line(colour = "grey"),
         panel.grid.major.y =element_line(colour = "grey"),
         strip.placement = "outside",
         axis.text.x = element_text(angle = 90, hjust = 1),
         panel.background = element_rect(fill = 'white', colour = 'white')
         )

enter image description here

编辑

如果要设置变量,可以尝试以下操作:

x <-"Datetime"
y <- "Value"
filler <- "Property"
    p1 <-ggplot()
 p1+ geom_bar(data=data_x,aes_string(x=x,y=y, fill =filler),stat="identity")+
   facet_wrap(vars(Category), strip.position = "bottom", scales = "free_x",  nrow=1)+ 
   theme(panel.spacing = unit(0, "lines"),
         strip.background = element_blank(),
         axis.line = element_line(colour = "grey"),
         panel.grid.major.y =element_line(colour = "grey"),
         strip.placement = "outside",
         axis.text.x = element_text(angle = 90, hjust = 1),
         panel.background = element_rect(fill = 'white', colour = 'white')
         )