本文介绍了用于IN-R堆叠条形图的带有嵌套分组变量的多行轴标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用gggraph制作一个具有多个类别的堆叠条形图,其中包含一个嵌套的标签为X轴的条形图,类似于我使用Excel制作的条形图,如下所示。
我尝试使用facet_wrap()
给出的非堆叠条形图的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")
(注意,因为我的堆叠条形图中既有正值也有负值,所以我将它们作为单独的层应用,但我认为这不会影响轴。)
推荐答案
您的意思是这样的吗?
# 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')
)
编辑
如果要设置变量,可以尝试以下操作:
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')
)
这篇关于用于IN-R堆叠条形图的带有嵌套分组变量的多行轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!