Discussion:
Half borders (left/bottom axes only) in ggplot2
陈文峰
2013-02-18 12:58:29 UTC
Permalink
*There is a much better method to remove the top and right borders of
ggplot2.*
Ref:
http://stackoverflow.com/questions/10861773/remove-grid-background-color-and-top-and-right-borders-from-ggplot2
I paste the script here. Everyone can try it. I like it very much!
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 baground, gridlines, and chart border
theme( plot.background = element_blank() ,
panel.grid.major = element_blank() ,
panel.grid.minor = element_blank() ,
panel.border = element_blank() ,
panel.background = element_blank() ) +
#draws x and y axis line
theme(axis.line = element_line(color = 'black'))

#generates:

[image: plot output]
Dear all,
A quick and simple technique post, and apologies if this is
obvious/already implemented somewhere else.
I was trying to have ggplot2 display only left and bottom ("L"-shaped)
borders, rather than a rectangle or no border, and saw on the web that this
has been a problem for others. My solution (based on the ggplot2 source
code in theme-elements.r) is pasted below, and is accessible directly from
source(
"http://egret.psychol.cam.ac.uk/statistics/R/extensions/rnc_ggplot2_border_themes.r"<http://egret.psychol.cam.ac.uk/statistics/R/extensions/rnc_ggplot2_border_themes.r>
)
opts( panel.border = theme_L_border() )
opts( panel.border = theme_left_border() )
opts( panel.border = theme_bottom_border() )
library(ggplot2)
source(
"http://egret.psychol.cam.ac.uk/statistics/R/extensions/rnc_ggplot2_border_themes.r"<http://egret.psychol.cam.ac.uk/statistics/R/extensions/rnc_ggplot2_border_themes.r>
)
df = data.frame(x=c(1,2,3),y=c(4,5,6))
ggplot(data=df, aes(x=x, y=y)) + geom_point() + theme_bw() + opts(
panel.border = theme_L_border() )
Note that the order can be significant; for example, if you specify the L
border option and then a theme, the theme settings will override the border
option, so you need to specify the theme (if any) before the border option,
as above.
The code is shown below.
all the best,
Rudolf.
# Rudolf Cardinal, March 2011
# Simple extensions to ggplot2 (v0.8.7); see
http://www.psychol.cam.ac.uk/statistics/R/
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()
)
}
theme_bottom_border <- function(colour = "black", size = 1, linetype = 1) {
structure(
function(x = 0, y = 0, width = 1, height = 1, ...) {
polylineGrob(
x=c(x, x+width), y=c(y,y), ..., default.units = "npc",
gp=gpar(lwd=size, col=colour, lty=linetype),
)
},
class = "theme",
type = "box",
call = match.call()
)
}
theme_left_border <- function(colour = "black", size = 1, linetype = 1) {
structure(
function(x = 0, y = 0, width = 1, height = 1, ...) {
polylineGrob(
x=c(x, x), y=c(y,y+height), ..., default.units = "npc",
gp=gpar(lwd=size, col=colour, lty=linetype),
)
},
class = "theme",
type = "box",
call = match.call()
)
}
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ggplot2-/***@public.gmane.org
To unsubscribe: email ggplot2+unsubscribe-/***@public.gmane.org
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Paul
2013-02-18 15:27:41 UTC
Permalink
ggplot2 0.9.3 introduced theme_classic() which does what you want.
Post by 陈文峰
*There is a much better method to remove the top and right borders of
ggplot2.*
http://stackoverflow.com/questions/10861773/remove-grid-background-color-and-top-and-right-borders-from-ggplot2
I paste the script here. Everyone can try it. I like it very much!
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 baground, gridlines, and chart border
theme( plot.background = element_blank() ,
panel.grid.major = element_blank() ,
panel.grid.minor = element_blank() ,
panel.border = element_blank() ,
panel.background = element_blank() ) +
#draws x and y axis line
theme(axis.line = element_line(color = 'black'))
[image: plot output]
Dear all,
A quick and simple technique post, and apologies if this is
obvious/already implemented somewhere else.
I was trying to have ggplot2 display only left and bottom ("L"-shaped)
borders, rather than a rectangle or no border, and saw on the web that this
has been a problem for others. My solution (based on the ggplot2 source
code in theme-elements.r) is pasted below, and is accessible directly from
source(
"http://egret.psychol.cam.ac.uk/statistics/R/extensions/rnc_ggplot2_border_themes.r"<http://egret.psychol.cam.ac.uk/statistics/R/extensions/rnc_ggplot2_border_themes.r>
)
opts( panel.border = theme_L_border() )
opts( panel.border = theme_left_border() )
opts( panel.border = theme_bottom_border() )
library(ggplot2)
source(
"http://egret.psychol.cam.ac.uk/statistics/R/extensions/rnc_ggplot2_border_themes.r"<http://egret.psychol.cam.ac.uk/statistics/R/extensions/rnc_ggplot2_border_themes.r>
)
df = data.frame(x=c(1,2,3),y=c(4,5,6))
ggplot(data=df, aes(x=x, y=y)) + geom_point() + theme_bw() + opts(
panel.border = theme_L_border() )
Note that the order can be significant; for example, if you specify the L
border option and then a theme, the theme settings will override the border
option, so you need to specify the theme (if any) before the border option,
as above.
The code is shown below.
all the best,
Rudolf.
# Rudolf Cardinal, March 2011
# Simple extensions to ggplot2 (v0.8.7); see
http://www.psychol.cam.ac.uk/statistics/R/
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()
)
}
theme_bottom_border <- function(colour = "black", size = 1, linetype = 1) {
structure(
function(x = 0, y = 0, width = 1, height = 1, ...) {
polylineGrob(
x=c(x, x+width), y=c(y,y), ..., default.units = "npc",
gp=gpar(lwd=size, col=colour, lty=linetype),
)
},
class = "theme",
type = "box",
call = match.call()
)
}
theme_left_border <- function(colour = "black", size = 1, linetype = 1) {
structure(
function(x = 0, y = 0, width = 1, height = 1, ...) {
polylineGrob(
x=c(x, x), y=c(y,y+height), ..., default.units = "npc",
gp=gpar(lwd=size, col=colour, lty=linetype),
)
},
class = "theme",
type = "box",
call = match.call()
)
}
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ggplot2-/***@public.gmane.org
To unsubscribe: email ggplot2+unsubscribe-/***@public.gmane.org
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Chmel87
2016-01-12 09:21:13 UTC
Permalink
Hi

Thanks a lot
This is a best solution, many other shared solutions did not work

Thanks again

Krystof
Dear all,
A quick and simple technique post, and apologies if this is
obvious/already implemented somewhere else.
I was trying to have ggplot2 display only left and bottom ("L"-shaped)
borders, rather than a rectangle or no border, and saw on the web that this
has been a problem for others. My solution (based on the ggplot2 source
code in theme-elements.r) is pasted below, and is accessible directly from
source(
"http://egret.psychol.cam.ac.uk/statistics/R/extensions/rnc_ggplot2_border_themes.r"
<http://egret.psychol.cam.ac.uk/statistics/R/extensions/rnc_ggplot2_border_themes.r>
)
opts( panel.border = theme_L_border() )
opts( panel.border = theme_left_border() )
opts( panel.border = theme_bottom_border() )
library(ggplot2)
source(
"http://egret.psychol.cam.ac.uk/statistics/R/extensions/rnc_ggplot2_border_themes.r"
<http://egret.psychol.cam.ac.uk/statistics/R/extensions/rnc_ggplot2_border_themes.r>
)
df = data.frame(x=c(1,2,3),y=c(4,5,6))
ggplot(data=df, aes(x=x, y=y)) + geom_point() + theme_bw() + opts(
panel.border = theme_L_border() )
Note that the order can be significant; for example, if you specify the L
border option and then a theme, the theme settings will override the border
option, so you need to specify the theme (if any) before the border option,
as above.
The code is shown below.
all the best,
Rudolf.
# Rudolf Cardinal, March 2011
# Simple extensions to ggplot2 (v0.8.7); see
http://www.psychol.cam.ac.uk/statistics/R/
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()
)
}
theme_bottom_border <- function(colour = "black", size = 1, linetype = 1) {
structure(
function(x = 0, y = 0, width = 1, height = 1, ...) {
polylineGrob(
x=c(x, x+width), y=c(y,y), ..., default.units = "npc",
gp=gpar(lwd=size, col=colour, lty=linetype),
)
},
class = "theme",
type = "box",
call = match.call()
)
}
theme_left_border <- function(colour = "black", size = 1, linetype = 1) {
structure(
function(x = 0, y = 0, width = 1, height = 1, ...) {
polylineGrob(
x=c(x, x), y=c(y,y+height), ..., default.units = "npc",
gp=gpar(lwd=size, col=colour, lty=linetype),
)
},
class = "theme",
type = "box",
call = match.call()
)
}
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ***@googlegroups.com
To unsubscribe: email ggplot2+***@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...