2013-10-16 107 views
0

我希望在单个条形图中的单个条块旁边有成对的堆叠条块。如何用R和ggplot创建堆叠和非堆叠组合条形图?

下面显示的十二个条形成对是成对的,但我想再次细分它们,在每个条形码旁边添加另一个条形。我想让轴线保持不变,但将虚线的白色条移动到堆叠酒吧旁边的“躲闪”位置。

下面列出的数据和代码用于创建R(3.0.1)和ggplot2(0.9.3)的图表。每个'定向'和'无向'实验共有六种处理方式。不幸的是,每种治疗方式都有稍微不同的情况一个是'系统'(上图),另一个是SA/SA-Sw(下图)。

x <- data.frame(
    ConnectType = c("Undirected","Undirected","Undirected","Undirected","Undirected","Undirected","Undirected","Undirected","Undirected","Undirected","Undirected","Undirected","Undirected","Undirected","Undirected","Undirected","Undirected","Undirected","Directed","Directed","Directed","Directed","Directed","Directed","Directed","Directed","Directed","Directed","Directed","Directed","Directed","Directed","Directed","Directed","Directed","Directed"), 
System = c("Mersey","Mersey","Mersey","Mersey","Mersey","Mersey","St. Marg. Bay","St. Marg. Bay","St. Marg. Bay","St. Marg. Bay","St. Marg. Bay","St. Marg. Bay","Sheet Harbour","Sheet Harbour","Sheet Harbour","Sheet Harbour","Sheet Harbour","Sheet Harbour","Mersey","Mersey","Mersey","Mersey","Mersey","Mersey","St. Marg. Bay","St. Marg. Bay","St. Marg. Bay","St. Marg. Bay","St. Marg. Bay","St. Marg. Bay","Sheet Harbour","Sheet Harbour","Sheet Harbour","Sheet Harbour","Sheet Harbour","Sheet Harbour"), 
BarType = factor(c("Dams","Culverts","Both","Dams","Culverts","Both","Dams","Culverts","Both","Dams","Culverts","Both","Dams","Culverts","Both","Dams","Culverts","Both","Dams","Culverts","Both","Dams","Culverts","Both","Dams","Culverts","Both","Dams","Culverts","Both","Dams","Culverts","Both","Dams","Culverts","Both"), 
       c("Dams","Culverts","Both")), 
QuanMethod = c("SA","SA","SA","SA-Sw","SA-Sw","SA-Sw","SA","SA","SA","SA-Sw","SA-Sw","SA-Sw","SA","SA","SA","SA-Sw","SA-Sw","SA-Sw","SA","SA","SA","SA-Sw","SA-Sw","SA-Sw","SA","SA","SA","SA-Sw","SA-Sw","SA-Sw","SA","SA","SA","SA-Sw","SA-Sw","SA-Sw"), 
ScaledGains = c(64.69,19.65,15.66,75.99,9.28,14.73,61.12,4.43,34.45,52.24,12.17,35.59,69.51,10.49,19.99,66.80,18.46,14.74, 93.39,0.22,6.39,90.20,0.45,9.35,81.08,0.00,18.92,77.09,0.01,22.9,87.14,2.19,10.67,88.28,2.51,9.21) 
) 

# ggplot2 library must be added through packages  
library(ggplot2) 
library(scales) 
library(grid) 

# set bar colours 
barcols<- c("#A9A9A9","#000000","#FFFFFF") 
bordcols<- c("#A9A9A9","#000000","#000000") 
linetypes1 <- c("solid","solid","dashed") 

# ggplot all of it 
ggplot(x, aes(x=QuanMethod, 
      y=ScaledGains, 
      fill=BarType, 
      color=BarType, 
      linetype=BarType)) + 
geom_bar(stat="identity") + 
facet_grid(ConnectType~System) + 
scale_linetype_manual(values=linetypes1) + 
scale_fill_manual(values = barcols) + 
scale_color_manual(values=bordcols) + 
xlab("Quantification Method") + 
ylab("Percent Connectivity Gains") + 
theme(legend.position = "bottom", 
     legend.title =element_blank()) 

example chart - either grey or black should be stacked![][1]

回答

0

答案是使用 '组' 标签在ggplot()。我只是为定义组的数据添加了一个新列。在这种情况下,'水坝'和'涵洞'有0,'水坝+涵洞'有1。我称之为'组1'。

ggplot(x, aes(x=QuanMethod, 
     y=ScaledGains, 
     fill=BarType, 
     color=BarType, 
     linetype=BarType, 
     group=group1)) + 
geom_bar(stat="identity") + 
facet_grid(ConnectType~System) + 
scale_linetype_manual(values=linetypes1) + 
scale_fill_manual(values = barcols) + 
scale_color_manual(values=bordcols) + 
xlab("Quantification Method") + 
ylab("Percent Connectivity Gains") + 
theme(legend.position = "bottom", 
legend.title =element_blank())