使用facet_wrap在ggplot中添加唯一轴标签名称

时间:2019-08-07 18:40:40

标签: r ggplot2 facet

似乎很多用户都在尝试做我在这里也尝试过的事情。

我正在尝试执行以下操作:

  1. 固定x轴标签(范围为0到1200),每个图重复x轴,每个图重复和可自定义x轴标签名称
  2. y轴标签是自由的(但要能够将第二个图中的范围编辑为breaks=c(0, 3, 6, 9, 12)),每个图上要重复的y轴,以及y轴标签可以针对每个情节重复和自定义

使用以下代码(adapted from previous OPs and documentation):

ggplot(mapping=aes(x=dist, fill=sex)) +
   geom_histogram(data = cbind(move, panel = "A"), color="black", binwidth=10) +
   geom_vline(data = cbind(move, panel = "A"), aes(xintercept=mean(dist)), 
    color="black", linetype="dashed", size=1)+
   geom_histogram(data = cbind(settle_data, panel = "B"), color="black", binwidth=10) +
   geom_vline(data = cbind(settle_data, panel = "B"), aes(xintercept=mean(dist)), 
    color="black", linetype="dashed", size=1)+
   geom_histogram(data = cbind(recruit_data, panel = "C"), color="black", binwidth=10) +
   geom_vline(data = cbind(recruit_data, panel = "C"), aes(xintercept=mean(dist)), 
    color="black", linetype="dashed", size=1)+
    coord_cartesian(xlim=c(0,1200))+ #limits axes range without deleting points
    scale_fill_manual(values=c("gray97", "gray47"),
            name = "Sex",   
            breaks=c("F","M"),
            labels=c("Female","Male")) +
    theme_bw() +
    theme(legend.position = c(0.8,0.8), #move legend into plot
        axis.line = element_line(colour = "black"),
        axis.text=element_text(size = 20), #changes size of axes #s
        axis.title=element_text(size=20), #changes size of axes labels
        plot.caption = element_text(hjust = 0, vjust = 2.12),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        text = element_text(size = 15)) +
    facet_wrap(~ panel, ncol = 1, scales='free_y', labeller=as_labeller(c(A = "Number of fixes", B = "Number of individuals", C = "Number of individuals") ), strip.position="left") +
    scale_x_continuous(breaks = seq(0,1250,by = 200)) +     
    theme(
    strip.placement = "outside",
    strip.background = element_blank(),
        axis.title.y = element_blank(),
        strip.text = element_text(size = 19))+
    labs(x = "Distance travelled from natal nest (m)")

结果图(通过将面板名称移动到图的左侧,我可以“破解”我遇到的y轴问题,就像它们是轴标签一样):

enter image description here

我想要的情节(编辑用红色圆圈圈出): enter image description here

我无法发布用于绘图的数据,但是我在下面创建了一个样本数据集:

move<- data.frame(
    dist = c(0,100,200,300,400,400,500,600,700,800,1000,1200,0,100,200,300,400,400,500,600,700,800,1000,1200,0,100,200,300,400,400,500,600,700,800,1000,1200,0,100,200,300,400,400,500,600,700,800,1000,1200),
    sex = c ("F", "M", "F", "F", "F", "M", "M", "M", "M", "M", "F","F","F", "M", "F", "F", "F", "M", "M", "M", "M", "M", "F", "F","F", "M", "F", "F", "F", "M", "M", "M", "M", "M", "F", "F","F", "M", "F", "F", "F", "M", "M", "M", "M", "M", "F", "F"))
settle_data<-data.frame(
    dist = c(0,10,20,30,400,40,50,60,700,80,1000,1200,0,10,20,30,400,40,50,60,700,80,1000,1200, 0,10,20,30,400,40,50,60,700,80,1000,1200,0,100,200,300,400,400,500,600,700,800,1000,1200,0,10,20,30,400,40,50,60,700,80,1000,1200,0,10,20,30,400,40,50,60,700,80,1000,1200,0,10,20,30,400,40,50,60,700,80,1000,1200,0,100,200,300,400,400,500,600,700,800,1000,1200),
    sex = c ("F", "M", "F", "F", "F", "M", "M", "M", "M", "M", "F","F","F", "M", "F", "F", "F", "M", "M", "M", "M", "M", "F", "F","F", "M","F", "F", "F", "M", "M", "M", "M", "M", "F", "F","F", "M", "F", "F","F", "M", "M", "M", "M", "M", "F", "F", "F", "M", "F", "F", "F", "M", "M", "M","M", "M", "F", "F","F", "M", "F", "F", "F", "M", "M", "M", "M", "M","F", "F","F", "M", "F", "F", "F", "M", "M", "M", "M", "M", "F", "F","F", "M","F", "F", "F", "M", "M", "M", "M", "M", "F", "F"))
recruit_data<- data.frame(
    dist = c(0,10,20,30,400,40,50,60,700,80,1000,1200,0,10,20,30,4,40,50,60,700,80,10,120,0,10,20,30,40,40,50,60,70,80,100,120,0,100,200,300,400,400,500,600,700,800,1000,1200),
    sex = c ("F", "M", "F", "F", "F", "M", "M", "M", "M", "M", "F","F","F", "M", "F", "F", "F", "M", "M", "M", "M", "M", "F", "F","F", "M","F", "F", "F", "M", "M", "M", "M", "M", "F", "F","F", "M", "F", "F","F", "M", "M", "M", "M", "M", "F", "F"))

2 个答案:

答案 0 :(得分:3)

我同意大多数意见,即分开绘制情节会更容易。这种方法的痛点通常是轴的对齐不正确。为了解决这个问题,我将指向patchwork程序包,它消除了大部分麻烦。

library(ggplot2)
library(patchwork)

# Get some basic plots
dat_list <- list(move, recruit_data, settle_data)

plots <- lapply(dat_list, function(df) {
  ggplot(df, aes(dist, fill = sex)) +
    geom_histogram(binwidth = 10) +
    scale_x_continuous(limits = c(0, 1200)) +
    theme(legend.position = "none")
})

# Make adjustments to each plot as needed
plots[[1]] <- plots[[1]] + labs(x = "Distance settled (m)", y = "Number of fixes") + 
  theme(legend.position = c(1,1), legend.justification = c(1,1))

plots[[2]] <- plots[[2]] + labs(x = "Distance recruited (m)", y = "Number of individuals") + 
  scale_y_continuous(breaks = seq(0, 12, by = 3), limits = c(0, 12))

plots[[3]] <- plots[[3]] + labs(x = "Distance from natal nest (m)", y = "Number of individuals")

# Patchwork all the plots together
plots[[1]] + plots[[2]] + plots[[3]] + plot_layout(nrow = 3)

enter image description here

微调口味。

答案 1 :(得分:0)

对于那些想要或需要使用facet_wrap的人来说,这是对我有用的解决方案(从评论中收集建议)。

以下编辑给出了以下图解:

enter image description here

有关编辑的重要说明:

coord_cartesian(xlim=c(0,1200))将所有图的x轴限制设置为相同

scale_y_continuous(breaks = scales::pretty_breaks(5))使y轴没有小数位

labs(x = "")将x轴添加到最底部

要向彼此的图块添加标签,geom_text()只需更改注释出现在哪个图块(panel=c("B")panel=c("C"))即可。

facet_wrap()内:

scales='free'使得每个绘图都可以重复使用轴

labeller=as_labeller(c([text]))可用于为每个绘图赋予其唯一的y轴名称

strip.position='left'将面板移到左侧(y轴所在的位置)

theme()内:

strip.placement = "outside"将面板标签移动到绘图的外部,从而使它们可以用作y轴标签名称

使用的代码:

ggplot(mapping=aes(x=dist, fill=sex)) +
   geom_histogram(data = cbind(move, panel = "A"), color="black", binwidth=10) +
   geom_vline(data = cbind(move, panel = "A"), aes(xintercept=mean(dist)), 
    color="black", linetype="dashed", size=1)+
   geom_histogram(data = cbind(settle_data, panel = "B"), color="black", binwidth=10) +
   geom_vline(data = cbind(settle_data, panel = "B"), aes(xintercept=mean(dist)), 
    color="black", linetype="dashed", size=1)+
   geom_histogram(data = cbind(recruit_data, panel = "C"), color="black", binwidth=10) +
   geom_vline(data = cbind(recruit_data, panel = "C"), aes(xintercept=mean(dist)), 
    color="black", linetype="dashed", size=1)+
    coord_cartesian(xlim=c(0,1200))+ 
    scale_fill_manual(values=c("gray97", "gray47"),
            name = "Sex",   
            breaks=c("F","M"),
            labels=c("Female","Male")) +
    theme_bw() +
    theme(legend.position = c(0.95,0.93), #move legend into plot
        axis.line = element_line(colour = "black"),
        axis.text=element_text(size = 20), #changes size of axes #s
        axis.title=element_text(size=20), #changes size of axes labels
        plot.caption = element_text(hjust = 0, vjust = 2.12),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        text = element_text(size = 20)) +
    facet_wrap(~ panel, ncol = 1, scales='free', labeller=as_labeller(c(A = "Number of fixes", B = "Number of individuals", C = "Number of individuals") ), strip.position="left") +
    scale_x_continuous(breaks = seq(0,1250,by = 200)) +  
    scale_y_continuous(breaks = scales::pretty_breaks(5))+  
    theme(
    strip.placement = "outside",
    strip.background = element_blank(),
        axis.title.y = element_blank(),
        strip.text = element_text(size = 19))+
    labs(x = "Distance recruited from natal nest (m)")+
 geom_text(data=data.frame(x=600, y=12, 
        label="Distance travelled from natal nest (m)", panel=c("B")), 
               aes(x,y,label=label), inherit.aes=FALSE, size = 7) +
 geom_text(data=data.frame(x=600, y=6, 
        label="Distance settled from natal nest (m)", panel=c("C")), 
               aes(x,y,label=label), inherit.aes=FALSE, size = 7)