为组合ggplots添加一个公共边框

时间:2016-05-01 05:21:28

标签: r ggplot2

我想为组合饼图添加一个公共边框。当我使用panel_border()时,左下线显示为比上线和右线更暗。我无法弄清楚我的错误.1)如何为组合图添加公共边框? 2)如何减少左线和底线的厚度?

df1 <- data.frame(
      variable = c("china","korea","canada","UK","USA"),
      value = c(1632,1320,4491,991,620)
    )

    df2 <- data.frame(
      variable = c("china","korea","canada","UK","USA"),
      value = c(7376,1770,5210,5005,3947)
    )
    library(ggplot2)
    p1 <-ggplot(df1,aes(x="", y = value, fill = variable))+ geom_bar(stat="identity", width=1) + ggtitle("Rainfall - 2014")+ panel_border() +coord_polar(theta = "y")+xlab("")+ylab("")+theme(legend.position="right", legend.title=element_blank(), plot.title = element_text(lineheight=3, face="bold", color="black", size=14))

    p2 <-ggplot(df2,aes(x="", y = value, fill = variable))+ geom_bar(stat="identity", width=1) + ggtitle("Rainfall - 2015")+panel_border() +coord_polar(theta = "y")+xlab("")+ylab("")+theme(legend.position="right", legend.title=element_blank(), plot.title = element_text(lineheight=3, face="bold", color="black", size=14))

    library(cowplot)
    plot_grid(p1, p2, labels=c("A", "B" ))

1 个答案:

答案 0 :(得分:1)

杉雨,

问题的第一个组成部分是您没有看到正在看到axis.lineaxis.text主题属性的边框。您需要通过将element_blank()应用于...

来从主题中删除这些
library(ggplot2)
library(cowplot)

df1 <- data.frame(
  variable = c("china","korea","canada","UK","USA"),
  value = c(1632,1320,4491,991,620)
)

df2 <- data.frame(
  variable = c("china","korea","canada","UK","USA"),
  value = c(7376,1770,5210,5005,3947)
)

p1 <-ggplot(df1, aes(x="", y = value, fill = variable))
p1 <- p1 + geom_bar(stat="identity", width=1) 
p1 <- p1 + ggtitle("Rainfall - 2014")
# p1 <- p1 + panel_border()
p1 <- p1 + coord_polar(theta = "y")
p1 <- p1 + xlab("")
p1 <- p1 + ylab("")
p1 <- p1 + theme(legend.position="right", 
  legend.title=element_blank(),
  axis.line=element_blank(),
  axis.ticks=element_blank(),  # the axis ticks
  plot.title = element_text(lineheight=3, face="bold", color="black", size=14))

p2 <-ggplot(df2,aes(x="", y = value, fill = variable))
p2 <- p2 + geom_bar(stat="identity", width=1)
p2 <- p2 + ggtitle("Rainfall - 2015")
# p2 <- p2 + panel_border()
p2 <- p2 + coord_polar(theta = "y")
p2 <- p2 + xlab("")
p2 <- p2 + ylab("")
p2 <- p2 + theme( legend.position="right", 
  legend.title = element_blank(),
  axis.line=element_blank(),
  axis.ticks=element_blank(),  # the axis ticks
  plot.title = element_text(lineheight=3, face="bold", color="black", size=14))
plot_grid(p1, p2, labels=c("A", "B" ))

结果:

enter image description here

更好的解决方案可能是如下清理情节:

library(gridExtra)
get_legend<-function(myggplot){
  tmp <- ggplot_gtable(ggplot_build(myggplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)
}

legend <- get_legend(p1)
p1 <- p1 + theme(legend.position="none")
p2 <- p2 + theme(legend.position="none")
# Arrange ggplot2 graphs with a specific width
grid.arrange(p1, p2, legend, ncol=3, widths=c(2.3, 2.3, 0.8))

结果:

enter image description here

现在我们通过添加...添加边框

# next line adds border
grid.rect(.5,.5,width=unit(.99,"npc"), height=unit(0.99,"npc"), 
  gp=gpar(lwd=3, fill=NA, col="black"))

因此:

enter image description here

删除轴测试我们将axis.text.x=element_blank()添加到主题定义中......因此:

library(ggplot2)
library(cowplot)

df1 <- data.frame(
  variable = c("china","korea","canada","UK","USA"),
  value = c(1632,1320,4491,991,620)
)

df2 <- data.frame(
  variable = c("china","korea","canada","UK","USA"),
  value = c(7376,1770,5210,5005,3947)
)

p1 <-ggplot(df1, aes(x="", y = value, fill = variable))
p1 <- p1 + geom_bar(stat="identity", width=1) 
p1 <- p1 + ggtitle("Rainfall - 2014")
# p1 <- p1 + panel_border()
p1 <- p1 + coord_polar(theta = "y")
p1 <- p1 + xlab("")
p1 <- p1 + ylab("")
p1 <- p1 + theme(legend.position="right", 
  legend.title=element_blank(),
  axis.line=element_blank(),
  axis.ticks=element_blank(),  # the axis ticks
  axis.text.x=element_blank(),
  plot.title = element_text(lineheight=3, face="bold", color="black", size=14))
p1

p2 <-ggplot(df2,aes(x="", y = value, fill = variable))
p2 <- p2 + geom_bar(stat="identity", width=1)
p2 <- p2 + ggtitle("Rainfall - 2015")
# p2 <- p2 + panel_border()
p2 <- p2 + coord_polar(theta = "y")
p2 <- p2 + xlab("")
p2 <- p2 + ylab("")
p2 <- p2 + theme( legend.position="right", 
  legend.title = element_blank(),
  axis.line=element_blank(),
  axis.ticks=element_blank(),  # the axis ticks
  axis.text.x=element_blank(),
  plot.title = element_text(lineheight=3, face="bold", color="black", size=14))
plot_grid(p1, p2, labels=c("A", "B" ))

library(gridExtra)
get_legend<-function(myggplot){
  tmp <- ggplot_gtable(ggplot_build(myggplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)
}

legend <- get_legend(p1)
p1 <- p1 + theme(legend.position="none")
p2 <- p2 + theme(legend.position="none")
# 4. Arrange ggplot2 graphs with a specific width
grid.arrange(p1, p2, legend, ncol=3, widths=c(2.3, 2.3, 0.8))
# next line adds border
grid.rect(.5,.5,width=unit(.99,"npc"), height=unit(0.99,"npc"), 
  gp=gpar(lwd=3, fill=NA, col="black"))

结果:

enter image description here