将x轴放在ggplot2图表的顶部

时间:2014-11-10 06:30:18

标签: r ggplot2

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

这是一个可重复的例子。数据加代码来制作以下图表:

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) )

enter image description here

但是我试图制作的东西似乎应该是显而易见的(例如它在base-R中很容易做到),但我还没有设法找到什么我正在寻找ggplot2。

enter image description here

6 个答案:

答案 0 :(得分:60)

您可以通过添加

将x轴标签移到顶部
scale_x_discrete(position = "top") 

答案 1 :(得分:6)

我已经使用过这项工作了。使用复制的情节x轴并将两者粘在一起,然后裁剪。我没有清理过的代码,但下面是一个例子。

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)

enter image description here

答案 2 :(得分:5)

查看cowplot package

ggdraw(switch_axis_position(p + axis = 'x'))

答案 3 :(得分:1)

请参阅http://ggplot2.tidyverse.org/reference/sec_axis.html

scale_y_continuous(sec.axis = dup_axis())

答案 4 :(得分:0)

现在,您可以通过在 scale_x_discrete 中添加“position = 'top'”来实现。

代码:

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),position = 'top') +
  scale_y_discrete(expand = c(0, 0),position = 'left') +
  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) )

答案 5 :(得分:-1)

我能想到的唯一方法是使用轴标签的vjust主题选项(以及我想的轴标题)。类似的东西:

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不能与element_text一起使用,所以我“坚持移动轴刻度。”