在R中的同一个图中排列许多图
Arranging many graphs in a same plot in R
我想在一个图中使用不同的图形,但是我使用的命令 (grid.arrange
) 扭曲了图表的条形图。除此之外,我想将每个图表的标题(A、B、C ..)放在图表的顶部,而不是它显示的侧面。
在图 1 中,我有一个图表(正确的条),在第二个图表中,所有图表都放在一起。有人可以帮我重新调整这个命令吗?
谢谢
library(ggplot2)
library(ggpubr)
A<-ggplot(dados, aes(specie, A)) +
geom_col(alpha = 0.5, aes(colour = trophic, fill = trophic,
group = trophic)) +
rotate_x_text(angle = 55)
B<-ggplot(dados, aes(specie, B)) +
geom_col(alpha = 0.5, aes(colour = trophic, fill = trophic,
group = trophic)) +
rotate_x_text(angle = 55)
C<-ggplot(dados, aes(specie, C)) +
geom_col(alpha = 0.5, aes(colour = trophic, fill = trophic,
group = trophic)) +
rotate_x_text(angle = 55)
D<-ggplot(dados, aes(specie, D)) +
geom_col(alpha = 0.5, aes(colour = trophic, fill = trophic,
group = trophic)) +
rotate_x_text(angle = 55)
E<-ggplot(dados, aes(specie, E)) +
geom_col(alpha = 0.5, aes(colour = trophic, fill = trophic,
group = trophic)) +
rotate_x_text(angle = 55)
G<-ggplot(dados, aes(specie, G)) +
geom_col(alpha = 0.5, aes(colour = trophic, fill = trophic,
group = trophic)) +
rotate_x_text(angle = 55)
H<-ggplot(dados, aes(specie, H)) +
geom_col(alpha = 0.5, aes(colour = trophic, fill = trophic,
group = trophic)) +
rotate_x_text(angle = 55)
I<-ggplot(dados, aes(specie, I)) +
geom_col(alpha = 0.5, aes(colour = trophic, fill = trophic,
group = trophic)) +
rotate_x_text(angle = 55)
J<-ggplot(dados, aes(specie, J)) +
geom_col(alpha = 0.5, aes(colour = trophic, fill = trophic,
group = trophic)) +
rotate_x_text(angle = 55)
grid.arrange(A, B, C, D, E, G, H, I, J, ncol=3)
我的数据:
specie A B C D E G H I J bodymass trophic
AAThry_swin 72 0 76.8 63.2 0 0 0 1.6 0 4000 1
ABHys_afri 23.2 0 77.6 68.8 0 0 0 0 0 14936.02 1
ACSylv_grim 48 0 52 0 0.8 0 0 15.2 0 15639.15 1
ADTrag_scri 100 2.4 100 2.4 17.6 0 3.2 14.4 0 43250.39 1
AERed_aru 100 0.8 53.6 0 2.4 0 25.6 0 0 58059.24 1
AFHipp_eq 14.4 0 6.4 0 4.8 0 4 0 0 264173.96 1
AGTrag_oryx 16 0 15.2 0 4 0 0 0 0 562592.69 1
AHSync_caf 81.6 0 41.6 8.8 8.8 0 25.6 2.4 0 592665.98 1
AILox_afric 1.6 28 1.6 0.8 3.2 0 47.2 3.2 0 3824539.93 1
BAOtol_cras 7.2 0 1.6 22.4 0 0 0 0 0 1206.61 2
BBMiop_tal 1.6 0 1.6 54.4 0 0 0 0.8 0 1248.86 2
BCLep_cap 14.4 0 12 0 0 0 0 0 0 1500 2
BEGenet_gen 0 0 0 80.8 0 0 0 0 0 1756.17 2
BFPhil_mont 20.8 0 100 16.8 4.8 0 2.4 12.8 0 4896.05 2
BGChlor_cyn 72.8 0 16.8 100 0 0 0 1.6 0 5000.01 2
BHCerc_mit 5.6 0 11.2 100 0 0 0 0.8 0 5041.29 2
BICivet_civ 43.2 0 52.8 19.2 0 0 0 16.8 41.6 12075.58 2
BJPota_larv 100 0 84 64 14.4 0 1.6 18.4 1.6 69063.79 2
BLHipp_amph 22.4 0 0 8.8 0 0 0 4 0 1536310.4 2
CALept_serv 0.8 56 3.2 80 0 14.4 0 23.2 78.4 11999.96 3
CBLyc_pict 0.8 10.4 0 23.2 0 12 0.8 9.6 19.2 21999.99 3
CCCan_mes 1.6 0 0 20.8 0 6.4 0 32 0 22000 3
CDPant_pa 18.4 24 1.6 12 0 17.6 27.2 4 8 52399.99 3
CEOryct_afer 11.2 1.6 22.4 0 0 0 0 5.6 4.8 56175.2 3
CFCroc_croc 51.2 0 0 22.4 0 5.6 0 1.6 0 63369.98 3
CGPant_le 0 15.2 0.8 27.2 0 57.6 24 1.6 1.6 158623.93 3
你可以试试:
library(dplyr)
dados %>%
gather(type, value, -c("specie", "bodymass", "trophic")) %>%
ggplot(aes(x=specie, y=value)) + geom_col(alpha=0.5, aes(color=trophic, fill=trophic, group=trophic)) +
facet_wrap(~type, ncol=3) +
theme(axis.text.x = element_text(angle=55))
编辑:
要控制trophic的颜色,需要把它从连续变量改成因子:
dados %>%
gather(type, value, -c("specie", "bodymass", "trophic")) %>%
mutate(trophic = as.factor(trophic)) %>%
ggplot(aes(x=specie, y=value)) + geom_col(alpha=0.5, aes(color=trophic, fill=trophic, group=trophic)) +
facet_wrap(~type, ncol=3) +
scale_fill_manual("trophic", values = c("#66FF33", "#FFFF00", "#FFCC99")) +
scale_color_manual("trophic", values = c("#66FF33", "#FFFF00", "#FFCC99")) +
theme(axis.text.x = element_text(angle=55))
这是一个使用 graphics
的解决方案(仅作为使用 layout
和自定义函数的大纲):
addfiglab <- function(lab, xl = par()$mar[2], yl = par()$mar[3], cex = 1) {
text(x = line2user(xl, 2), y = line2user(yl, 3),
lab, xpd = NA, font = 2, cex = cex, adj = c(0, 1))
}
line2user <- function(line, side, outer = FALSE) {
unit <- if (outer) "nic" else "npc"
lh <- par('cin')[2] * par('cex') * par('lheight')
x_off <- diff(grconvertX(c(0, lh), 'inches', unit))
y_off <- diff(grconvertY(c(0, lh), 'inches', unit))
switch(side,
`1` = grconvertY(-line * y_off, unit, 'user'),
`2` = grconvertX(-line * x_off, unit, 'user'),
`3` = grconvertY(1 + line * y_off, unit, 'user'),
`4` = grconvertX(1 + line * x_off, unit, 'user'),
stop("Side must be 1, 2, 3, or 4", call.=FALSE))
}
layout(matrix(c(rep(1, 9), 2:10), ncol = 3, byrow = TRUE))
plot(1:10); addfiglab("A")
for (i in 2:10) {plot(i); addfiglab(LETTERS[i])}
我想在一个图中使用不同的图形,但是我使用的命令 (grid.arrange
) 扭曲了图表的条形图。除此之外,我想将每个图表的标题(A、B、C ..)放在图表的顶部,而不是它显示的侧面。
在图 1 中,我有一个图表(正确的条),在第二个图表中,所有图表都放在一起。有人可以帮我重新调整这个命令吗?
谢谢
library(ggplot2)
library(ggpubr)
A<-ggplot(dados, aes(specie, A)) +
geom_col(alpha = 0.5, aes(colour = trophic, fill = trophic,
group = trophic)) +
rotate_x_text(angle = 55)
B<-ggplot(dados, aes(specie, B)) +
geom_col(alpha = 0.5, aes(colour = trophic, fill = trophic,
group = trophic)) +
rotate_x_text(angle = 55)
C<-ggplot(dados, aes(specie, C)) +
geom_col(alpha = 0.5, aes(colour = trophic, fill = trophic,
group = trophic)) +
rotate_x_text(angle = 55)
D<-ggplot(dados, aes(specie, D)) +
geom_col(alpha = 0.5, aes(colour = trophic, fill = trophic,
group = trophic)) +
rotate_x_text(angle = 55)
E<-ggplot(dados, aes(specie, E)) +
geom_col(alpha = 0.5, aes(colour = trophic, fill = trophic,
group = trophic)) +
rotate_x_text(angle = 55)
G<-ggplot(dados, aes(specie, G)) +
geom_col(alpha = 0.5, aes(colour = trophic, fill = trophic,
group = trophic)) +
rotate_x_text(angle = 55)
H<-ggplot(dados, aes(specie, H)) +
geom_col(alpha = 0.5, aes(colour = trophic, fill = trophic,
group = trophic)) +
rotate_x_text(angle = 55)
I<-ggplot(dados, aes(specie, I)) +
geom_col(alpha = 0.5, aes(colour = trophic, fill = trophic,
group = trophic)) +
rotate_x_text(angle = 55)
J<-ggplot(dados, aes(specie, J)) +
geom_col(alpha = 0.5, aes(colour = trophic, fill = trophic,
group = trophic)) +
rotate_x_text(angle = 55)
grid.arrange(A, B, C, D, E, G, H, I, J, ncol=3)
我的数据:
specie A B C D E G H I J bodymass trophic
AAThry_swin 72 0 76.8 63.2 0 0 0 1.6 0 4000 1
ABHys_afri 23.2 0 77.6 68.8 0 0 0 0 0 14936.02 1
ACSylv_grim 48 0 52 0 0.8 0 0 15.2 0 15639.15 1
ADTrag_scri 100 2.4 100 2.4 17.6 0 3.2 14.4 0 43250.39 1
AERed_aru 100 0.8 53.6 0 2.4 0 25.6 0 0 58059.24 1
AFHipp_eq 14.4 0 6.4 0 4.8 0 4 0 0 264173.96 1
AGTrag_oryx 16 0 15.2 0 4 0 0 0 0 562592.69 1
AHSync_caf 81.6 0 41.6 8.8 8.8 0 25.6 2.4 0 592665.98 1
AILox_afric 1.6 28 1.6 0.8 3.2 0 47.2 3.2 0 3824539.93 1
BAOtol_cras 7.2 0 1.6 22.4 0 0 0 0 0 1206.61 2
BBMiop_tal 1.6 0 1.6 54.4 0 0 0 0.8 0 1248.86 2
BCLep_cap 14.4 0 12 0 0 0 0 0 0 1500 2
BEGenet_gen 0 0 0 80.8 0 0 0 0 0 1756.17 2
BFPhil_mont 20.8 0 100 16.8 4.8 0 2.4 12.8 0 4896.05 2
BGChlor_cyn 72.8 0 16.8 100 0 0 0 1.6 0 5000.01 2
BHCerc_mit 5.6 0 11.2 100 0 0 0 0.8 0 5041.29 2
BICivet_civ 43.2 0 52.8 19.2 0 0 0 16.8 41.6 12075.58 2
BJPota_larv 100 0 84 64 14.4 0 1.6 18.4 1.6 69063.79 2
BLHipp_amph 22.4 0 0 8.8 0 0 0 4 0 1536310.4 2
CALept_serv 0.8 56 3.2 80 0 14.4 0 23.2 78.4 11999.96 3
CBLyc_pict 0.8 10.4 0 23.2 0 12 0.8 9.6 19.2 21999.99 3
CCCan_mes 1.6 0 0 20.8 0 6.4 0 32 0 22000 3
CDPant_pa 18.4 24 1.6 12 0 17.6 27.2 4 8 52399.99 3
CEOryct_afer 11.2 1.6 22.4 0 0 0 0 5.6 4.8 56175.2 3
CFCroc_croc 51.2 0 0 22.4 0 5.6 0 1.6 0 63369.98 3
CGPant_le 0 15.2 0.8 27.2 0 57.6 24 1.6 1.6 158623.93 3
你可以试试:
library(dplyr)
dados %>%
gather(type, value, -c("specie", "bodymass", "trophic")) %>%
ggplot(aes(x=specie, y=value)) + geom_col(alpha=0.5, aes(color=trophic, fill=trophic, group=trophic)) +
facet_wrap(~type, ncol=3) +
theme(axis.text.x = element_text(angle=55))
编辑:
要控制trophic的颜色,需要把它从连续变量改成因子:
dados %>%
gather(type, value, -c("specie", "bodymass", "trophic")) %>%
mutate(trophic = as.factor(trophic)) %>%
ggplot(aes(x=specie, y=value)) + geom_col(alpha=0.5, aes(color=trophic, fill=trophic, group=trophic)) +
facet_wrap(~type, ncol=3) +
scale_fill_manual("trophic", values = c("#66FF33", "#FFFF00", "#FFCC99")) +
scale_color_manual("trophic", values = c("#66FF33", "#FFFF00", "#FFCC99")) +
theme(axis.text.x = element_text(angle=55))
这是一个使用 graphics
的解决方案(仅作为使用 layout
和自定义函数的大纲):
addfiglab <- function(lab, xl = par()$mar[2], yl = par()$mar[3], cex = 1) {
text(x = line2user(xl, 2), y = line2user(yl, 3),
lab, xpd = NA, font = 2, cex = cex, adj = c(0, 1))
}
line2user <- function(line, side, outer = FALSE) {
unit <- if (outer) "nic" else "npc"
lh <- par('cin')[2] * par('cex') * par('lheight')
x_off <- diff(grconvertX(c(0, lh), 'inches', unit))
y_off <- diff(grconvertY(c(0, lh), 'inches', unit))
switch(side,
`1` = grconvertY(-line * y_off, unit, 'user'),
`2` = grconvertX(-line * x_off, unit, 'user'),
`3` = grconvertY(1 + line * y_off, unit, 'user'),
`4` = grconvertX(1 + line * x_off, unit, 'user'),
stop("Side must be 1, 2, 3, or 4", call.=FALSE))
}
layout(matrix(c(rep(1, 9), 2:10), ncol = 3, byrow = TRUE))
plot(1:10); addfiglab("A")
for (i in 2:10) {plot(i); addfiglab(LETTERS[i])}