试试这个,重新定义 guide_grid。
此解决方案来自Cookbook for R
# Save the original definition of the guide_grid
guide_grid_orig <- ggplot2:::guide_grid
# Create the replacement function
guide_grid_no_vline <- function(theme, x.minor, x.major, y.minor, y.major) {
x.minor <- setdiff(x.minor, x.major)
y.minor <- setdiff(y.minor, y.major)
ggname("grill", grobTree(
theme_render(theme, "panel.background"),
if(length(y.minor) > 0) theme_render(
theme, "panel.grid.minor", name = "y",
x = rep(0:1, length(y.minor)), y = rep(y.minor, each=2),
id.lengths = rep(2, length(y.minor))
),
if(length(y.major) > 0) theme_render(
theme, "panel.grid.major", name = "y",
x = rep(0:1, length(y.major)), y = rep(y.major, each=2),
id.lengths = rep(2, length(y.major))
)
))
}
# Set the environment to be the same as original
environment(guide_grid_no_vline) <- environment(ggplot2:::guide_grid)
# Assign the function inside ggplot2
assignInNamespace("guide_grid", guide_grid_no_vline, ns="ggplot2")
# Draw the plot with the redefined guide_grid
ggplot(CO3, aes(x=outcome)) +
geom_bar(aes(x=outcome))+
facet_grid(Treatment~Type, margins='Treatment', scales='free') +
theme_bw() +
opts(axis.text.x=theme_text(angle= 45, vjust=1, hjust= 1))
# Restore the original guide_grid function so that it will draw all gridlines again
assignInNamespace("guide_grid", guide_grid_orig, ns="ggplot2")