Henning Wildhagen
2010-Dec-06 22:46 UTC
[R] ggplot2: Controlling line width of panel borders
Dear R-users,
i encountered some problems when trying to adjust the line width of the
axes and stripes in a plot created with ggplot2.
I use the "barley" dataset of the lattice package to illustrate my
problem:
library(ggplot2)
library(lattice)
barley[["SD"]] <- 5
limits <- aes(ymax=barley$yield + barley$SD,ymin=barley$yield - barley$SD)
p1 <- ggplot(barley,aes(fill=year,y=yield,x=variety))
p2 <- p1 + geom_bar(position="dodge",stat="identity")
dodge <- position_dodge(width=0.9)
p3 <- p2 + geom_errorbar(limits,position=dodge,width=0.5,size=0.7)
p4 <- p3 + facet_wrap(~site,ncol=2)
p5 <- p4+theme_bw(base_size=16)
p5
last_plot() + opts(panel.grid.minor=theme_blank()) # deletes the minor grid
lines
## Now i would like to increase the width of all remaining lines (bottom,
left, top, right border of all panels and stripes) in the plot, as a
certain minimum lines width is required by many journals
last_plot() +
opts(panel.grid.major=theme_line(colour="darkgrey",size=0.71))
# that works fine
last_plot() + opts(panel.border=theme_rect(colour="black",size=0.71))
# that only changes the bottom and right border of the panel
last_plot() +
opts(strip.background=theme_rect(colour="black",fill="grey90",size=0.71))
# that only changes the bottom and right border of the stripes
last_plot() + opts(axis.line=theme_segment(size=0.71))
# trying to increase the line width of the left border of the panel, i.e.
the y-axis,
# however, that changes only the y-axis of the left column of panels, not
of the right column
Thanks for any hints on how to increase the line width of the borders of
panels and stripes in this plot!
Henning
--
[[alternative HTML version deleted]]
Hi: You might find this link useful: https://github.com/hadley/ggplot2/wiki/%2Bopts%28%29-List ...and apropos your choice of plot, http://emdbolker.wikidot.com/blog:dynamite Cheers, Dennis On Mon, Dec 6, 2010 at 2:46 PM, Henning Wildhagen <HWildhagen@gmx.de> wrote:> Dear R-users, > > i encountered some problems when trying to adjust the line width of the > axes and stripes in a plot created with ggplot2. > I use the "barley" dataset of the lattice package to illustrate my problem: > > > library(ggplot2) > library(lattice) > barley[["SD"]] <- 5 > > limits <- aes(ymax=barley$yield + barley$SD,ymin=barley$yield - barley$SD) > > > p1 <- ggplot(barley,aes(fill=year,y=yield,x=variety)) > p2 <- p1 + geom_bar(position="dodge",stat="identity") > > dodge <- position_dodge(width=0.9) > p3 <- p2 + geom_errorbar(limits,position=dodge,width=0.5,size=0.7) > p4 <- p3 + facet_wrap(~site,ncol=2) > p5 <- p4+theme_bw(base_size=16) > p5 > > last_plot() + opts(panel.grid.minor=theme_blank()) # deletes the minor grid > lines > > ## Now i would like to increase the width of all remaining lines (bottom, > left, top, right border of all panels and stripes) in the plot, as a > certain minimum lines width is required by many journals >> last_plot() + > opts(panel.grid.major=theme_line(colour="darkgrey",size=0.71)) > # that works fine > last_plot() + opts(panel.border=theme_rect(colour="black",size=0.71)) > # that only changes the bottom and right border of the panel > last_plot() + > opts(strip.background=theme_rect(colour="black",fill="grey90",size=0.71)) > # that only changes the bottom and right border of the stripes > last_plot() + opts(axis.line=theme_segment(size=0.71)) > # trying to increase the line width of the left border of the panel, i.e. > the y-axis, > # however, that changes only the y-axis of the left column of panels, not > of the right column > > Thanks for any hints on how to increase the line width of the borders of > panels and stripes in this plot! > > Henning > > -- > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
thomas.parr at maine.edu
2013-Mar-27 20:37 UTC
[R] ggplot2: Controlling line width of panel borders
This is an old post, but I had the exact problem described here and after
two days of monkeying around in theme, I wanted to provide the non-ggplot
related "solution."
In summary, the problem is that in the faceted plots, lines on the right an
bottom in the subplots appear to be thicker than faceted. This is a
problem with the graphics device you are using (a windows x11 device I
suspect). I have provided a slightly more concise illustration of the
problem.
################
x11()
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p + facet_grid(. ~ cyl)+
theme_bw()+ #drop a mostly white theme on for contrast
theme(panel.border = element_rect(fill=NA, colour = "black", size=1))
#bump
up the border size to 1, the minimum for many publications.
################
Now do the same with:
################
pdf(file="mygraphic.pdf")
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p + facet_grid(. ~ cyl)+
theme_bw()+ #drop a mostly white theme on for contrast
theme(panel.border = element_rect(fill=NA, colour =
"black",
size=1)) #bump up the border size to
1, the minimum for many publications.
dev.off()
################
This will output directly to a file so you will need to hunt it down.
Inspect it and you will see that there is no problem, everything looks
exactly the way you coded it.
In short, the problem is not with ggplot. X11(), I assume, or some unseen
process calling a windows graphic device is just not visually representing
the code properly. If you use a pdf graphics device to output the figure
the "problem" goes away. However, this means you can't view it
instantly... but you do have a vectorized figure.
Trust your code not your graphics device.
>