R:2x2 Window 中的四个格子条形图 side-by-side?
R: Four Lattice barcharts side-by-side in 2x2 Window?
我想做 2x2 条形图的代码 side-by-side 这样 y-axis 最大值在所有和
中都是相同的
- 普通标签
- 普通 xlabel
- 常见的传说
- 常用标题
- 每个条形图都有自己的副标题
- 每个条形图之间的分隔线,如图 2 所示
代码
# Wanted output 2x2 barchart where top column Ite. 1 and Ite. 2 and row-names female and male
# http://www.magesblog.com/2012/12/changing-colours-and-legends-in-lattice.html
require("lattice")
f<-function(x) as.double(as.character(x)) #factors converted to vectors
data.female <- structure(list(N11.1 = structure(c(3L, 3L), .Label = c("", "0.0",
"1.0", "N11"), class = "factor"), N22.1 = structure(c(2L, 2L), .Label = c("",
"0.0", "2.0", "N22"), class = "factor"), N33.1 = structure(c(2L,
2L), .Label = c("", "0.0", "N33"), class = "factor"), N44.1 = structure(2:3, .Label = c("",
"0.0", "0.1", "0.2", "N44"), class = "factor"), N21.1 = structure(c(2L,
2L), .Label = c("", "0.0", "N21"), class = "factor"), N31.1 = structure(c(2L,
2L), .Label = c("", "0.0", "N31"), class = "factor"), N32.1 = structure(c(5L,
7L), .Label = c("", "0.0", "10.8", "11.0", "12.0", "17.0", "20.9",
"22.8", "24.0", "3.0", "4.0", "44.0", "N32"), class = "factor")), .Names = c("N11.1",
"N22.1", "N33.1", "N44.1", "N21.1", "N31.1", "N32.1"), row.names = c("Sinus",
"Arr/AHB"), class = "data.frame")
data.male <- structure(list(N11.1 = structure(c(3L, 3L), .Label = c("", "0.0",
"1.0", "N11"), class = "factor"), N22.1 = structure(c(2L, 2L), .Label = c("",
"0.0", "2.0", "N22"), class = "factor"), N33.1 = structure(c(2L,
2L), .Label = c("", "0.0", "N33"), class = "factor"), N44.1 = structure(c(2L,
2L), .Label = c("", "0.0", "0.1", "0.2", "N44"), class = "factor"),
N21.1 = structure(c(2L, 2L), .Label = c("", "0.0", "N21"), class = "factor"),
N31.1 = structure(c(2L, 2L), .Label = c("", "0.0", "N31"), class = "factor"),
N32.1 = structure(c(11L, 9L), .Label = c("", "0.0", "10.8",
"11.0", "12.0", "17.0", "20.9", "22.8", "24.0", "3.0", "4.0",
"44.0", "N32"), class = "factor")), .Names = c("N11.1", "N22.1",
"N33.1", "N44.1", "N21.1", "N31.1", "N32.1"), row.names = c("Sinus",
"Arr/AHB"), class = "data.frame")
ID<-c("Sinus","Arr/AHB")
tl <- "female"
barchart(f(N11.1)+f(N22.1)+f(N33.1)+f(N44.1)+f(N21.1)+f(N31.1)+f(N32.1) ~ ID,
data=data.female,
auto.key=list(space='right'),
ylim=c(0,50),
beside=TRUE,
ylab = "Number of cases",
xlab = "Population/Sample",
main = tl
)
tl <- "male"
barchart(f(N11.1)+f(N22.1)+f(N33.1)+f(N44.1)+f(N21.1)+f(N31.1)+f(N32.1) ~ ID,
data=data.male,
auto.key=list(space='right'),
ylim=c(0,50),
beside=TRUE,
ylab = "Number of cases",
xlab = "Population/Sample",
main = tl
)
# Just repeat two barcharts more to get 2x2 example
tl <- "female"
barchart(f(N11.1)+f(N22.1)+f(N33.1)+f(N44.1)+f(N21.1)+f(N31.1)+f(N32.1) ~ ID,
data=data.female,
auto.key=list(space='right'),
ylim=c(0,50),
beside=TRUE,
ylab = "Number of cases",
xlab = "Population/Sample",
main = tl
)
tl <- "male"
barchart(f(N11.1)+f(N22.1)+f(N33.1)+f(N44.1)+f(N21.1)+f(N31.1)+f(N32.1) ~ ID,
data=data.male,
auto.key=list(space='right'),
ylim=c(0,50),
beside=TRUE,
ylab = "Number of cases",
xlab = "Population/Sample",
main = tl
)
图1 电流输出分离,
图 2 想要的输出结构,
图 3 用户 20650 条形图代码输出
R: 3.3.1
OS:Debian 8.5
您正在使用依赖于 grid PKG 的 Lattice PKG,因此您需要按照 Lattice: multiple plots in one window? 中的说明使用 windowing Grid PKG。 ggplot2 从未在代码中使用过。您可以使用 grid.arrange
命令执行 2x2 window。
Grid.arrange 带有 GridExtras 包和 Lattice 包
require(lattice)
require(gridExtra)
f<-function(x) as.double(as.character(x)) #factors converted to vectors
data.female <- structure(list(N11.1 = structure(c(3L, 3L), .Label = c("", "0.0",
"1.0", "N11"), class = "factor"), N22.1 = structure(c(2L, 2L), .Label = c("",
"0.0", "2.0", "N22"), class = "factor"), N33.1 = structure(c(2L,
2L), .Label = c("", "0.0", "N33"), class = "factor"), N44.1 = structure(2:3, .Label = c("",
"0.0", "0.1", "0.2", "N44"), class = "factor"), N21.1 = structure(c(2L,
2L), .Label = c("", "0.0", "N21"), class = "factor"), N31.1 = structure(c(2L,
2L), .Label = c("", "0.0", "N31"), class = "factor"), N32.1 = structure(c(5L,
7L), .Label = c("", "0.0", "10.8", "11.0", "12.0", "17.0", "20.9",
"22.8", "24.0", "3.0", "4.0", "44.0", "N32"), class = "factor")), .Names = c("N11.1",
"N22.1", "N33.1", "N44.1", "N21.1", "N31.1", "N32.1"), row.names = c("Sinus",
"Arr/AHB"), class = "data.frame")
data.male <- structure(list(N11.1 = structure(c(3L, 3L), .Label = c("", "0.0",
"1.0", "N11"), class = "factor"), N22.1 = structure(c(2L, 2L), .Label = c("",
"0.0", "2.0", "N22"), class = "factor"), N33.1 = structure(c(2L,
2L), .Label = c("", "0.0", "N33"), class = "factor"), N44.1 = structure(c(2L,
2L), .Label = c("", "0.0", "0.1", "0.2", "N44"), class = "factor"),
N21.1 = structure(c(2L, 2L), .Label = c("", "0.0", "N21"), class = "factor"),
N31.1 = structure(c(2L, 2L), .Label = c("", "0.0", "N31"), class = "factor"),
N32.1 = structure(c(11L, 9L), .Label = c("", "0.0", "10.8",
"11.0", "12.0", "17.0", "20.9", "22.8", "24.0", "3.0", "4.0",
"44.0", "N32"), class = "factor")), .Names = c("N11.1", "N22.1",
"N33.1", "N44.1", "N21.1", "N31.1", "N32.1"), row.names = c("Sinus",
"Arr/AHB"), class = "data.frame")
ID<-c("Sinus","Arr/AHB")
tl <- "female"
p1 <- barchart(f(N11.1)+f(N22.1)+f(N33.1)+f(N44.1)+f(N21.1)+f(N31.1)+f(N32.1) ~ ID,
data=data.female,
auto.key=list(space='right'),
ylim=c(0,50),
beside=TRUE,
ylab = "Number of cases",
xlab = "Population/Sample",
main = tl
)
tl <- "male"
p2 <- barchart(f(N11.1)+f(N22.1)+f(N33.1)+f(N44.1)+f(N21.1)+f(N31.1)+f(N32.1) ~ ID,
data=data.male,
auto.key=list(space='right'),
ylim=c(0,50),
beside=TRUE,
ylab = "Number of cases",
xlab = "Population/Sample",
main = tl
)
# Just repeat two barcharts more to get 2x2 example
tl <- "female"
p3 <- barchart(f(N11.1)+f(N22.1)+f(N33.1)+f(N44.1)+f(N21.1)+f(N31.1)+f(N32.1) ~ ID,
data=data.female,
auto.key=list(space='right'),
ylim=c(0,50),
beside=TRUE,
ylab = "Number of cases",
xlab = "Population/Sample",
main = tl
)
tl <- "male"
p4 <- barchart(f(N11.1)+f(N22.1)+f(N33.1)+f(N44.1)+f(N21.1)+f(N31.1)+f(N32.1) ~ ID,
data=data.male,
auto.key=list(space='right'),
ylim=c(0,50),
beside=TRUE,
ylab = "Number of cases",
xlab = "Population/Sample",
main = tl)
grid.arrange(p1,p2,p3,p4, ncol=2, nrow=2)
对于常见的xlabel、ylabel等,将最后一行改为
grid.arrange(p1,p2,p3,p4, ncol=2, nrow=2,left=("LEFT TITLE"),right=("RIGHT"),bottom=("BOTTOM"), top=("TOP"))
P.s。我把最后一个关于共享图例的谜题移到了here.
我会通过重塑您的数据来做到这一点
首先整理出变量的class,加入分组变量
# convert type and add gender label
# I would have a look at why your numerics have been stored as factors
data.female[] <- lapply(data.female, function(x) as.numeric(as.character(x)))
data.female$gender <- "female"
data.female$ID <- rownames(data.female)
data.male[] <- lapply(data.male, function(x) as.numeric(as.character(x)))
data.male$gender <- "male"
data.male$ID <- rownames(data.male)
将两个数据框绑定在一起
dat <- rbind(data.female[names(data.male)], data.male)
整理绘图数据
library(reshape2)
datm <- melt(dat)
情节
# Lattice
library(lattice)
barchart(variable ~ value|ID, groups=gender, data=datm,
auto.key=list(space='right'))
# ggplot2
ggplot(datm, aes(variable, value, fill=gender)) +
geom_bar(stat="identity", position = position_dodge()) +
facet_grid(ID ~ .)
我想做 2x2 条形图的代码 side-by-side 这样 y-axis 最大值在所有和
中都是相同的- 普通标签
- 普通 xlabel
- 常见的传说
- 常用标题
- 每个条形图都有自己的副标题
- 每个条形图之间的分隔线,如图 2 所示
代码
# Wanted output 2x2 barchart where top column Ite. 1 and Ite. 2 and row-names female and male
# http://www.magesblog.com/2012/12/changing-colours-and-legends-in-lattice.html
require("lattice")
f<-function(x) as.double(as.character(x)) #factors converted to vectors
data.female <- structure(list(N11.1 = structure(c(3L, 3L), .Label = c("", "0.0",
"1.0", "N11"), class = "factor"), N22.1 = structure(c(2L, 2L), .Label = c("",
"0.0", "2.0", "N22"), class = "factor"), N33.1 = structure(c(2L,
2L), .Label = c("", "0.0", "N33"), class = "factor"), N44.1 = structure(2:3, .Label = c("",
"0.0", "0.1", "0.2", "N44"), class = "factor"), N21.1 = structure(c(2L,
2L), .Label = c("", "0.0", "N21"), class = "factor"), N31.1 = structure(c(2L,
2L), .Label = c("", "0.0", "N31"), class = "factor"), N32.1 = structure(c(5L,
7L), .Label = c("", "0.0", "10.8", "11.0", "12.0", "17.0", "20.9",
"22.8", "24.0", "3.0", "4.0", "44.0", "N32"), class = "factor")), .Names = c("N11.1",
"N22.1", "N33.1", "N44.1", "N21.1", "N31.1", "N32.1"), row.names = c("Sinus",
"Arr/AHB"), class = "data.frame")
data.male <- structure(list(N11.1 = structure(c(3L, 3L), .Label = c("", "0.0",
"1.0", "N11"), class = "factor"), N22.1 = structure(c(2L, 2L), .Label = c("",
"0.0", "2.0", "N22"), class = "factor"), N33.1 = structure(c(2L,
2L), .Label = c("", "0.0", "N33"), class = "factor"), N44.1 = structure(c(2L,
2L), .Label = c("", "0.0", "0.1", "0.2", "N44"), class = "factor"),
N21.1 = structure(c(2L, 2L), .Label = c("", "0.0", "N21"), class = "factor"),
N31.1 = structure(c(2L, 2L), .Label = c("", "0.0", "N31"), class = "factor"),
N32.1 = structure(c(11L, 9L), .Label = c("", "0.0", "10.8",
"11.0", "12.0", "17.0", "20.9", "22.8", "24.0", "3.0", "4.0",
"44.0", "N32"), class = "factor")), .Names = c("N11.1", "N22.1",
"N33.1", "N44.1", "N21.1", "N31.1", "N32.1"), row.names = c("Sinus",
"Arr/AHB"), class = "data.frame")
ID<-c("Sinus","Arr/AHB")
tl <- "female"
barchart(f(N11.1)+f(N22.1)+f(N33.1)+f(N44.1)+f(N21.1)+f(N31.1)+f(N32.1) ~ ID,
data=data.female,
auto.key=list(space='right'),
ylim=c(0,50),
beside=TRUE,
ylab = "Number of cases",
xlab = "Population/Sample",
main = tl
)
tl <- "male"
barchart(f(N11.1)+f(N22.1)+f(N33.1)+f(N44.1)+f(N21.1)+f(N31.1)+f(N32.1) ~ ID,
data=data.male,
auto.key=list(space='right'),
ylim=c(0,50),
beside=TRUE,
ylab = "Number of cases",
xlab = "Population/Sample",
main = tl
)
# Just repeat two barcharts more to get 2x2 example
tl <- "female"
barchart(f(N11.1)+f(N22.1)+f(N33.1)+f(N44.1)+f(N21.1)+f(N31.1)+f(N32.1) ~ ID,
data=data.female,
auto.key=list(space='right'),
ylim=c(0,50),
beside=TRUE,
ylab = "Number of cases",
xlab = "Population/Sample",
main = tl
)
tl <- "male"
barchart(f(N11.1)+f(N22.1)+f(N33.1)+f(N44.1)+f(N21.1)+f(N31.1)+f(N32.1) ~ ID,
data=data.male,
auto.key=list(space='right'),
ylim=c(0,50),
beside=TRUE,
ylab = "Number of cases",
xlab = "Population/Sample",
main = tl
)
图1 电流输出分离, 图 2 想要的输出结构, 图 3 用户 20650 条形图代码输出
R: 3.3.1
OS:Debian 8.5
您正在使用依赖于 grid PKG 的 Lattice PKG,因此您需要按照 Lattice: multiple plots in one window? 中的说明使用 windowing Grid PKG。 ggplot2 从未在代码中使用过。您可以使用 grid.arrange
命令执行 2x2 window。
Grid.arrange 带有 GridExtras 包和 Lattice 包
require(lattice)
require(gridExtra)
f<-function(x) as.double(as.character(x)) #factors converted to vectors
data.female <- structure(list(N11.1 = structure(c(3L, 3L), .Label = c("", "0.0",
"1.0", "N11"), class = "factor"), N22.1 = structure(c(2L, 2L), .Label = c("",
"0.0", "2.0", "N22"), class = "factor"), N33.1 = structure(c(2L,
2L), .Label = c("", "0.0", "N33"), class = "factor"), N44.1 = structure(2:3, .Label = c("",
"0.0", "0.1", "0.2", "N44"), class = "factor"), N21.1 = structure(c(2L,
2L), .Label = c("", "0.0", "N21"), class = "factor"), N31.1 = structure(c(2L,
2L), .Label = c("", "0.0", "N31"), class = "factor"), N32.1 = structure(c(5L,
7L), .Label = c("", "0.0", "10.8", "11.0", "12.0", "17.0", "20.9",
"22.8", "24.0", "3.0", "4.0", "44.0", "N32"), class = "factor")), .Names = c("N11.1",
"N22.1", "N33.1", "N44.1", "N21.1", "N31.1", "N32.1"), row.names = c("Sinus",
"Arr/AHB"), class = "data.frame")
data.male <- structure(list(N11.1 = structure(c(3L, 3L), .Label = c("", "0.0",
"1.0", "N11"), class = "factor"), N22.1 = structure(c(2L, 2L), .Label = c("",
"0.0", "2.0", "N22"), class = "factor"), N33.1 = structure(c(2L,
2L), .Label = c("", "0.0", "N33"), class = "factor"), N44.1 = structure(c(2L,
2L), .Label = c("", "0.0", "0.1", "0.2", "N44"), class = "factor"),
N21.1 = structure(c(2L, 2L), .Label = c("", "0.0", "N21"), class = "factor"),
N31.1 = structure(c(2L, 2L), .Label = c("", "0.0", "N31"), class = "factor"),
N32.1 = structure(c(11L, 9L), .Label = c("", "0.0", "10.8",
"11.0", "12.0", "17.0", "20.9", "22.8", "24.0", "3.0", "4.0",
"44.0", "N32"), class = "factor")), .Names = c("N11.1", "N22.1",
"N33.1", "N44.1", "N21.1", "N31.1", "N32.1"), row.names = c("Sinus",
"Arr/AHB"), class = "data.frame")
ID<-c("Sinus","Arr/AHB")
tl <- "female"
p1 <- barchart(f(N11.1)+f(N22.1)+f(N33.1)+f(N44.1)+f(N21.1)+f(N31.1)+f(N32.1) ~ ID,
data=data.female,
auto.key=list(space='right'),
ylim=c(0,50),
beside=TRUE,
ylab = "Number of cases",
xlab = "Population/Sample",
main = tl
)
tl <- "male"
p2 <- barchart(f(N11.1)+f(N22.1)+f(N33.1)+f(N44.1)+f(N21.1)+f(N31.1)+f(N32.1) ~ ID,
data=data.male,
auto.key=list(space='right'),
ylim=c(0,50),
beside=TRUE,
ylab = "Number of cases",
xlab = "Population/Sample",
main = tl
)
# Just repeat two barcharts more to get 2x2 example
tl <- "female"
p3 <- barchart(f(N11.1)+f(N22.1)+f(N33.1)+f(N44.1)+f(N21.1)+f(N31.1)+f(N32.1) ~ ID,
data=data.female,
auto.key=list(space='right'),
ylim=c(0,50),
beside=TRUE,
ylab = "Number of cases",
xlab = "Population/Sample",
main = tl
)
tl <- "male"
p4 <- barchart(f(N11.1)+f(N22.1)+f(N33.1)+f(N44.1)+f(N21.1)+f(N31.1)+f(N32.1) ~ ID,
data=data.male,
auto.key=list(space='right'),
ylim=c(0,50),
beside=TRUE,
ylab = "Number of cases",
xlab = "Population/Sample",
main = tl)
grid.arrange(p1,p2,p3,p4, ncol=2, nrow=2)
对于常见的xlabel、ylabel等,将最后一行改为
grid.arrange(p1,p2,p3,p4, ncol=2, nrow=2,left=("LEFT TITLE"),right=("RIGHT"),bottom=("BOTTOM"), top=("TOP"))
P.s。我把最后一个关于共享图例的谜题移到了here.
我会通过重塑您的数据来做到这一点
首先整理出变量的class,加入分组变量
# convert type and add gender label
# I would have a look at why your numerics have been stored as factors
data.female[] <- lapply(data.female, function(x) as.numeric(as.character(x)))
data.female$gender <- "female"
data.female$ID <- rownames(data.female)
data.male[] <- lapply(data.male, function(x) as.numeric(as.character(x)))
data.male$gender <- "male"
data.male$ID <- rownames(data.male)
将两个数据框绑定在一起
dat <- rbind(data.female[names(data.male)], data.male)
整理绘图数据
library(reshape2)
datm <- melt(dat)
情节
# Lattice
library(lattice)
barchart(variable ~ value|ID, groups=gender, data=datm,
auto.key=list(space='right'))
# ggplot2
ggplot(datm, aes(variable, value, fill=gender)) +
geom_bar(stat="identity", position = position_dodge()) +
facet_grid(ID ~ .)