极坐标图(ggplot2),去掉了外环,顶部网格

时间:2018-09-19 10:39:33

标签: ggplot2 polar-coordinates geom-hline

我已经搜索并重新搜索了不同的问题,尽管我看到了类似的问题(确实有些非常有用),但我似乎找不到答案。

我正在使用极坐标图,默认情况下,这些图会添加一个我不想要的额外外圈。通过删除[panel.grid]并改用[geom.hline],我设法摆脱了它。

这是我的问题:

  • 如果我使用panel.ontop = FALSE,则网格线显示在绘图下方,并且我松散了单位参考(我需要)(例如,无法看到与1对应的分度在哪里)

  • 如果我使用panel.ontop = TRUE,我会松开网格线

我想要的是使图的顶部带有网格线(并且没有最外面的环)。在这里,您可以看到我的代码示例:

##Data
Main.Actions <- c("Financing","Anchoring regulations", "Awareness", "Sewage", "Enforcement", "Coordination")
count<-c(2, 2,4,2,3,3)
improve<-data.frame(Main.Actions, count, stringsAsFactors = FALSE)

## Plot

MA4<- ggplot(improve, aes(x = Main.Actions, y=count, fill=Main.Actions, width=1)) +
  geom_hline(yintercept = seq(0, 4, by = 1), colour = "grey", size = 0.35) +
  theme(
    panel.grid.major.x=element_blank(),
    panel.background = element_blank(),
    panel.border = element_blank(),
    panel.ontop = FALSE,
    legend.position = "none",
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.text.y=element_blank(),
    axis.text.x=element_blank(),
    axis.ticks.y=element_blank())+

  theme(plot.margin=unit(c(0,0,0,0),"cm"))+

  geom_bar(stat="identity")+scale_fill_brewer(palette = "Blues") + 
  coord_polar()

MA4

enter image description here

我敢肯定这很简单,但是我一生中已经花了3天的时间才能解决这个问题。

我们将不胜感激

安娜

1 个答案:

答案 0 :(得分:1)

解决了!!

我将[geom_hline]放在了[geom_bar]之后,现在它绘制出了我需要的东西。我希望这对其他用户有帮助。在下面找到代码:

MA4<- ggplot(improve, aes(x = Main.Actions, y=count, fill=Main.Actions, width=1)) +

  theme(
    panel.grid.major.x=element_blank(),
    panel.background = element_blank(),
    panel.border = element_blank(),
    panel.ontop =FALSE,
    legend.position = "none",
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.text.y=element_blank(),
    axis.text.x=element_blank(),
    axis.ticks.y=element_blank())+

  theme(plot.margin=unit(c(0,0,0,0),"cm"))+

  geom_bar(stat="identity")+scale_fill_brewer(palette = "Blues") + 
  geom_hline(yintercept = seq(0, 4, by = 1), colour = "darkgrey", size = 0.35) +
  coord_polar()