Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 从ggplot2图形中删除右边框_R_Plot_Ggplot2_R Grid - Fatal编程技术网

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())