source("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() )
or, to put it into a more concrete example: library(ggplot2)
source("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() )
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2
Just a thought, perhaps it'd be useful to generalise this to any type
of partial frame. There's a function in gridExtra that I wrote for
similar purposes; maybe the "type" could be given as a parameter.
library(gridExtra)
example(borderGrob)
Best,
baptiste
Good idea! I've added a theme_border(type=..., ...) function (pasted
below, and the web version of the source now has all four). The type
parameter matches your "library(gridExtra); example(borderGrob)" code.
Example use:
source("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_border(type=9) )
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) {
# use with e.g. "ggplot(...) + opts( panel.border=theme_L_border() ) + ..."
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) {
# use with e.g. "ggplot(...) + opts( panel.border=theme_bottom_border() ) + ..."
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) {
# use with e.g. "ggplot(...) + opts( panel.border=theme_left_border() ) + ..."
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()
)
}
theme_border<- function(type, colour = "black", size = 1, linetype = 1) {
# use with e.g. "ggplot(...) + opts( panel.border=theme_border(type=9) ) + ..."
structure(
function(x = 0, y = 0, width = 1, height = 1, ...) {
# types from: library(gridExtra); example(borderGrob)
# 1=none, 2=bottom, 3=right, 4=top
# 5=left, 6=B+R, 7=T+R, 8=T+L
# 9=B+L, 10=T+B, 11=L+R, 12=T+B+R
# 13=T+L+R, 14=T+B+L, 15=B+L+R, 16=T+B+L+R
xlist<- c()
ylist<- c()
idlist<- c()
if (type==1) { # blank; can't pass absence of coordinates, so pass a single point and use an invisible line
xlist<- c(x,x)
ylist<- c(y,y)
idlist<- c(1,1)
linetype<- "blank"
}
if (type==2 || type==6 || type==9 || type==10 || type==12 || type==14 || type==15 || type==16) { # bottom
xlist<- append(xlist, c(x, x+width))
ylist<- append(ylist, c(y, y))
idlist<- append(idlist, c(1,1))
}
if (type==4 || type==7 || type==8 || type==10 || type==12 || type==13 || type==14 || type==16) { # top
xlist<- append(xlist, c(x, x+width))
ylist<- append(ylist, c(y+height, y+height))
idlist<- append(idlist, c(2,2))
}
if (type==5 || type==8 || type==9 || type==11 || type==13 || type==14 || type==15 || type==16) { # left
xlist<- append(xlist, c(x, x))
ylist<- append(ylist, c(y, y+height))
idlist<- append(idlist, c(3,3))
}
if (type==3 || type==6 || type==7 || type==11 || type==12 || type==13 || type==15 || type==16) { # right
xlist<- append(xlist, c(x+width, x+width))
ylist<- append(ylist, c(y, y+height))
idlist<- append(idlist, c(4,4))
}
polylineGrob(
x=xlist, y=ylist, id=idlist, ..., default.units = "npc",
gp=gpar(lwd=size, col=colour, lty=linetype),
)
},
class = "theme",
type = "box",
call = match.call()
)
}
There is a much better method to remove the top and right borders of ggplot2.Ref:I paste the script here. Everyone can try it. I like it very much!library(ggplot2)a <- seq(1,20)b <- a^0.25df <- as.data.frame(cbind(a,b)) #base ggplot objectp <- ggplot(df, aes(x = a, y = b))p + #plots the pointsgeom_point() + #theme with white backgroundtheme_bw() + #eliminates baground, gridlines, and chart bordertheme( 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 linetheme(axis.line = element_line(color = 'black'))#generates: