杉雨,
問題的第一部分是,你沒有看到你所看到的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](https://i.stack.imgur.com/3ybhv.jpg)
一個更好的解決方案可能是清理情節如下:
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](https://i.stack.imgur.com/HQIMJ.jpg)
,現在我們只需添加邊框...
# 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](https://i.stack.imgur.com/8jLyK.jpg)
去除軸測試我們添加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](https://i.stack.imgur.com/h0ndh.jpg)
謝謝你的回答。是否有可能刪除餅圖周圍的數字? – sanu
是的,將'axis.text.x = element_blank()'添加到主題定義... – Technophobe01
非常感謝您的幫助! – sanu