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())