在 ggplot + coord_polar 中自定义 x 轴标签位置
customize x axis label placement in ggplot + coord_polar
我正在使用 coord_polar
将物种丰度随时间的变化显示为时钟图,如下所示
year<-c(2017, 2018, 2019) # observation dates
sp1<-c(39,55,85)
sp2 <-c(100, 75, 65)
sp3 <-c(32,57,42)
df <-data.frame(year, sp1, sp2, sp3)
d <- df %>%
pivot_longer(!year, names_to = "species", values_to = "cover")
plot <-ggplot(d, aes(year, cover, color = species)) +
geom_line(size = 2) + coord_polar() + theme_classic() +
theme(text = element_text(size = 18),
legend.text = element_text(face = "italic"),
#axis.text.x=element_blank(), # remove all x axis labels in polar_coord
#axis.text.x=element_text(angle = 25), # rotates angle, but does not increase distance between labels.
legend.title=element_blank(),
legend.position="right") +
scale_x_continuous(breaks=c(2019, 2018, 2017)) # this customizes the x-axis labels to be my
# exact observation dates. List order places 2019 at the 11:59 "clock" position,
# and 2017 at the 12:01 "clock" position)
plot
我看到的问题是 2017 年和 2019 年的标签太接近了,很难在视觉上将它们解释为时钟的开始和停止。有没有办法增加它们之间的间距? (见下面的示例图片)
我一直在尝试使用上面评论的 theme(axis.text.x = ...)
来自定义,但到目前为止它没有按照我的要求进行。还有其他建议吗?谢谢!
也许用注释更容易?
ggplot(d, aes(year, cover, color = species)) +
geom_line(size = 2) +
annotate("text", size = 6, label = year,
x = c(2017.1, 2018, 2018.9),
y = c(110, 90, 110)) +
coord_polar() +
theme_classic() +
theme(text = element_text(size = 18),
legend.text = element_text(face = "italic"),
#axis.text.x=element_blank(), # remove all x axis labels in polar_coord
#axis.text.x=element_text(angle = 25), # rotates angle, but does not increase distance between labels.
legend.title=element_blank(),
legend.position="right") +
scale_x_continuous(labels = NULL)
您可以简单地调整 x 轴上的分隔符和标签。我认为它还改善了具有弯曲标签和一些年份分隔符的外观:
library(geomtextpath)
ggplot(d, aes(year, cover, color = species)) +
geom_rect(data = data.frame(y = c(2017, 2017 + 2/3, 2018 + 1/3)),
aes(ymax = Inf, ymin = -Inf, xmin = y, xmax = y + 2/3),
inherit.aes = FALSE, alpha = 0.05, color = "gray75") +
geom_line(size = 2) +
coord_curvedpolar() +
theme_classic() +
scale_x_continuous(breaks = c(2017.33, 2018, 2018.67), labels = 2017:2019) +
theme(text = element_text(size = 18),
legend.text = element_text(face = "italic"),
legend.title=element_blank(),
legend.position="right",
axis.line.x.bottom = element_blank())
我正在使用 coord_polar
将物种丰度随时间的变化显示为时钟图,如下所示
year<-c(2017, 2018, 2019) # observation dates
sp1<-c(39,55,85)
sp2 <-c(100, 75, 65)
sp3 <-c(32,57,42)
df <-data.frame(year, sp1, sp2, sp3)
d <- df %>%
pivot_longer(!year, names_to = "species", values_to = "cover")
plot <-ggplot(d, aes(year, cover, color = species)) +
geom_line(size = 2) + coord_polar() + theme_classic() +
theme(text = element_text(size = 18),
legend.text = element_text(face = "italic"),
#axis.text.x=element_blank(), # remove all x axis labels in polar_coord
#axis.text.x=element_text(angle = 25), # rotates angle, but does not increase distance between labels.
legend.title=element_blank(),
legend.position="right") +
scale_x_continuous(breaks=c(2019, 2018, 2017)) # this customizes the x-axis labels to be my
# exact observation dates. List order places 2019 at the 11:59 "clock" position,
# and 2017 at the 12:01 "clock" position)
plot
我看到的问题是 2017 年和 2019 年的标签太接近了,很难在视觉上将它们解释为时钟的开始和停止。有没有办法增加它们之间的间距? (见下面的示例图片)
我一直在尝试使用上面评论的 theme(axis.text.x = ...)
来自定义,但到目前为止它没有按照我的要求进行。还有其他建议吗?谢谢!
也许用注释更容易?
ggplot(d, aes(year, cover, color = species)) +
geom_line(size = 2) +
annotate("text", size = 6, label = year,
x = c(2017.1, 2018, 2018.9),
y = c(110, 90, 110)) +
coord_polar() +
theme_classic() +
theme(text = element_text(size = 18),
legend.text = element_text(face = "italic"),
#axis.text.x=element_blank(), # remove all x axis labels in polar_coord
#axis.text.x=element_text(angle = 25), # rotates angle, but does not increase distance between labels.
legend.title=element_blank(),
legend.position="right") +
scale_x_continuous(labels = NULL)
您可以简单地调整 x 轴上的分隔符和标签。我认为它还改善了具有弯曲标签和一些年份分隔符的外观:
library(geomtextpath)
ggplot(d, aes(year, cover, color = species)) +
geom_rect(data = data.frame(y = c(2017, 2017 + 2/3, 2018 + 1/3)),
aes(ymax = Inf, ymin = -Inf, xmin = y, xmax = y + 2/3),
inherit.aes = FALSE, alpha = 0.05, color = "gray75") +
geom_line(size = 2) +
coord_curvedpolar() +
theme_classic() +
scale_x_continuous(breaks = c(2017.33, 2018, 2018.67), labels = 2017:2019) +
theme(text = element_text(size = 18),
legend.text = element_text(face = "italic"),
legend.title=element_blank(),
legend.position="right",
axis.line.x.bottom = element_blank())