【问题标题】:Subgroup axes ggplot2 and axis limits子组轴 ggplot2 和轴范围
【发布时间】:2015-05-23 18:30:06
【问题描述】:

跟进:

Subgroup axes ggplot2 similar to Excel PivotChart

ggplot2 multiple sub groups of a bar chart

R version 3.1.1 (2014-07-10) Platform: i386-w64-mingw32/i386 (32-bit)

我正在与ggplot2 合作。目的是将轴调整为类似于 Excel 著名的枢轴图的外观。我知道,我如何才能实现我想要的外观,但是一旦我使用轴限制,代码就不再足够了。

数据:

library(reshape2)
library(ggplot2)
library(grid)

df=data.frame(year=rep(2010:2014,each=4),
          quarter=rep(c("Q1","Q2","Q3","Q4"),5),
          da=c(46,47,51,50,56.3,53.6,55.8,58.9,61.0,63,58.8,62.5,59.5,61.7,60.6,63.9,68.4,62.2,62,70.4))

df.m <- melt(data = df,id.vars = c("year","quarter"))

g1 <- ggplot(data = df.m, aes(x = interaction(quarter,year), y = value, group = variable)) +
      geom_area(fill = "red")+
      coord_cartesian(ylim = c(0, 75)) +
      annotate(geom = "text", x = seq_len(nrow(df)), y = -1.5, label = df$quarter, size = 2, color = "gray48") +
      annotate(geom = "text", x = 2.5 + 4 * (0:4), y = -3, label = unique(df$year), size = 3, color ="gray48") +
      theme_grey(base_size = 10)+
      theme(line = element_line(size = 0.2),
            axis.title.x = element_blank(),
            axis.text.x = element_blank(),
            legend.position= "none")

#remove clipping of x axis labels
g2 <- ggplot_gtable(ggplot_build(g1))
g2$layout$clip[g2$layout$name == "panel"] <- "off"
grid.draw(g2)

png(filename = "test.png",width = 14/2.54,height = 6/2.54, units = "in",res = 300)
grid.draw(g2)
dev.off()

情节很好,轴标签如愿。但是一旦你改变了 y 轴的界限,一切都搞砸了。

我希望你有一个想法,如何解决我的问题!

【问题讨论】:

  • 请修复您的代码,以便我们可以复制它并查​​看与您相同的结果。即使我添加了必要的library 调用,我也没有得到你的第一个情节。
  • 我添加了库调用。您不会在预览中看到该图,但在您保存后。
  • 我改变了注释图的位置。现在您也可以在预览中看到它。

标签: r plot ggplot2


【解决方案1】:

实际上,它正在绘制您所要求的内容。检查?geom_area,您会注意到最小y 为0。因此,当您关闭裁剪时,ggplot 将在下边距范围内显示尽可能多的区域。而是使用geom_ribbon()。它有 ymax 和 ymin。此外,您需要注意在两个 annotate() 函数中设置 y 坐标。

library(reshape2)
library(ggplot2)
library(grid)

df=data.frame(year=rep(2010:2014,each=4),
   quarter=rep(c("Q1","Q2","Q3","Q4"),5),
   da=c(46,47,51,50,56.3,53.6,55.8,58.9,61.0,63,58.8,62.5,59.5,61.7,60.6,63.9,68.4,62.2,62,70.4))

df.m <- melt(data = df,id.vars = c("year","quarter"))

ymin <- 40 
g1 <- ggplot(data = df.m, aes(x = interaction(quarter,year), ymax = value, group = variable)) +
      geom_ribbon(aes(ymin=ymin), fill = "red")+
      coord_cartesian(ylim = c(ymin, 75)) +
      annotate(geom = "text", x = seq_len(nrow(df)), y = 37.5, label = df$quarter, size = 2, color = "gray48") +
      annotate(geom = "text", x = 2.5 + 4 * (0:4), y = 36.5, label = unique(df$year), size = 3, color ="gray48") +
      theme_grey(base_size = 10)+
      theme(line = element_line(size = 0.2),
            axis.title.x = element_blank(),
            axis.text.x = element_blank(),
            legend.position= "none",
            plot.margin = unit(c(1,1,3,1), "lines"))     # The bottom margin is exaggerated a little

# turn off clipping of the panel
g2 <- ggplotGrob(g1)
g2$layout$clip[g2$layout$name == "panel"] <- "off"
grid.draw(g2)

【讨论】:

  • 感谢您的提示。 geom_ribbon 的解决方法是个好主意!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多