简体   繁体   English

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

[英]Rotate x-axis text when using coord_polar()

I have a scatter-plot with many values on a polar axis - coord_polar() .我有一个极轴上有许多值的散点图 - 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?是否可以放置文本以使其沿极坐标 x 轴径向打印?


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).下面是我正在处理的图表示例,其中 x 轴是分类的,y 轴是连续变量(类似于示例)。

在此处输入图像描述

I understand this is an old topic, but I found a better solution for this problem, inspired from baptise's comment:我知道这是一个古老的话题,但我从 baptise 的评论中找到了一个更好的解决方案:

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

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. Yoplait 的回答有很大帮助,但仍然没有解决必须将图的一半颠倒阅读的问题。 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)
    )
  )

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)

在此处输入图片说明

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.你的 reprex 是一个特别困难的情况,x 轴上的双灌木和日期等等,所以我懒得让它工作,但是:查看geomtextpathgeom_textvline ,它的工作方式就像geom_vline但带有文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM