Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use <code>position = 'top'</code></p> <pre><code>library(ggplot2) # Generate some data df = data.frame(x = 1:10, y = 1:10) # x-axis breaks breaks = 1:10 # base plot p &lt;- ggplot(df, aes(x,y)) + geom_point() + scale_x_continuous(breaks = breaks, position = 'top') + scale_y_continuous(limits = c(0, 11), expand = c(0,0)) + theme_bw() </code></pre> <p><strong>Original solution</strong> available using <code>gtable</code> (with some updating to ggplot 2.1.0). See <a href="https://stackoverflow.com/questions/28199667/r-ggplot2-log10-x-axis-on-top-and-y-axis-geom-line/28206324#28206324">here</a></p> <pre><code>library(ggplot2) library(gtable) library(grid) # Generate some data df = data.frame(x = 1:10, y = 1:10) # x-axis breaks breaks = 1:10 # base plot p &lt;- ggplot(df, aes(x,y)) + geom_point() + scale_x_continuous(breaks = breaks) + scale_y_continuous(limits = c(0, 11), expand = c(0,0)) + theme_bw() # Get ggplot grob g1 &lt;- ggplotGrob(p) ## Get the position of the plot panel in g1 pp &lt;- c(subset(g1$layout, name == "panel", se = t:r)) # Title grobs have margins. # The margins need to be swapped. # Function to swap margins - # taken from the cowplot package: # https://github.com/wilkelab/cowplot/blob/master/R/switch_axis.R vinvert_title_grob &lt;- function(grob) { heights &lt;- grob$heights grob$heights[1] &lt;- heights[3] grob$heights[3] &lt;- heights[1] grob$vp[[1]]$layout$heights[1] &lt;- heights[3] grob$vp[[1]]$layout$heights[3] &lt;- heights[1] grob$children[[1]]$hjust &lt;- 1 - grob$children[[1]]$hjust grob$children[[1]]$vjust &lt;- 1 - grob$children[[1]]$vjust grob$children[[1]]$y &lt;- unit(1, "npc") - grob$children[[1]]$y grob } # Get xlab and swap margins index &lt;- which(g1$layout$name == "xlab-b") xlab &lt;- g1$grobs[[index]] xlab &lt;- vinvert_title_grob(xlab) # Put xlab at the top of g1 g1 &lt;- gtable_add_rows(g1, g1$heights[g1$layout[index, ]$t], pp$t-1) g1 &lt;- gtable_add_grob(g1, xlab, pp$t, pp$l, pp$t, pp$r, clip = "off", name="topxlab") # Get x axis (axis line, tick marks and tick mark labels) index &lt;- which(g1$layout$name == "axis-b") xaxis &lt;- g1$grobs[[index]] # Swap axis ticks and tick mark labels ticks &lt;- xaxis$children[[2]] ticks$heights &lt;- rev(ticks$heights) ticks$grobs &lt;- rev(ticks$grobs) # Move tick marks # Get tick mark length plot_theme &lt;- function(p) { plyr::defaults(p$theme, theme_get()) } tml &lt;- plot_theme(p)$axis.ticks.length # Tick mark length ticks$grobs[[2]]$y &lt;- ticks$grobs[[2]]$y - unit(1, "npc") + tml # Swap tick mark labels' margins and justifications ticks$grobs[[1]] &lt;- vinvert_title_grob(ticks$grobs[[1]]) # Put ticks and tick mark labels back into xaxis xaxis$children[[2]] &lt;- ticks # Add axis to top of g1 g1 &lt;- gtable_add_rows(g1, g1$heights[g1$layout[index, ]$t], pp$t) g1 &lt;- gtable_add_grob(g1, xaxis, pp$t+1, pp$l, pp$t+1, pp$r, clip = "off", name = "axis-t") # Remove original x axis and xlab g1 = g1[-c(9,10), ] # Draw it grid.newpage() grid.draw(g1) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload