在ggplot中向下移动刻面标签
shifting facet labels downward in ggplot
如何移动构面标签(A、B、C),使它们位于方框内的左上角?
示例数据集:
date <- as.factor(rep(c(1,2,3,4,5,6,7,8), 7))
chlconc <- runif(56, min = 0.1, max = 15.9)
colony <- as.factor(c(rep("A", 24), rep("B", 16), rep("C", 16)))
year <- as.factor(c(rep("2014", 8), rep("2016", 8), rep("2017", 8), rep("2016", 8), rep("2017", 8), rep("2014", 8), rep("2017", 8)))
graphvalues <- data.frame(year, date, colony, chlconc)
ggplot代码
library(ggplot2)
library(ggthemes)
pd <- position_dodge(0.1)
ggplot(graphvalues, aes(date, chlconc, group = year)) +
facet_wrap(~colony, ncol = 1, labeller = label_parsed) +
geom_line(aes(color = year), position = pd) +
geom_point(aes(color = year), position = pd) +
scale_y_continuous(limit=c(0, 16), breaks=c(0, 4, 8, 12, 16), labels=c(0, 4, 8, 12, 16)) +
scale_color_discrete(name="Year") +
geom_hline(yintercept=2, linetype="dashed") +
theme(legend.text=element_text(size=14)) +
theme(axis.text=element_text(size=14)) +
theme(axis.title=element_text(size=18)) +
theme(legend.title=element_text(size=14)) +
theme_few() +
theme(strip.text = element_text(hjust = 0, size = 14, face = "bold"))
分面标签位于绘图区域之外,我认为没有办法让它们进入您的绘图区域。但是您可以通过添加一个 geom_text()
层并将您的分面变量作为标签,并为 geom_text()
层设置 x 和 y 值,将其放置在您的左上角,从而在绘图区域内获得标签图形。然后您还删除了小平面标签。
ggplot(graphvalues, aes(date, chlconc, group = year)) +
facet_wrap(~colony, ncol = 1) +
geom_line(aes(color = year), position = pd) +
geom_point(aes(color = year), position = pd) +
scale_y_continuous(limit=c(0, 16), breaks=c(0, 4, 8, 12, 16), labels=c(0, 4, 8, 12, 16)) +
scale_color_discrete(name="Year") +
geom_hline(yintercept=2, linetype="dashed") +
theme_classic() +
theme(strip.text = element_blank(), #remove strip text
strip.background = element_blank()) + #remove strip rectangles
geom_text(aes(label = colony, x = 0.75, y = 15.5)) #add titles using geom_text()
如何移动构面标签(A、B、C),使它们位于方框内的左上角?
示例数据集:
date <- as.factor(rep(c(1,2,3,4,5,6,7,8), 7))
chlconc <- runif(56, min = 0.1, max = 15.9)
colony <- as.factor(c(rep("A", 24), rep("B", 16), rep("C", 16)))
year <- as.factor(c(rep("2014", 8), rep("2016", 8), rep("2017", 8), rep("2016", 8), rep("2017", 8), rep("2014", 8), rep("2017", 8)))
graphvalues <- data.frame(year, date, colony, chlconc)
ggplot代码
library(ggplot2)
library(ggthemes)
pd <- position_dodge(0.1)
ggplot(graphvalues, aes(date, chlconc, group = year)) +
facet_wrap(~colony, ncol = 1, labeller = label_parsed) +
geom_line(aes(color = year), position = pd) +
geom_point(aes(color = year), position = pd) +
scale_y_continuous(limit=c(0, 16), breaks=c(0, 4, 8, 12, 16), labels=c(0, 4, 8, 12, 16)) +
scale_color_discrete(name="Year") +
geom_hline(yintercept=2, linetype="dashed") +
theme(legend.text=element_text(size=14)) +
theme(axis.text=element_text(size=14)) +
theme(axis.title=element_text(size=18)) +
theme(legend.title=element_text(size=14)) +
theme_few() +
theme(strip.text = element_text(hjust = 0, size = 14, face = "bold"))
分面标签位于绘图区域之外,我认为没有办法让它们进入您的绘图区域。但是您可以通过添加一个 geom_text()
层并将您的分面变量作为标签,并为 geom_text()
层设置 x 和 y 值,将其放置在您的左上角,从而在绘图区域内获得标签图形。然后您还删除了小平面标签。
ggplot(graphvalues, aes(date, chlconc, group = year)) +
facet_wrap(~colony, ncol = 1) +
geom_line(aes(color = year), position = pd) +
geom_point(aes(color = year), position = pd) +
scale_y_continuous(limit=c(0, 16), breaks=c(0, 4, 8, 12, 16), labels=c(0, 4, 8, 12, 16)) +
scale_color_discrete(name="Year") +
geom_hline(yintercept=2, linetype="dashed") +
theme_classic() +
theme(strip.text = element_blank(), #remove strip text
strip.background = element_blank()) + #remove strip rectangles
geom_text(aes(label = colony, x = 0.75, y = 15.5)) #add titles using geom_text()