R 从ggplot2图形中删除右边框
使用以下代码,我可以删除顶部和右侧边框以及其他内容。我想知道如何仅删除R 从ggplot2图形中删除右边框,r,plot,ggplot2,r-grid,R,Plot,Ggplot2,R Grid,使用以下代码,我可以删除顶部和右侧边框以及其他内容。我想知道如何仅删除ggplot2图形的右边框 p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() p + theme_classic() p主题系统挡住了去路,但只要稍加调整,你就可以破解主题元素 library(ggplot2) library(grid) element_grob.element_custom <- function(element, ...)
ggplot2
图形的右边框
p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
p + theme_classic()
p主题系统挡住了去路,但只要稍加调整,你就可以破解主题元素
library(ggplot2)
library(grid)
element_grob.element_custom <- function(element, ...) {
segmentsGrob(c(1,0,0),
c(0,0,1),
c(0,0,1),
c(0,1,1), gp=gpar(lwd=2))
}
## silly wrapper to fool ggplot2
border_custom <- function(...){
structure(
list(...), # this ... information is not used, btw
class = c("element_custom","element_blank", "element") # inheritance test workaround
)
}
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() +
theme_classic() +
theme(panel.border=border_custom())
库(ggplot2)
图书馆(网格)
element\u grob.element\u custom您只需删除两个边框(因为它首先使用theme\u classic()
),然后使用annotate()添加一个边框即可:
这不是那么容易,因为面板。边框作为元素_rect
对象进行管理,因此它已经是一个矩形。可能的重复:我同意Davide的观点:这可能不容易。我假设theme_classic实际上完全移除了长方体,而是绘制了轴线。为了说明这一点,请尝试p+主题(axis.line=element\u line())
。它将以标准主题绘制p
,但添加了轴线。@律师:不,这不是重复。谢谢你的回答。如果您不介意,请查看我的编辑并提出任何解决方案。再次感谢您的帮助。假设您在将绘图转换为GTTable之前将此主题应用于绘图,则应传递此元素
p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
p + theme_classic() + annotate(
geom = 'segment',
y = Inf,
yend = Inf,
x = -Inf,
xend = Inf
)
p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
p + annotate(
geom = 'segment',
y = Inf,
yend = Inf,
x = -Inf,
xend = Inf
) + theme(panel.border = element_blank(), axis.line = element_line())