R ggplot:将y轴移动到极坐标图上的网格线(Polar_Coord)

时间:2018-04-12 19:25:16

标签: r ggplot2 axis-labels polar-coordinates

我正在创建一个极坐标图,显示配对组数据的方向直方图。更具体地说,在不同的兄弟群体之间旅行的方向。

这是一个模拟:

mockdf <- data.frame(dir = as.numeric( runif( 1000, -pi/2, pi) ),
                 ID = sample(letters[1:2], 1000, TRUE))
ggplot(data=mockdf, aes(x=mockdf$dir)) +
  coord_polar(theta = "x", start = pi, direction = 1) +
  scale_fill_manual(name = "Sibling", values=c("black", "White")) +
  geom_histogram(bins=32, aes(fill=mockdf$ID), color= "black") +
  facet_wrap(~mockdf$ID) +
  scale_y_continuous("Number of reloactions", limits = c(-8,30)) +
  scale_x_continuous(limits = c(-pi,pi), breaks = c(0, pi/4, pi/2, 3*pi/4, 
  pi, -3*pi/4, -pi/2, -pi/4),
                     labels = c("N", "NE", "E", "SE", "S", "SW", "W", "NW"))

Polar Plot Example

我想将0,10,20,30的y轴标签移动到网格本身上(即沿SW方向),但是这样做有困难。有谁知道我是怎么做的?

1 个答案:

答案 0 :(得分:1)

您可以使用theme中的选项删除当前轴文本和刻度线,并geom_text()手动添加标签。我已经演示了如何添加第一个。

ggplot(data=mockdf, aes(x=mockdf$dir)) +
  coord_polar(theta = "x", start = pi, direction = 1) +
  scale_fill_manual(name = "Sibling", values=c("black", "White")) +
  geom_histogram(bins=32, aes(fill=mockdf$ID), color= "black") +
  facet_wrap(~mockdf$ID) +
  scale_y_continuous("Number of reloactions", limits = c(-8,30)) +
  scale_x_continuous(limits = c(-pi,pi), breaks = c(0, pi/4, pi/2, 3*pi/4, 
                                                pi, -3*pi/4, -pi/2, -pi/4),
                 labels = c("N", "NE", "E", "SE", "S", "SW", "W", "NW")) + 
  # remove text and tick marks from axis
   theme(axis.text.y = element_blank(),
         axis.ticks.y = element_blank()) + 
  # add label manually
  geom_text(x = 4, y = 10, label = "10") 

plot example with text annotation