如何将geom_spoke围绕其来源居中

时间:2019-04-02 11:52:56

标签: r ggplot2

示例清楚地表明,geom_spoke绘制的线条源自( x y ),其长度为 { {1}} 指向 radius 指定的方向:

angle

使辐条居中( ggplot(df, aes(x, y)) + geom_point() + geom_spoke(aes(angle = angle), radius = 0.5) x )最简单的可重用方法是什么?

我不希望为此修改数据(不那么容易重用)或进行内联三角法(也不太可重用)。我不介意解决方案中的“半径”是否变为“直径”。

1 个答案:

答案 0 :(得分:2)

我认为最简单的方法是完成创建Position子类的整个过程,例如在position_nudge

y

现在您可以轻松使用它:

position_center_spoke <- function() PositionCenterSpoke

PositionCenterSpoke <- ggplot2::ggproto('PositionCenterSpoke', ggplot2::Position,
  compute_panel = function(self, data, params, scales) {
    # xend/yend is computed by this point, so shortcut!
    data$x <- 2*data$x - data$xend
    data$y <- 2*data$y - data$yend
    #data$x <- data$x - data$radius*cos(data$angle)
    #data$y <- data$y - data$radius*sin(data$angle)

    # After shifting, the spoke needs to have diameter length,
    # but I’m not sure if the radius is still used anywhere.
    data$radius <- 2*data$radius

    # Now the scales would need to be retrained,
    # But compute_panel doesn’t allow that and
    # compute_layer “should not be overridden”
    data
  }
)

position = 'center_spoke'