2016-05-01 18 views
0

Ich möchte einen gemeinsamen Rahmen für die kombinierten Kreisdiagramme hinzufügen. Wenn ich panel_border() verwende, erscheinen die linke und die untere Zeile dunkler als die obere und die rechte Zeile. Ich konnte meinen Fehler nicht verstehen.1) Wie kann ich eine gemeinsame Grenze für kombinierte Plots hinzufügen? 2) Wie kann ich die Dicke der linken und unteren Linien reduzieren?fügen Sie einen gemeinsamen Rahmen für kombinierte ggplots hinzu

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")) 

Antwort

1

Sanu,

Die erste Komponente des Problems ist, dass Sie nicht eine Grenze sehen, dass Ihre die axis.line sehen, und axis.text Themenattribute. Sie müssen diese aus dem Thema zu entfernen, indem element_blank(), um sowohl die Anwendung ...

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")) 

Ergebnis:

enter image description here

eine bessere Lösung könnte sein, die Handlung zu bereinigen, wie folgt:

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)) 

Ergebnis:

enter image description here

und jetzt fügen wir die Grenze durch Zugabe ...

# 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")) 

so:

enter image description here

die Achse Test entfernen wir axis.text.x=element_blank() zum Thema Definition hinzufügen ... So :

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")) 

Ergebnis:

enter image description here

+0

Vielen Dank für Ihre großartige Antwort. Ist es möglich, die Zahlen im Kreisdiagramm zu entfernen? – sanu

+0

Ja, fügen Sie 'axis.text.x = element_blank()', zur Theme-Definition hinzu ... – Technophobe01

+0

Vielen Dank für Ihre Hilfe! – sanu

Verwandte Themen