0

我想在单个条形图中的单个条形旁边有成对的堆叠条形。

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

下面列出的数据和代码用于使用 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())

示例图表 - 应堆叠灰色或黑色![][1]

4

1 回答 1

1

答案是在 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())
于 2013-10-22T13:52:36.900 回答