【问题标题】:How to add multiple level of x-axis in ggplot [duplicate]如何在ggplot中添加多级x轴[重复]
【发布时间】:2016-07-18 02:59:08
【问题描述】:
variable <-     c("PM10","SO2","NO","NO2","PM10","SO2","NO","NO2","PM10","SO2","NO","NO2","PM10","SO2","NO","NO2","PM10","SO2","NO","NO2","PM10","SO2","NO","NO2")
sex <- c("male","male","male","male","female","female","female","female",
     "male","male","male","male","female","female","female","female",
     "male","male","male","male","female","female","female","female")
exposureperiod <- c("P1","P1","P1","P1","P1","P1","P1","P1",
                "P2","P2","P2","P2","P2","P2","P2","P2",
                "P3","P3","P3","P3","P3","P3","P3","P3")

set.seed(100)
coef <- runif(24, -2, 2)
coef_lb <- coef - 0.3
coef_ub <- coef + 0.3

df <- data.frame(variable,sex,exposureperiod,coef,coef_lb,coef_ub)

df$variable <- factor(df$variable,levels=c("PM10","SO2","NO","NO2"))
levels(df$variable) <- c("PM[10]","SO[2]", "NO", "NO[2]")

df$exposureperiod <- factor(df$exposureperiod,levels=c("P1","P2","P3"))
df$sex <- factor(df$sex,levels=c("male","female"))

df <- df[order(df$variable,df$sex),]

df$aux <- c(1,2,3,
         5,6,7,
         11,12,13,
         15,16,17,
         21,22,23,
         25,26,27,
         31,32,33,
         35,36,37)

library(ggplot2)

plot <- ggplot(data = df, aes(x = aux, y = coef)) +
  geom_pointrange(aes(ymin=coef_lb,ymax=coef_ub),shape="none") +
  geom_point(aes(shape = exposureperiod)) +
  scale_shape_discrete(name  ="Exposure period",
                   breaks=c("P1", "P2","P3"),
                   labels=c("P1","P2","P3")) +
  scale_x_continuous("Sex and Pollutant",breaks=c(2,6,12,16,22,26,32,36), 
                 labels=c("Boys","Girls","Boys","Girls","Boys","Girls","Boys","Girls")) +
  scale_y_continuous("Mean Difference in Tanner Stage",
                 limits=c(-3, 3),
                 breaks=round(seq(-3, 3, by = 0.5),1)) +
  geom_hline(yintercept=0,alpha=1,linetype="dashed") +
  theme(axis.text.x = element_text()) +
  theme_bw(base_size = 16,base_family="Arial") +
  theme(legend.text.align = 0,
    legend.title = element_text(face="plain"),
    legend.key = element_blank(),
    legend.position = "bottom") +
  guides(shape= guide_legend(nrow = 3,byrow = TRUE)) +
  theme(text = element_text(colour = "black",face="plain"),
    axis.title.y = element_text(face="plain"),
    axis.title.x = element_text(face="plain"),
    axis.text.x = element_text(face="plain",hjust = 0),
    axis.text.y = element_text(face="plain")) + 
  theme(panel.background = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank(),
    axis.line = element_line(colour = "black"))+
  theme(axis.ticks = element_line(size = 1))

plot

使用上面的脚本,我得到了如下图。

但我想添加另一个级别的 x 轴,表示 PM10、SO2、NO 和 NO2,如下图所示。 (为了说明,我手动添加了这些污染物。)当然,x 轴标题和图例应该相应地向下移动。

我之前用过facet,但我想避免facet产生的污染物之间的差距。

谢谢。

【问题讨论】:

  • 我建议按污染物分面(并将主题和分面调整到与此接近的近似值)。污染物之间的小间隙(就像你在刻面中得到的那样)会改善情节。
  • 其实我之前确实用过facet,但是x轴被分成了四个线段,不能合二为一。鉴于我想避免污染物之间的小间隙,我转向这个。
  • 那么你对 ggplot2 不走运。没有差距会比有差距更糟糕地展示您的数据。
  • 你或许可以从this获取一些东西
  • 您可以通过panel.margin 消除构面之间的空格。这与facet_wrap 的较新的switch 参数相结合,似乎是获得所需情节的直接方法。

标签: r ggplot2 axis


【解决方案1】:

你可以试试看剧情

plot <- plot + facet_wrap(~variable)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多