Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting only x and y axis borders (no box) in ggplot2?

Tags:

r

border

ggplot2

The convention of some journals is to show only the x and y axis in a plot not a box around the entire plot area. How can I achieve this in ggplot2? I tried theme_minimal_cb_L from HERE but it seems to erase the entire box around the plot (does not leave the x and y axis) as seen here:

enter image description here

Here's the code I'm using:

dat <- structure(list(x = c(0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1, 1.05, 
    1.1, 1.15, 1.2, 1.25, 1.3), y1 = c(34, 30, 26, 23, 21, 19, 17, 
    16, 15, 13, 12, 12, 11), y2 = c(45, 39, 34, 31, 28, 25, 23, 21, 
    19, 17, 16, 15, 14)), .Names = c("x", "y1", "y2"), row.names = c(NA, 
    -13L), class = "data.frame")


library(reshape2); library(ggplot2)
dat2 <- melt(dat, id='x')

theme_minimal_cb_L <- function (base_size = 12, base_family = "", ...){
  modifyList (theme_minimal (base_size = base_size, base_family = base_family),
              list (axis.line = element_line (colour = "black")))
}

ggplot(data=dat2, aes(x=x, y=value, color=variable)) + 
    geom_point(size=3) + geom_line(size=.5) +
    theme_minimal_cb_L()
like image 372
Tyler Rinker Avatar asked Oct 14 '12 14:10

Tyler Rinker


2 Answers

I believe you're saying you just want the lines on the left and the bottom of the plot - correct?

theme(panel.border = element_blank(), axis.line = element_line())

The panel.border removes the overall border and then using axis.line adds in the x and y axis lines.


Edit: Since the additional theme stuff you posted didn't work for me. I'll post the code I actually ran

dat <- structure(list(x = c(0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1, 1.05, 
    1.1, 1.15, 1.2, 1.25, 1.3), y1 = c(34, 30, 26, 23, 21, 19, 17, 
    16, 15, 13, 12, 12, 11), y2 = c(45, 39, 34, 31, 28, 25, 23, 21, 
    19, 17, 16, 15, 14)), .Names = c("x", "y1", "y2"), row.names = c(NA, 
    -13L), class = "data.frame")


library(reshape2); library(ggplot2)
dat2 <- melt(dat, id='x')

ggplot(data=dat2, aes(x=x, y=value, color=variable)) + 
    geom_point(size=3) + geom_line(size=.5) +
    theme(panel.border = element_blank(), axis.line = element_line())

# or using the template you used from talkstats...
ggplot(data=dat2, aes(x=x, y=value, color=variable)) + 
    geom_point(size=3) + geom_line(size=1.5) +
    scale_y_continuous(breaks=seq(0, 45, by=5)) +
    scale_x_continuous(breaks=seq(.7, 1.3, by=.05)) +
    theme_bw() + ylab("Sample Size") + xlab("Mean Difference") +
    theme(legend.position="bottom", legend.title=element_blank(),
        legend.key = element_rect(colour = 'white'), 
        legend.background = element_rect(colour = "black")) +
    ggtitle("Sample Size vs. Mean Difference by Power") +
    theme(panel.border = element_blank(), axis.line = element_line())
like image 143
Dason Avatar answered Sep 22 '22 10:09

Dason


axis.line() seems not to be working anymore. Instead use axis.line.x() and axis.line.y()

So adding this to your plot:

... + theme(panel.border = element_blank(),
axis.line.x = element_line(size = 0.5, linetype = "solid", colour = "black"),
axis.line.y = element_line(size = 0.5, linetype = "solid", colour = "black")) + ...

would do the trick.

like image 42
A.P Avatar answered Sep 19 '22 10:09

A.P