从ggplot2图中删除右边框 [英] Removing Right Border from ggplot2 graph

查看:2943
本文介绍了从ggplot2图中删除右边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面的代码,我可以删除顶部和右边框以及其他东西。我想知道如何只删除 ggplot2 图的右边框。任何在这方面的帮助将不胜感激。感谢您的帮助。

  p < -  ggplot(mtcars,aes(x = wt,y = mpg)) + geom_point()

p + theme_classic()


解决方案 div>

主题系统阻碍了你的发展,但是有一点小小的变化,你可以破解主题元素,

  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))
}
##愚蠢的包装来欺骗ggplot2
border_custom< - function(...){
structure(
list(...),#this ...信息不是使用,btw
class = c(element_custom,element_blank,element)#继承测试解决方法



ggplot(mtcars,aes ()())+ geom_point()+
theme_classic()+
theme(panel.border = border_custom())


With the following code I can remove top and right borders along with other things. I wonder how to remove the right border of the ggplot2 graph only. Any help in this regard will be highly appreciated. Thanks in advance for your help.

p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() 

p + theme_classic()

解决方案

the theme system gets in the way, but with a little twist you can hack the theme elements,

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图中删除右边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆