2016-05-01 70 views
0

我想爲組合餅圖添加共同邊框。當我使用panel_border()時,左側和底部線條顯得比上部和右側線條更暗。我無法弄清楚自己的錯誤。1)如何爲組合地塊添加共同邊框? 2)如何減少左右底線的厚度?爲組合ggplots添加共同邊框

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

杉雨,

問題的第一部分是,你沒有看到你所看到的axis.line邊框和axis.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

+0

謝謝你的回答。是否有可能刪除餅圖周圍的數字? – sanu

+0

是的,將'axis.text.x = element_blank()'添加到主題定義... – Technophobe01

+0

非常感謝您的幫助! – sanu