极坐标图的对称轴标签 [英] Symmetrical axis labels for polar plot

查看:97
本文介绍了极坐标图的对称轴标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种使用ggplot2绘制轴的方法,如下图所示. (也许是themescale_x_continuous中的一些提示?)

I'm looking for a way to plot axis like on picture below, using ggplot2. (maybe some tips in theme or scale_x_continuous?)

我的意思是标签的对称序列,在此示例中从100到100,中心为20(不是轴的水平方向).

I mean a symmetrical sequence of labels, which goes from 100 to 100 in this example with the center at 20 (not a horizontal orientation for the axis).

可复制的示例:

library(ggplot2)
required(grid)
ggplot(data = i, aes(x = wd, y = co2)) +
  ggtitle("CO2")+
  geom_point(size=4, colour = "red")+
  geom_linerange(aes(ymax =ci2, ymin=ci1), colour = "red", size = 2)+
  coord_polar()+
  
  geom_hline(yintercept = seq(365, 405, by = 5), colour = "grey", size = 0.2) +
  geom_vline(xintercept = seq(0, 360, by = 22.5), colour = "grey", size = 0.2) +
  scale_x_continuous(limits = c(0, 360), expand = c(0, 0), 
                     breaks = seq(0, 359.99, by = 22.5), 
                     labels=c("N","NNE","NE","ENE","E","ESE","SE","SSE",
                              "S","SSW","SW","WSW","W","WNW","NW","NNW")) +
  scale_y_continuous(limits = c(365, 405), breaks = seq(365, 405, by = 10)) +
  
  theme_bw() +
  theme(panel.border = element_blank(),
        panel.grid  = element_blank(),
        legend.key = element_blank(),
        axis.ticks = element_line(colour = "grey"),
        axis.ticks.length = unit(-1, "lines"),
        axis.ticks.margin = unit(1.3,"lines"),
        axis.text =  element_text(size=24),
        axis.text.y = element_text(size=24),
        axis.title = element_blank(),
        axis.line=element_line(),
        axis.line.x=element_blank(),
        axis.line.y = element_line(colour = "grey"),
        plot.title = element_text(hjust = 0, size = 20))

数据:

i <- structure(list(wd = c(0, 112.5, 135, 180, 202.5, 22.5, 225, 247.5, 
270, 292.5, 337.5, 45, 67.5, 90), co2 = c(389.82, 376.82, 386.06, 
392.04, 392.55, 387.97, 391.45, 389.87, 390.12, 389.68, 391.39, 
390.1, 386.89, 383.05), ci1 = c(388.37, 367.67, 378.98, 381.76, 
388.63, 386.65, 388.32, 388.5, 389.03, 387.25, 389.05, 388.65, 
385.64, 381.1), ci2 = c(391.26, 385.98, 393.15, 402.31, 396.46, 
389.28, 394.58, 391.23, 391.21, 392.12, 393.73, 391.55, 388.15, 
385.01)), row.names = c(NA, -14L), class = "data.frame")

推荐答案

假定原始绘图名为p,并且在对称中心处已经有一个轴标签/刻度线(否则,用例没有意义),以下grob hack应该可以工作:

Assuming the original plot was named p, and there is already an axis label / tick at the symmetric centre (otherwise this use case won't make sense), the following grob hack should work:

gp <- ggplotGrob(p)

# flip labels
new.labels <- gp$grobs[[which(gp$layout$name == "axis-l")]]$children[[2]]$grobs[[1]]$children[[1]]
new.labels$label <- c(rev(new.labels$label[-1]), new.labels$label)
new.labels$x <- unit.c(rep(new.labels$x[1], length(new.labels$x) - 1),
                       new.labels$x)
new.labels$y <- unit.c(rev(2*new.labels$y[1] - new.labels$y[-1]),
                       new.labels$y)

# flip ticks
new.ticks <- gp$grobs[[which(gp$layout$name == "axis-l")]]$children[[2]]$grobs[[2]]
new.ticks$x <- unit.c(rep(new.ticks$x[1:2], length(new.ticks$x)/2 - 1),
                      new.ticks$x)
new.ticks$y <- new.labels$y[rep(seq_along(new.labels$y), each = 2)]
new.ticks$id.lengths <- rep(new.ticks$id.lengths[1], length(new.ticks$y))

# assign flipped labels / ticks back to plot 
new.labels -> gp$grobs[[which(gp$layout$name == "axis-l")]]$children[[2]]$grobs[[1]]$children[[1]]
new.ticks -> gp$grobs[[which(gp$layout$name == "axis-l")]]$children[[2]]$grobs[[2]]

# show plot
grid::grid.draw(gp)

(注意:与问题相比,此处为p定义了较少的主题规范)

(Note: compared to the question, p is defined here with fewer theme specifications, for simplicity)

p <- ggplot(data = i, aes(x = wd, y = co2)) +
  ggtitle("CO2")+
  geom_point(size=4, colour = "red")+
  geom_linerange(aes(ymax =ci2, ymin=ci1), colour = "red", size = 2)+
  coord_polar()+
  
  geom_hline(yintercept = seq(365, 405, by = 5), 
             colour = "grey", size = 0.2) +
  geom_vline(xintercept = seq(0, 360, by = 22.5), 
             colour = "grey", size = 0.2) +
  scale_x_continuous(limits = c(0, 360), expand = c(0, 0), 
                     breaks = seq(0, 359.99, by = 22.5), 
                     labels=c("N","NNE","NE","ENE","E","ESE","SE","SSE","S","SSW","SW","WSW","W","WNW","NW","NNW")) +
  scale_y_continuous(limits = c(365, 405), 
                     breaks = seq(365, 405, by = 10)) +
  
  theme_void() +
  theme(axis.ticks = element_line(colour = "grey"),
        axis.ticks.length = unit(-0.5, "lines"),
        axis.text =  element_text(size=12, 
                                  margin = unit(rep(1, 4), "lines")),
        axis.line.y = element_line(colour = "grey"))

这篇关于极坐标图的对称轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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