[英]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 = ...)
進行自定義,但到目前為止它並沒有達到我想要的效果。 還有其他建議嗎? 謝謝!
您可以簡單地調整 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())
使用注釋可能更容易?
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.