从ggplot2中删除网格,背景颜色以及顶部和右侧边框
我想通过使用ggplot2重现下面的图形。我可以靠近,但不能删除顶部和右边界。下面我使用ggplot2进行了几次尝试,包括在Stackoverflow上或通过Stackoverflow发现的一些建议。不幸的是,我一直无法得到这些建议的工作。从ggplot2中删除网格,背景颜色以及顶部和右侧边框
我希望有人能够纠正下面的一个或多个代码片段。
谢谢你的任何建议。
# 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())
编辑忽略此答案。现在有更好的答案。查看评论。使用+ 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())
原帖 此靠拢。 axis.line
在y轴上没有工作(see here),但似乎尚未修复。因此,在移除面板边框后,必须使用geom_vline
分别绘制y轴。
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
极端点被限幅,但限幅可以通过baptiste使用代码复原。
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name=="panel"] <- "off"
grid.draw(gt)
或者使用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)
我想我也许应该给对勾现在的其他答案。你的回答是绝对值得的2012年的复选标记,是非常有益的。但是,现在另一个答案似乎更好。如果仅出于历史原因,我讨厌切换复选标记。 –
如果是我,我会在12个月前完成它。 –
对ggplot(0.9.2+)的最近更新已经彻底改变了主题的语法。最值得注意的是,opts()
现已弃用,已被theme()
取代。 Sandy's答案仍然会(截至2012年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'))
生成:
到theme_classic()
一个替代方案是随附cowplot包主题,theme_cowplot()
(与自动加载包)。它看起来类似于theme_classic()
,有一些细微的差异。最重要的是,默认标签尺寸较大,因此可以在出版物中使用所得数字,而无需进一步修改(特别是如果您使用save_plot()
而不是ggsave()
保存它们)。此外,背景是透明的,而不是白色,如果您想在Illustrator中编辑图形,这可能很有用。最后,在我看来,多面情节看起来更好。
例子:
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
这是由这个代码生成的文件plot.png
样子:
免责声明:我包的作者。
我跟着Andrew's answer,但我也不得不按照https://stackoverflow.com/a/35833548,并分别设置x和y轴,因为我的版本的ggplot(v2.1.0)中的一个错误。
而不是
theme(axis.line = element_line(color = 'black'))
我用
theme(axis.line.x = element_line(color="black", size = 2),
axis.line.y = element_line(color="black", size = 2))
非常酷的答案。没有其他工作为我 - 我想这个错误应该更好地记录。 – thiagoveloso
,张贴在下面留言,但是现在可以用做+ theme_classic() – nsheff