Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 为组合图形添加公共边框_R_Ggplot2 - Fatal编程技术网

R 为组合图形添加公共边框

R 为组合图形添加公共边框,r,ggplot2,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(

我想为组合饼图添加一个公共边框。当我使用
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" ))
df1Sanu

问题的第一个部分是,您没有看到边框,而是看到了
axis.line
axis.text
主题属性。您需要通过将元素_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" ))
因此:

要删除axis测试,我们在主题定义中添加
axis.text.x=element\u 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"))
库(ggplot2)
图书馆(cowplot)

谢谢你的回答。是否可以删除饼图周围的数字?非常感谢您的帮助!
# 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"))
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"))