使用 coord_polar() 时旋转 x 轴文本

发布于 2024-12-10 15:07:04 字数 379 浏览 0 评论 0原文

我有一个在极轴上有许多值的散点图 - coord_polar()。生成的图表包含拥挤的文本,因为文本始终与页面底部对齐。

是否可以放置文本,使其沿极 x 轴径向打印?


编辑提供一个示例:

qplot(data=presidential, name,end) + coord_polar()

在总统案例中,我希望看到总统的名字与他们所在的轴/辐条成一定角度。下面是我正在处理的图表示例,其中 x 轴是分类变量,y 轴是连续变量(与示例类似)。

在此处输入图像描述

I have a scatter-plot with many values on a polar axis - coord_polar(). The resulting graph has crowded text because the text is always aligned with the bottom of the page.

Is it possible to place the text so that it is printed radially along the polar x-axis?


Edited to provide an example:

qplot(data=presidential, name,end) + coord_polar()

In the presidential case I would like to see the presidential names angled to align with the axis/spoke they are on. Below is an example of the graph I am working on where the x axis is categorical and the y-axis is a continuous variable (similar to the example).

enter image description here

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

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

发布评论

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

评论(5

厌倦 2024-12-17 15:07:04

我知道这是一个老话题,但我发现了一个更好的解决方案来解决这个问题,灵感来自巴普蒂斯的评论:

ggplot(data, aes(x=someId, y=someValue)) +
  geom_point() + 
  coord_polar() +
  theme(axis.text.x = element_text(
    angle= -90 - 360 / length(unique(data$someId)) * seq_along(data$someId)
    )
  )

I understand this is an old topic, but I found a better solution for this problem, inspired from baptise's comment:

ggplot(data, aes(x=someId, y=someValue)) +
  geom_point() + 
  coord_polar() +
  theme(axis.text.x = element_text(
    angle= -90 - 360 / length(unique(data$someId)) * seq_along(data$someId)
    )
  )
白首有我共你 2024-12-17 15:07:04

Yoplait 的答案很有帮助,但仍然没有解决必须在图表的一半中颠倒阅读的问题。这张海报提出的想法的延伸如下。

首先准备角度向量,确保我们将图分成两部分:

sequence_length = length(unique(data$someId))
first_sequence = c(1:(sequence_length%/%2)) 
second_sequence = c((sequence_length%/%2+1):sequence_length) 
first_angles = c(90 - 180/length(first_sequence) * first_sequence)
second_angles = c(-90 - 180/length(second_sequence) * second_sequence)

现在我们可以附加角度向量来制作实际的图:

ggplot(data, aes(x=someId, y=someValue)) +
  geom_point() + 
  coord_polar() +
  theme(axis.text.x = element_text(
    angle= c(first_angles,second_angles)
    )
  )

The answer by Yoplait helps a lot, but still does not solve the issue of having to read upside down in half of the graph. An extention of the idea that this poster proposes is as follows.

First prepare the angle vector, making sure we split the graph in two:

sequence_length = length(unique(data$someId))
first_sequence = c(1:(sequence_length%/%2)) 
second_sequence = c((sequence_length%/%2+1):sequence_length) 
first_angles = c(90 - 180/length(first_sequence) * first_sequence)
second_angles = c(-90 - 180/length(second_sequence) * second_sequence)

And now we can append the angle vectors to make the actual graph:

ggplot(data, aes(x=someId, y=someValue)) +
  geom_point() + 
  coord_polar() +
  theme(axis.text.x = element_text(
    angle= c(first_angles,second_angles)
    )
  )
多彩岁月 2024-12-17 15:07:04

这是一个不太优雅的坐标示例:

CoordPolar2 <- proto(CoordPolar, {
  objname <- "polar2"
  guide_foreground <- function(., details, theme) {
    theta <- .$theta_rescale(details$theta.major, details)
    labels <- details$theta.labels

    # Combine the two ends of the scale if they are close
    theta <- theta[!is.na(theta)]
    ends_apart <- (theta[length(theta)] - theta[1]) %% (2*pi)
    if (ends_apart < 0.05) {
      n <- length(labels)
      if (is.expression(labels)) {
        combined <- substitute(paste(a, "/", b), 
          list(a = labels[[1]], b = labels[[n]]))
      } else {
        combined <- paste(labels[1], labels[n], sep="/")
      }
      labels[[n]] <- combined
      labels <- labels[-1]
      theta <- theta[-1]
    }

    grobTree(
      if (length(labels) > 0) {
        lab <- theme_render(
          theme, "axis.text.x", 
          labels, 0.45 * sin(theta) + 0.5, 0.45 * cos(theta) + 0.5,
          hjust = 0.5, vjust = 0.5,
          default.units="native"
        )
        lab$rot <- (pi/2 - theta) / pi * 180
        lab
      },
      theme_render(theme, "panel.border")
    )
  }
})

coord_polar2 <- CoordPolar2$build_accessor()

p <- qplot(data=presidential, name,end) + coord_polar2()
print(p)

在此处输入图像描述

here is a not elegant example of the coordinate:

CoordPolar2 <- proto(CoordPolar, {
  objname <- "polar2"
  guide_foreground <- function(., details, theme) {
    theta <- .$theta_rescale(details$theta.major, details)
    labels <- details$theta.labels

    # Combine the two ends of the scale if they are close
    theta <- theta[!is.na(theta)]
    ends_apart <- (theta[length(theta)] - theta[1]) %% (2*pi)
    if (ends_apart < 0.05) {
      n <- length(labels)
      if (is.expression(labels)) {
        combined <- substitute(paste(a, "/", b), 
          list(a = labels[[1]], b = labels[[n]]))
      } else {
        combined <- paste(labels[1], labels[n], sep="/")
      }
      labels[[n]] <- combined
      labels <- labels[-1]
      theta <- theta[-1]
    }

    grobTree(
      if (length(labels) > 0) {
        lab <- theme_render(
          theme, "axis.text.x", 
          labels, 0.45 * sin(theta) + 0.5, 0.45 * cos(theta) + 0.5,
          hjust = 0.5, vjust = 0.5,
          default.units="native"
        )
        lab$rot <- (pi/2 - theta) / pi * 180
        lab
      },
      theme_render(theme, "panel.border")
    )
  }
})

coord_polar2 <- CoordPolar2$build_accessor()

p <- qplot(data=presidential, name,end) + coord_polar2()
print(p)

enter image description here

无尽的现实 2024-12-17 15:07:04

编辑:现在是 ggplot2 3.5.0+ 的一部分

ggplot2 的开发版本刚刚添加了新的 coord_radial()guide_axis_theta(),只需设置轴即可使此问题变得更容易角度。

library(ggplot2)
packageVersion("ggplot2") # Development version
#> [1] '3.4.4.9000'

p <- ggplot(presidential, aes(name, end)) +
  geom_point() +
  coord_radial()

p + guides(theta = guide_axis_theta(angle = 90))

同样的策略也适用于将文本角度设置为圆的切线。

p + guides(theta = guide_axis_theta(angle = 0))

创建于 2023 年 11 月 20 日,使用 reprex v2.0.2

EDIT: now part of ggplot2 3.5.0+

The development version of ggplot2 just added a new coord_radial() and guide_axis_theta() that makes this problem easier by just setting the axis angle.

library(ggplot2)
packageVersion("ggplot2") # Development version
#> [1] '3.4.4.9000'

p <- ggplot(presidential, aes(name, end)) +
  geom_point() +
  coord_radial()

p + guides(theta = guide_axis_theta(angle = 90))

The same strategy also works for setting the text angle as a tangent to the circle.

p + guides(theta = guide_axis_theta(angle = 0))

Created on 2023-11-20 with reprex v2.0.2

柳若烟 2024-12-17 15:07:04

你的reprex是一个有点特别困难的情况,x轴上有双布什和日期等等,所以我懒得让它工作,但是:看看< a href="https://allancameron.github.io/geomtextpath/" rel="nofollow noreferrer">geomtextpath 的 geom_textvline,其工作方式类似于geom_vline 但带有文本。

Your reprex is a somewhat particularly difficult case, what with the double Bushs and Dates on the x-axis and all, so I couldn't be bothered to make it work for that, but: check out geomtextpath's geom_textvline, it works like geom_vline but with text.

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