ggplot2 - 极坐标图的轴

时间:2015-11-19 15:21:50

标签: r ggplot2

有没有办法在ggplot2中绘制轴上的轴(可能是一些提示)     themescale_x_continuous)?

enter image description here

我的意思是不是水平方向!但是对称的"比例序列从100到100,中心在20。

感谢您的建议!

默认情况下看起来像这样:

enter image description here 代码:

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.10,386.89,383.05),
ci1 = c(388.37,367.67,378.98,381.76,388.63,386.65,388.32,388.50,389.03,387.25,389.05,388.65,385.64,381.10),
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)),.Names = c("wd", "co2", "ci1", "ci2"), row.names = c(NA, -14L), class = "data.frame")

1 个答案:

答案 0 :(得分:0)

假设原始图名为p,并且在对称中心处已经有一个轴标签/刻度线(否则该用例将无济于事),下面是hack应该起作用:

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)

plot

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

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