去掉极坐标图的最外环(ggplot2)

时间:2017-09-29 23:09:31

标签: r ggplot2 polar-coordinates

我正在尝试建立一个极地情节,以显示某些方向的百分位数,但在删除极地情节的最外环时遇到问题,即使它已作为有助于构建极坐标图的条形图的一部分被删除。

以下是可重现的代码:

library(ggplot2)

perc_df=data.frame(id=c(125,126,127,128,129,130),percentile=c(.50,.75,.99,.27,.12,.66))

cxc <- ggplot(perc_df, aes(x = id)) +
  geom_bar(width = 1, aes(weight = percentile, fill = ..count..)) +
  scale_y_continuous(limits = c(0,1),breaks=seq(0, 1, .25),expand=c(0,0)) +
  scale_fill_gradient2(low = 'red', high = 'green',mid='yellow', limits=c(0,1),midpoint=.5,guide = "colourbar")

cxc + coord_polar(theta = 'x',start = 2.6)+ guides(fill=FALSE) +
  theme(axis.title.x=element_blank(),axis.text.x=element_blank(),axis.ticks.x=element_blank(),axis.title.y=element_blank(),
        axis.text.y=element_blank(),axis.ticks.y=element_blank(),plot.title = element_text(lineheight=.8, face="bold",hjust = 0.5),
        panel.border = element_blank())

这是产生的:

radar

这非常接近,但我们希望百分位数为1位于情节的最后一圈而不是该情节的外环。

编辑:我已经尝试将答案纳入此处,但没有任何运气。 Remove extra space and ring at the edge of a polar plot

1 个答案:

答案 0 :(得分:2)

该问题的回答非常简单。您必须添加geom_hline(在添加geom_bar之前可能需要它们)。我不认为geom_vline在你的情况下是有意义的,因为x轴上的变量不是数值。

我添加了一行:

geom_hline(yintercept = seq(0, 1, by = 0.25), colour = "grey90", size = 0.2)

整个代码如下所示:

perc_df <- data.frame(id = c(125, 126, 127, 128, 129, 130), 
                      percentile = c(0.50, 0.75, 0.99, 0.27, 0.12, 0.66))

library(ggplot2)
ggplot(perc_df, aes(id)) +
    geom_hline(yintercept = seq(0, 1, by = 0.25), colour = "grey90", size = 0.2) +
    geom_bar(aes(weight = percentile, fill = ..count..), 
             width = 1, ) +
    scale_y_continuous(limits = c(0, 1), 
                       breaks = seq(0, 1, 0.25),
                       expand = c(0, 0)) +
    scale_fill_gradient2(low = "red", mid="yellow", high = "green", 
                         limits = c(0, 1), midpoint = 0.5, 
                         guide = "colourbar") +
    coord_polar(theta = "x", start = 2.6) + 
    guides(fill = FALSE) +
    theme_bw() +
    theme(axis.title = element_blank(),
          panel.border = element_blank(),
          legend.key = element_blank(),
          axis.ticks = element_blank(),
          axis.text = element_blank(),
          panel.grid  = element_blank(),
          plot.title = element_text(lineheight = 0.8, face = "bold", 
                                    hjust = 0.5))

它产生这样的情节:

enter image description here