自定义X轴标签位置在GGPLOT中+ coord_polar

发布于 2025-02-02 00:40:09 字数 1388 浏览 3 评论 0原文

我正在使用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年的标签是如此近,因此在视觉上很难将其解释为开始和停止时钟。有没有办法增加它们之间的间距? (请参阅下面的示例图)

我一直在尝试使用主题(axis.text.x = ...),如上所述进行自定义,但到目前为止,它还没有做我想要的。还有其他建议吗?谢谢!

I'm using coord_polar to show changes in species abundance over time as a clock plot as shown below

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

The issue that I see is that the 2017 and 2019 labels are so close it's visually hard to interpret these as the start and stop of the clock. Is there a way to increase the spacing between them? (see example pic below)

I've been trying to use theme(axis.text.x = ...) as commented above to customize, but so far it's not doing what I want. Any other suggestions? Thanks!

plot I want

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天邊彩虹 2025-02-09 00:40:09

您可以简单地调整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()) 

”在此处输入图像描述”

You can simply adjust the breaks and labels on the x axis. I think it also improves the appearance to have curved labels and perhaps some year dividers:

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

enter image description here

白日梦 2025-02-09 00:40:09

注释也许更容易?

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) 

Perhaps easier with an annotation?

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) 

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文