【问题标题】:How to create stacked and non-stacked combo barchart with R and ggplot?如何使用 R 和 ggplot 创建堆叠和非堆叠组合条形图?
【发布时间】:2013-10-16 23:17:29
【问题描述】:

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

下面显示的 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())

【问题讨论】:

    标签: r ggplot2 bar-chart


    【解决方案1】:

    答案是在 ggplot() 中使用 'group' 标签。我只是在定义组的数据中添加了一个新列。在这种情况下,“水坝”和“涵洞”的值为 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())
    

    【讨论】:

      猜你喜欢
      • 2021-11-16
      • 2023-03-18
      • 1970-01-01
      • 2021-11-13
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 2017-11-01
      相关资源
      最近更新 更多