【问题标题】:R ggplot: Move y axis to grid lines on polar plot (Polar_Coord)R ggplot:将y轴移动到极坐标图上的网格线(Polar_Coord)
【发布时间】:2018-09-23 01:41:49
【问题描述】:

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

这是一个模拟:

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

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

【问题讨论】:

  • 使用 annotate 代替内置的轴标签器,并将它们放在任何你想要的地方。

标签: r ggplot2 axis-labels polar-coordinates


【解决方案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") 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-27
    • 1970-01-01
    • 2022-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多