【问题标题】:Putting x-axis at top of ggplot2 chart将 x 轴放在 ggplot2 图表的顶部
【发布时间】:2014-11-10 06:30:18
【问题描述】:

我觉得这应该很明显......我要做的就是从图表底部删除 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) )

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

【问题讨论】:

  • ggplot 中的轴不如基本 R 中的灵活。对于您的示例,除了标签之外,您没有使用任何轴组件。我想说你最好的选择是根本不绘制轴,只需使用 geom_text 将标签放在你想要的位置。
  • 移动 x 轴很困难,但可以使用 ggplot_gtable 和 ggplot_build 函数。这些函数“绘制”ggplot,但不是在屏幕上,而是生成所谓的“grob”对象,代表视觉元素(框、线、网格等),但在与 ggplot 对象不同的级别(比例尺、主题、美学等)。您可以根据需要操作 grobs、交换它们、调整它们的大小等。
  • 除非您将其自动化,否则您可能只想制作图形然后在事后对其进行编辑。在 illustrator 或 inkscape 中移动轴需要几秒钟。
  • 我发现了这个页面,因为我正在开发一个闪亮的应用程序; inkscape 或 Illustrator 是不可能的!

标签: r ggplot2


【解决方案1】:

您可以通过添加将x轴标签移动到顶部

scale_x_discrete(position = "top") 

【讨论】:

  • 请注意,如果您在代码中使用 coord_flip(),此解决方案可能无法按预期工作。我认为这是 ggplot2 中更大的问题,其中一些函数(如 scale...labs)的“x”和“y”参数继续表示 aes() 调用中定义的“x”和“y”,并且当您翻转坐标时,其他像“facet_wrap”之类的会改变它们的含义。您可以通过在 aes() 调用中手动翻转 x 和 y 变量来轻松解决此问题。
【解决方案2】:

我已经使用了这个解决方法。使用重复的绘图 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)

【讨论】:

    【解决方案3】:

    查看cowplot package

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

    【讨论】:

    • 在当前版本的cowplot下,我需要把加号换成逗号:ggdraw(switch_axis_position(p, axis = 'x'))
    【解决方案4】:

    现在,您可以通过在 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】:

    http://ggplot2.tidyverse.org/reference/sec_axis.html

    scale_y_continuous(sec.axis = dup_axis())
    

    【讨论】:

    • 考虑通过解释代码的作用/为什么它解决 OP 的问题并总结您共享的链接中可用的详细信息来扩展您的答案。
    • 虽然此代码 sn-p 可能是解决方案,但包含解释确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因
    【解决方案6】:

    我能想到的唯一方法是使用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 一起使用,所以我一直在移动轴刻度。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-02
      • 1970-01-01
      • 1970-01-01
      • 2020-03-20
      相关资源
      最近更新 更多