从ggplot2删除网格,背景颜色以及顶部和右侧边框 [英] Remove grid, background color, and top and right borders from ggplot2
问题描述
我想通过使用ggplot2重现下面的情节。我可以靠近,但不能删除顶部和右边界。下面我使用ggplot2进行了几次尝试,包括在Stackoverflow上或通过Stackoverflow发现的一些建议。不幸的是,我一直无法得到这些建议的工作。
我希望有人可以纠正下面的一个或多个代码片段。
谢谢对于任何建议。
#期望的情节
a < - seq(1,20)
b< - a ^ 0.25
图(a,b,bty =l)
图书馆(ggplot2)
df < - as。 data.frame(cbind(a,b))
#1. ggplot2 default
ggplot(df,aes(x = a,y = b))+ geom_point()
#2.移除背景颜色
ggplot(df,aes(x = a,y = b))+ geom_point()+ opts(panel.background = theme_rect(fill ='white',color ='black'))
#3.也删除网格线
none< - theme_blank()
ggplot(df,aes(x = a,y = b)) + geom_point()+ opts(panel.background = theme_rect(fill ='white',color ='black'))+ opts(panel.grid.major = none,panel.grid.minor = none)
#4.不删除顶部和右边界
ggplot(df,aes(x = a,y = b))+ geom_point()+ opts(panel.background = theme_rect(fill ='white' ,颜色='黑色'))+ opts(panel.grid.major = none,panel.grid.minor = none)+ opts(panel.border =无)
#5.不删除顶部和右边界
ggplot(df,aes(x = a,y = b))+ geom_point()+ opts(panel.background = theme_rect(fill ='white',color ='black'))+ opts (panel.grid.major = none,panel.grid.minor = none)+ opts(axis.line = theme_segment())
#6.除了顶部和右侧之外,还移除x和y轴边界
#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',color ='black'))+ opts(panel.grid.major = none,panel.grid.minor = none)+ opts(panel。 background = theme_rect(color = NA))
#7.尝试删除顶部和右侧边框时返回错误
#https://groups.google.com/group/ggplot2/browse_thread / thread / f998d113638bf251
#
#el中的错误(...):找不到函数polylin eGrob
#
theme_L_border< - function(color =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 = color,lty = linetype),
)
},
class = 主题,
type =box,
call = match.call()
)
}
ggplot(df,aes(x = a,y = b))+ geom_point()+ opts(panel.background = theme_rect(fill ='white',color ='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(color =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轴。
<$ p $ (ggplot2)
库(网格)
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(color =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) c(0,22))+
scale_y_continuous(expand = c(0,0),limits = c(.95,2.2))+
theme_bw()+
opts(axis。 line = theme_segment(color =black),
panel.grid.major = theme_blank(),
panel.grid.minor = theme_blank(),
panel.border = theme_blank() )+
geom_vline(xintercept = 0)
I would like to reproduce the plot immediately below by using ggplot2. I can come close, but cannot remove the top and right borders. Below I present several attempts using ggplot2, including several suggestions found on or via Stackoverflow. Unfortunately I have not been able to get those suggestions to work.
I am hoping someone may be able to correct one or more of the code snippets below.
Thank you for any suggestions.
# 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())
EDIT Ignore this answer. There are now better answers. See the comments. Use + theme_classic()
EDIT
This is a better version. The bug mentioned below in the original post remains (I think). But the axis line is drawn under the panel. Therefore, remove both the panel.border
and panel.background
to see the axis lines.
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())
Original post
This gets close. There was a bug with axis.line
not working on the y-axis (see here), that appears not to be fixed yet. Therefore, after removing the panel border, the y-axis has to be drawn in separately using 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
The extreme points are clipped, but the clipping can be undone using code by baptiste.
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name=="panel"] <- "off"
grid.draw(gt)
Or use limits
to move the boundaries of the panel.
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)
这篇关于从ggplot2删除网格,背景颜色以及顶部和右侧边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!