使用coord_polar()时,旋转ggplot2中的x轴文本 [英] rotate x-axis text in ggplot2 when using coord_polar()

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

问题描述

我使用 ggplot2 绘制极轴上许多值的散点图 - coord_polar()。生成的图形拥挤,因为文本总是与页面底部对齐。是否可以放置文本,以便沿着极坐标x轴径向打印?



编辑以提供示例:

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

在总统案中,我希望看到总统的名字倾向于与他们所在的轴/发言相一致。下面是我正在处理的图表的一个例子,其中x轴是分类的,y轴是一个连续变量(类似于这个例子)。

解决方案

这里是一个不太好的坐标示例:

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

#如果它们靠近
theta< - theta [!is.na(theta)]
ends_apart,如果(ends_apart <0.05){
n< - 长度(标签)$ b $,那么<θ(θ[长度θ]-θ[1])%%(2 * pi)如果(is.expression(labels)){
combined < - substitute(paste(a,/,b),
list(a = labels [[1]]) b = labels [[n]]))
} else {
combined < - paste(labels [1],labels [n],sep =/)
}
个标签[[n]] < - 组合
标签< - 标签[-1]
theta < - theta [-1]
}

grobTree(
if(长度(标签)> 0){
lab< - theme_render(
theme,axis.text.x,
labels,0.45 * sin(theta)+0.5,0.45 *cosθ+ 0.5 ,
hjust = 0.5,vjust = 0.5,
default.units =native

lab $ rot < - (pi / 2-θ)/ pi * 180
lab
},
theme_render(theme,panel.border)

}
})

coord_polar2 < - CoordPolar2 $ build_accessor()

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


I am using ggplot2 to plot a scatterplot of 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).

解决方案

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()时,旋转ggplot2中的x轴文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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