在GGPLOT中仅移动一些X轴标签

发布于 2025-01-17 21:32:47 字数 613 浏览 4 评论 0原文

请参阅此图:

tibble(x = 0:10, y = rnorm(11)) %>%
  ggplot(aes(x,y)) +
  geom_point() +
  scale_x_continuous(breaks = c(seq(0,10,2)/10, seq(2,10,1)))

“在此处输入图像描述”

轴开头的标签非常拥挤。有没有一种方法可以仅推下一些标签(例如c(0.2、0.6、0.8)),以便所有标签都可以读取?奖励:从tick滴到那些被推下的标签(换句话说,刻度更长)。

我知道我可以使用vjust = -5作为+ theme(axis.text.x = element_text(vjust = -5))),但这会推下所有标签。

See this plot:

tibble(x = 0:10, y = rnorm(11)) %>%
  ggplot(aes(x,y)) +
  geom_point() +
  scale_x_continuous(breaks = c(seq(0,10,2)/10, seq(2,10,1)))

enter image description here

The labels in the beginning of the axis are very crowded. Is there a way to push down only some labels (e.g. those of c(0.2, 0.6, 0.8)) so that all labels would be readable? Bonus: add a vertical line from ticks to those labels which have been pushed down (in other words make the tick longer).

I know I can use vjust = -5 as in + theme(axis.text.x = element_text(vjust = -5)) but that would push down all labels.

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

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

发布评论

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

评论(2

孤独陪着我 2025-01-24 21:32:47

这是一种非常简单的方法,它通过在Vector X_TICKS中的标签之前添加新的行字符\ n来向下推下轴文本。

library(tidyverse)
x_ticks <- c(seq(0,10,2)/10, seq(2,10,1))

tibble(x = 0:10, y = rnorm(11)) %>%
  ggplot(aes(x,y)) +
  geom_point() +
  scale_x_continuous(breaks = x_ticks, 
                     labels = ifelse(x_ticks %in% c(0.2, 0.6, 0.8), 
                                     paste0("\n", x_ticks), x_ticks))

“”

在2022-03-30上由 reprex软件包(v2.0.1)

Here is a very simple method that pushes down the axis text by adding a new line character \n before the label in the vector x_ticks.

library(tidyverse)
x_ticks <- c(seq(0,10,2)/10, seq(2,10,1))

tibble(x = 0:10, y = rnorm(11)) %>%
  ggplot(aes(x,y)) +
  geom_point() +
  scale_x_continuous(breaks = x_ticks, 
                     labels = ifelse(x_ticks %in% c(0.2, 0.6, 0.8), 
                                     paste0("\n", x_ticks), x_ticks))

Created on 2022-03-30 by the reprex package (v2.0.1)

心凉怎暖 2025-01-24 21:32:47

如果您确实想要那些长刻度,这就是如何以困难的方式做到这一点

tibble(x = 0:10, y = rnorm(11)) %>%
  ggplot(aes(x,y)) +
  geom_vline(xintercept = seq(2, 10, 4)/10, color = "white", size = 0.5) +
  geom_point() +
  scale_x_continuous(breaks = c(seq(0,10,4)/10, seq(2,10,1))) +
  coord_cartesian(clip = "off", ylim = c(-2, 2)) +
  annotate("text", label = format(seq(2, 10, 4)/10, nsmall = 1),
           x = seq(2, 10, 4)/10, y = -2.4,
           size = 3) +
  annotate("segment", x = seq(2, 10, 4)/10, xend = seq(2, 10, 4)/10,
           y = -2.33, yend = -2.2, size = 0.2) +
  theme(axis.text = element_text(color = "black"))

在此处输入图像描述

This is how to do it the hard way, if you really want those long ticks

tibble(x = 0:10, y = rnorm(11)) %>%
  ggplot(aes(x,y)) +
  geom_vline(xintercept = seq(2, 10, 4)/10, color = "white", size = 0.5) +
  geom_point() +
  scale_x_continuous(breaks = c(seq(0,10,4)/10, seq(2,10,1))) +
  coord_cartesian(clip = "off", ylim = c(-2, 2)) +
  annotate("text", label = format(seq(2, 10, 4)/10, nsmall = 1),
           x = seq(2, 10, 4)/10, y = -2.4,
           size = 3) +
  annotate("segment", x = seq(2, 10, 4)/10, xend = seq(2, 10, 4)/10,
           y = -2.33, yend = -2.2, size = 0.2) +
  theme(axis.text = element_text(color = "black"))

enter image description here

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