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

Bre*_*yer 5 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"))
Run Code Online (Sandbox Code Playgroud)

极坐标图示例

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

小智 0

您可以使用其中的选项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") 
Run Code Online (Sandbox Code Playgroud)

带文本注释的绘图示例