我成功地創建了一個情節,有 2 個方面。但是,我想創建第三個分組因素 - 前四列應標記為“A”,后四列應標記為“B”,標題居中但位于同一行。我已經添加了我正在尋找的影像。有誰知道如何在 ggplot2 中做到這一點?如果有某種空白列在視覺上將兩個組分開也很棒。我只想創建 2 個圖并加入它們,但問題是在最左邊的組上添加軸會使框小于后四個框。為保密起見,我不在這里包括軸。
這是我的代碼
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 = "")
謝謝!
uj5u.com熱心網友回復:
選項 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)

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/358883.html
