In my real research world, it is very common to show x-axis on the top (or both top and bottom) and y-axis on the right. However, the default positions are x on the bottom and y on the left in ggplot2.
Following Kohske Post Here, the commands used are:
x <- seq(0, 10, 0.1) y <- sin(x * pi) qplot(x, y, geom = "line") + scale_x_continuous(guide = guide_axis(position = "top")) + scale_y_continuous(guide = guide_axis(position = "right"))
I have tried above commands in dev-mode:
install_packages("devtools") library(devtools) dev_mode() install_github("ggplot2", "kohske", "feature/pguide") library(ggplot2)
Unfortunately, it didn't work well with the latest plyr
package. Messages:
The following 'from' values not present in 'x': col, color, pch, cex, lty, lwd, srt, adj, bg, fg, min, max... Error in plyr:::split_indices(seq_len(nrow(data)), scale_id, n)
Then I tried the codes from github directedly, the messages are:
Error in continuous_scale(c("x", "xmin", "xmax", "xend", "xintercept"), : formal argument "guide" matched by multiple actual arguments
I have noticed that Hadley said this functionality is on his to-do list. However, I could not find a solution at this moment. Could anyone help?
From ggplot 2.2.0
you can set the position of the axes with the position
argument in scale_
:
ggplot(mpg, aes(displ, hwy)) + geom_point() + scale_x_continuous(position = "top") + scale_y_continuous(position = "right")
ggplot2 solution
I adopt This solution to create a right y axis. Personally I find manipulating grobs using within a gtable really difficult. I give up with the x-axis but I give a lattice solution. I hope this functionality will be implemented in ggplot2 as soon as possible.
library(ggplot2) library(gtable) library(grid) grid.newpage() dat <- data.frame(x<-seq(0, 10, 0.1),y = sin(x * pi)) p <- ggplot(dat, aes(x, y)) + geom_line(colour = "blue") + theme_bw() # extract gtable g <- ggplot_gtable(ggplot_build(p)) # axis tweaks ia <- which(g$layout$name == "axis-l") ax <- g$grobs[[ia]]$children[[2]] ax$widths <- rev(ax$widths) ax$grobs <- rev(ax$grobs) ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm") pp <- c(subset(g$layout, name == "panel", select = t:r)) g <- gtable_add_cols(g, g$widths[g$layout[ia, ]$l], length(g$widths) - 1) g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b) g$grobs[[ia]]$children[[2]] <- NULL ############################## ia <- which(g$layout$name == "ylab") ylab <- g$grobs[[ia]] g <- gtable_add_cols(g, g$widths[g$layout[ia, ]$l], length(g$widths) - 1) g <- gtable_add_grob(g, ylab, pp$t, length(g$widths) - 1, pp$b) g$grobs[[ia]]$label = '' grid.draw(g)
lattice solution
This is not a ggplot2 solution , but lattice
one. Using latticeExtra
with a ggplot2 theme we can get a similar look with the desired behavior.
library(latticeExtra) xyplot(y~ x, type='l', scales=list(x=list(alternating=2), y=list(alternating=2)), par.settings = ggplot2like(),axis=axis.grid)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With