I feel like this should be obvious... all I\'m trying to do is to remove the x-axis from the bottom of my graph and add it to the top.
Here is a reproducible examp
check out the cowplot package
ggdraw(switch_axis_position(p + axis = 'x'))
You can move the x-axis labels to the top by adding
scale_x_discrete(position = "top")
see http://ggplot2.tidyverse.org/reference/sec_axis.html
scale_y_continuous(sec.axis = dup_axis())
The only way I can think to do this is to go wild with the vjust
theme option for the axis labels (and the axis title as well, I suppose). Something like:
df <- data.frame(x = 1:10, y = (1:10)^2) # example data
p <- ggplot(df, aes(x = x, y = y)) + geom_point() +
theme(
axis.text.x = element_text(vjust = -5),
axis.title.x = element_text(vjust = -5))
vjust
doesn't work with element_text
, though, so I"m stuck on moving the axis ticks.
I've used this work around. Used a duplicated the plot's x-axis and affixed the two together, then crop. I don't have the cleaned up code, but below is an example.
p.bot <-
ggplot(cor.dat, aes(Var2, Var1, fill = value)) +
geom_tile(colour="gray90", size=1.5, stat="identity") +
geom_text(data=cor.dat, aes(Var2, Var1, label = value), color="black", size=rel(4.5)) +
scale_fill_gradient(low = "white", high = "dodgerblue", space = "Lab", na.value = "gray90", guide = "colourbar") +
scale_x_discrete(expand = c(0, 0)) +
scale_y_discrete(expand = c(0, 0)) +
xlab("") +
ylab("") +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_rect(fill=NA,color="gray90", size=0.5, linetype="solid"),
axis.line = element_blank(),
axis.ticks = element_blank(),
panel.background = element_rect(fill="gray90"),
plot.background = element_rect(fill="gray90"),
legend.position = "none",
axis.text.x = element_blank(),
plot.margin = unit(c(1,0,0,0), "cm"),
axis.text.y = element_text(color="black", size=14) )
p.top <- p.bot + theme(
axis.text.x = element_text(color="black", size=14),
axis.text.y = element_text(color="gray90", size=14)
) + coord_cartesian(ylim = c(0,0))
require(gtable)
#Extract Grobs
g1<-ggplotGrob(p.top)
g2<-ggplotGrob(p.bot)
#Bind the tables
g<-gtable:::rbind_gtable(g1, g2, "first")
#Remove a row between the plots
g <- gtable_add_rows(g, unit(-1.25,"cm"), pos=nrow(g1))
#draw
panels <- g$layout$t[grep("panel", g$layout$name)]
g$heights[panels] <- lapply(c(0,2), unit, "null")
grid.newpage()
grid.draw(g)