使用coord_polar()时旋转x轴文本 [英] Rotate x-axis text when using coord_polar()

查看:89
本文介绍了使用coord_polar()时旋转x轴文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在极轴上有很多值的散点图- coord_polar().由于文本始终与页面底部对齐,因此结果图上的文本比较拥挤.

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.

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

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

经过编辑以提供示例:

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

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

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

推荐答案

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

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)

这篇关于使用coord_polar()时旋转x轴文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆