将标题添加到 ggplot 的顶部
Adding title to top of ggplot
我成功地创建了一个图,有 2 个方面。但是,我想创建第三个分组因素 - 前四列应标记为“A”,后四列应标记为“B”,标题居中但位于同一行。我已经添加了我正在寻找的图像。有谁知道如何在 ggplot2 中执行此操作?如果有某种空白列在视觉上将两组分开,那也很棒。我只想创建 2 个图并加入它们,但问题是在 left-most 组上添加轴会使框小于后四个框。为了保密,我不包括轴。
这是我的代码
Carbon.p<-ggplot(data.QC.1[!data.QC.1$hemiparasite %in% c("TRER", "TRVE", "CAHI"),], aes(x=type.full, y = Value))+
# geom_point(aes(color=type.full))+
stat_summary(fun = "mean", shape = "-", size = 2.5)+
scale_color_manual(values = c("#de2d26" ,"#9ebcda", "#8856a7")) +
facet_grid(host ~ hemiparasite, scales= "free", switch = "y")+
theme_bw()+
geom_jitter(aes(color = type.full), alpha = 0.5))+
theme(text = element_text(size = 10),
panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
# panel.border = element_blank(),
axis.line.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.line.y = element_blank(),
legend.title= element_blank(),
legend.position = "bottom",
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
legend.key = element_blank(),
strip.background = element_rect(colour="black", fill="white"),
# strip.text.y = element_blank())+
# axis.text.x = element_text(hjust = 0.5, angle = 90))+ ## why isn't this working
labs(x = expression(""),
y = expression("d13C"),
title = "")
谢谢!
选项 1 - 在绘图范围外使用注释或 geom_text,用 coord_cartesian
和 clip = off
.
约束视图 window
library(ggplot2); library(dplyr)
mtcars %>%
mutate(label = "") %>%
add_row(wt = 3.5, mpg = 42, am = 0, carb = c(2, 6), label = c("A", "B")) %>%
ggplot(aes(wt, mpg, label = label)) +
geom_point(data = . %>% filter(label == "")) +
geom_text() +
facet_grid(am~carb) +
coord_cartesian(ylim = c(10,35), clip = "off") +
theme(plot.margin = unit(c(1,0,0,0), "line"))
选项 2 - 用 patchwork
.
连接的两个单独的图表
library(patchwork)
plot_carbs <- function(my_carbs, label, axis = TRUE) {
mtcars %>%
filter(carb %in% my_carbs) %>%
ggplot(aes(wt, mpg)) +
geom_point() +
facet_grid(am~carb) +
labs(title = label) +
theme(plot.title = element_text(hjust = 0.5)) +
{ if(!axis) theme(
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.title.y = element_blank()) } +
coord_cartesian(xlim = c(1.5, 6), ylim = c(10,35))
}
plot_carbs(1:3, "A") +
plot_carbs(c(4,6,8), "B", FALSE)
我成功地创建了一个图,有 2 个方面。但是,我想创建第三个分组因素 - 前四列应标记为“A”,后四列应标记为“B”,标题居中但位于同一行。我已经添加了我正在寻找的图像。有谁知道如何在 ggplot2 中执行此操作?如果有某种空白列在视觉上将两组分开,那也很棒。我只想创建 2 个图并加入它们,但问题是在 left-most 组上添加轴会使框小于后四个框。为了保密,我不包括轴。
这是我的代码
Carbon.p<-ggplot(data.QC.1[!data.QC.1$hemiparasite %in% c("TRER", "TRVE", "CAHI"),], aes(x=type.full, y = Value))+
# geom_point(aes(color=type.full))+
stat_summary(fun = "mean", shape = "-", size = 2.5)+
scale_color_manual(values = c("#de2d26" ,"#9ebcda", "#8856a7")) +
facet_grid(host ~ hemiparasite, scales= "free", switch = "y")+
theme_bw()+
geom_jitter(aes(color = type.full), alpha = 0.5))+
theme(text = element_text(size = 10),
panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
# panel.border = element_blank(),
axis.line.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.line.y = element_blank(),
legend.title= element_blank(),
legend.position = "bottom",
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
legend.key = element_blank(),
strip.background = element_rect(colour="black", fill="white"),
# strip.text.y = element_blank())+
# axis.text.x = element_text(hjust = 0.5, angle = 90))+ ## why isn't this working
labs(x = expression(""),
y = expression("d13C"),
title = "")
谢谢!
选项 1 - 在绘图范围外使用注释或 geom_text,用 coord_cartesian
和 clip = off
.
library(ggplot2); library(dplyr)
mtcars %>%
mutate(label = "") %>%
add_row(wt = 3.5, mpg = 42, am = 0, carb = c(2, 6), label = c("A", "B")) %>%
ggplot(aes(wt, mpg, label = label)) +
geom_point(data = . %>% filter(label == "")) +
geom_text() +
facet_grid(am~carb) +
coord_cartesian(ylim = c(10,35), clip = "off") +
theme(plot.margin = unit(c(1,0,0,0), "line"))
选项 2 - 用 patchwork
.
library(patchwork)
plot_carbs <- function(my_carbs, label, axis = TRUE) {
mtcars %>%
filter(carb %in% my_carbs) %>%
ggplot(aes(wt, mpg)) +
geom_point() +
facet_grid(am~carb) +
labs(title = label) +
theme(plot.title = element_text(hjust = 0.5)) +
{ if(!axis) theme(
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.title.y = element_blank()) } +
coord_cartesian(xlim = c(1.5, 6), ylim = c(10,35))
}
plot_carbs(1:3, "A") +
plot_carbs(c(4,6,8), "B", FALSE)