我想通过使用ggplot2重现下面的情节.我可以靠近,但不能删除顶部和右边界.下面我介绍几次使用ggplot2的尝试,包括在Stackoverflow上或通过Stackoverflow找到的一些建议.不幸的是,我无法将这些建议付诸实践.
我希望有人能够纠正下面的一个或多个代码片段.
谢谢你的任何建议.
# desired plot
a <- seq(1,20)
b <- a^0.25
plot(a,b, bty = "l")
library(ggplot2)
df <- as.data.frame(cbind(a,b))
# 1. ggplot2 default
ggplot(df, aes(x = a, y = b)) + geom_point()
# 2. removes background color
ggplot(df, aes(x = a, y = b)) + geom_point() + opts(panel.background = theme_rect(fill='white', colour='black'))
# 3. also removes gridlines
none <- theme_blank()
ggplot(df, aes(x = a, y = b)) + geom_point() + opts(panel.background = theme_rect(fill='white', colour='black')) + opts(panel.grid.major = none, panel.grid.minor = none)
# 4. does not remove top and right border
ggplot(df, aes(x = a, y = b)) + geom_point() + opts(panel.background = theme_rect(fill='white', colour='black')) + opts(panel.grid.major = none, panel.grid.minor = none) + opts(panel.border = none)
# 5. does not remove top and right border
ggplot(df, aes(x = a, y = b)) + geom_point() + opts(panel.background = theme_rect(fill='white', colour='black')) + opts(panel.grid.major = none, panel.grid.minor = none) + opts(axis.line = theme_segment())
# 6. removes x and y axis in addition to top and right border
# http://stackoverflow.com/questions/5458409/remove-top-and-right-border-from-ggplot2
ggplot(df, aes(x = a, y = b)) + geom_point() + opts(panel.background = theme_rect(fill='white', colour='black')) + opts(panel.grid.major = none, panel.grid.minor = none) + opts(panel.background=theme_rect(colour=NA))
# 7. returns error when attempting to remove top and right border
# https://groups.google.com/group/ggplot2/browse_thread/thread/f998d113638bf251
#
# Error in el(...) : could not find function "polylineGrob"
#
theme_L_border <- function(colour = "black", size = 1, linetype = 1) {
structure(
function(x = 0, y = 0, width = 1, height = 1, ...) {
polylineGrob(
x=c(x+width, x, x), y=c(y,y,y+height), ..., default.units = "npc",
gp=gpar(lwd=size, col=colour, lty=linetype),
)
},
class = "theme",
type = "box",
call = match.call()
)
}
ggplot(df, aes(x = a, y = b)) + geom_point() + opts(panel.background = theme_rect(fill='white', colour='black')) + opts(panel.grid.major = none, panel.grid.minor = none) + opts( panel.border = theme_L_border())
Run Code Online (Sandbox Code Playgroud)
San*_*att 118
编辑 忽略这个答案.现在有更好的答案.查看评论.使用+ theme_classic()
编辑
这是一个更好的版本.原始帖子中提到的错误仍然存在(我认为).但轴线在面板下绘制.因此,同时删除panel.border和panel.background查看轴线.
library(ggplot2)
a <- seq(1,20)
b <- a^0.25
df <- as.data.frame(cbind(a,b))
ggplot(df, aes(x = a, y = b)) + geom_point() +
theme_bw() +
theme(axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank())
Run Code Online (Sandbox Code Playgroud)

原帖
这个很贴心.有一个错误axis.line没有在y轴上工作(见这里),似乎还没有修复.因此,在移除面板边框后,必须使用单独绘制y轴geom_vline.
library(ggplot2)
library(grid)
a <- seq(1,20)
b <- a^0.25
df <- as.data.frame(cbind(a,b))
p = ggplot(df, aes(x = a, y = b)) + geom_point() +
scale_y_continuous(expand = c(0,0)) +
scale_x_continuous(expand = c(0,0)) +
theme_bw() +
opts(axis.line = theme_segment(colour = "black"),
panel.grid.major = theme_blank(),
panel.grid.minor = theme_blank(),
panel.border = theme_blank()) +
geom_vline(xintercept = 0)
p
Run Code Online (Sandbox Code Playgroud)
极端点被剪切,但是可以使用baptiste的代码撤消剪辑.
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name=="panel"] <- "off"
grid.draw(gt)
Run Code Online (Sandbox Code Playgroud)

或者用于limits移动面板的边界.
ggplot(df, aes(x = a, y = b)) + geom_point() +
xlim(0,22) + ylim(.95, 2.1) +
scale_x_continuous(expand = c(0,0), limits = c(0,22)) +
scale_y_continuous(expand = c(0,0), limits = c(.95, 2.2)) +
theme_bw() +
opts(axis.line = theme_segment(colour = "black"),
panel.grid.major = theme_blank(),
panel.grid.minor = theme_blank(),
panel.border = theme_blank()) +
geom_vline(xintercept = 0)
Run Code Online (Sandbox Code Playgroud)
And*_*rew 74
最近对ggplot(0.9.2+)的更新已经彻底改变了主题的语法.最值得注意的是,opts()现已被弃用,已被替换为theme(). 桑迪的回答仍将(截至12月1日)产生一个图表,但导致R抛出一堆警告.
这是反映当前ggplot语法的更新代码:
library(ggplot2)
a <- seq(1,20)
b <- a^0.25
df <- as.data.frame(cbind(a,b))
#base ggplot object
p <- ggplot(df, aes(x = a, y = b))
p +
#plots the points
geom_point() +
#theme with white background
theme_bw() +
#eliminates background, gridlines, and chart border
theme(
plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank()
) +
#draws x and y axis line
theme(axis.line = element_line(color = 'black'))
Run Code Online (Sandbox Code Playgroud)
产生:

Cla*_*lke 21
另一种选择theme_classic()是cowplot包附带的主题theme_cowplot()(自动加载包).它看起来很像theme_classic(),有一些细微的差别.最重要的是,默认标签尺寸较大,因此生成的图形可以在出版物中使用而无需进一步修改(特别是如果您使用save_plot()而不是保存它们ggsave()).此外,背景是透明的,而不是白色,如果您想在插图画家中编辑图形,这可能很有用.最后,在我看来,刻面图看起来更好.
例:
library(cowplot)
a <- seq(1,20)
b <- a^0.25
df <- as.data.frame(cbind(a,b))
p <- ggplot(df, aes(x = a, y = b)) + geom_point()
save_plot('plot.png', p) # alternative to ggsave, with default settings that work well with the theme
Run Code Online (Sandbox Code Playgroud)
这是plot.png此代码生成的文件如下所示:

免责声明:我是包裹作者.
我按照安德鲁的回答,但我也必须按照/sf/answers/2508348391/分别设置x和y轴,因为我的ggplot版本(v2.1.0)中存在错误.
代替
theme(axis.line = element_line(color = 'black'))
Run Code Online (Sandbox Code Playgroud)
我用了
theme(axis.line.x = element_line(color="black", size = 2),
axis.line.y = element_line(color="black", size = 2))
Run Code Online (Sandbox Code Playgroud)
这是一个非常简单的答案
yourPlot +
theme(
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black")
)
Run Code Online (Sandbox Code Playgroud)
就是这么简单。来源:本文末尾