R ggplot : Move y axis to grid lines on polar plot (Polar_Coord)

标签 r ggplot2 axis-labels polar-coordinates

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

这是一个模拟:

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 方向),但这样做有困难。有谁知道我该怎么做?

最佳答案

您可以使用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

关于R ggplot : Move y axis to grid lines on polar plot (Polar_Coord),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49804412/

相关文章:

r - 关于R水管工的基本问题

r - 为 ggpaired 添加抖动

R:当数字很大时,在 x 轴上以指定的间隔做刻度线

r - 在ggplot条形图中显示离散变量的所有x轴标签

python - 在轴上显示小数位和科学计数法

javascript - Plotly-在 R 中生成 JSON,在 html/javascript 中保存和重用

r - 使用r将多个csv文件导入postgresql数据库(内存错误)

r - 根据 Shiny R 中的单选按钮显示 slider 输入

使用 ggcorrplot2 : "x-axis" labels get cropped 的 R 相关图

r - 在 R ggplot2 中制作圆环