将x轴放在ggplot2图表的顶部 [英] Putting x-axis at top of ggplot2 chart

查看:641
本文介绍了将x轴放在ggplot2图表的顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得这应该是显而易见的......我所要做的就是从图表底部删除x轴并将其添加到顶部。

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.

这是一个可重现的例子。 Data plus code to make the following graph:

Here is a reproducible example. Data plus code to make the following graph:

library(reshape2)
library(ggplot2)

data(mtcars)
dat <- with(mtcars, data.frame(mpg, cyl, disp, hp, wt, gear))
cor.matrix <- round(cor(dat, use = "pairwise.complete.obs", method = "spearman"), digits = 2)
diag(cor.matrix)<-NA

cor.dat <- melt(cor.matrix)
cor.dat <- data.frame(cor.dat)
cor.dat <- cor.dat[complete.cases(cor.dat),]


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 = element_text(color="black", size=14) )

但是我试图产生的是 - 这应该是显而易见的(例如,在base-R中很容易做到),但是我还没有设法在ggplot2中找到我要找的东西。

But what I'm trying to produce is- it seems like this should be obvious (e.g. it's very easy to do in base-R) , but I haven't managed to find what I'm looking for in ggplot2.

推荐答案

您可以通过添加

You can move the x-axis labels to the top by adding

scale_x_discrete(position = "top") 

这篇关于将x轴放在ggplot2图表的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆