gpt4 book ai didi

r - 在ggplot中向下移动刻面标签

转载 作者:行者123 更新时间:2023-12-05 04:07:07 27 4
gpt4 key购买 nike

如何移动构面标签(A、B、C),使它们位于左上角的方框内?

enter image description here

示例数据集:

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()

enter image description here

关于r - 在ggplot中向下移动刻面标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48935569/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com