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:
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:
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:
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:
Vielen Dank für Ihre großartige Antwort. Ist es möglich, die Zahlen im Kreisdiagramm zu entfernen? – sanu
Ja, fügen Sie 'axis.text.x = element_blank()', zur Theme-Definition hinzu ... – Technophobe01
Vielen Dank für Ihre Hilfe! – sanu