在這里,我將 mtcars 用于這兩個圖只是為了重現一個示例。但實際上我有 df1 生產 p1 和 df2 生產 p2。p1 和 p2 都具有相同的 x 軸值。
# from : http://homepages.gac.edu/~anienow2/MCS_142/R/R-barchart2-1.html
library(ggplot2) #load ggplot2 library
mtcars$gear <- factor(mtcars$gear) # Create a categorical variable
mtcars$cyl <- factor(mtcars$cyl) # Create categorical variable
p1 <- ggplot(data = mtcars, aes(x=gear, fill=cyl) ) geom_bar() # Creates stacked bar chart
p1 <- p1 xlab("Gears") ggtitle("Cylinders by Gears") # Adds title and labels
p1
mtcars$carb <- factor(mtcars$carb)
p2 <- ggplot(data = mtcars, aes(x=gear, fill=carb) ) geom_bar()
p2 <- p2 xlab("Gears") ggtitle("carb") # Adds title and labels
p2


我不想重復 x 軸。考慮到每個圖都來自不同的資料框,我無法弄清楚如何在這里應用 facet_wrap 或 facet_grip 方法。有任何想法嗎?
uj5u.com熱心網友回復:
一種方法可能是使用:
library(grid)
grid.newpage()
grid.draw(rbind(ggplotGrob(p1 theme_minimal()
theme(axis.title.x = element_blank(), axis.text.x = element_blank())),
ggplotGrob(p2 theme_minimal()), size = "last"))

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/483775.html
