ggplot facet_grid 需要标签功能帮助

ggplot facet_grid labeller function help needed

我想重新标记 facet_grid 生成的组,但是,所有标签都与我的函数同名。我似乎对那里的错误视而不见...

这是一些示例数据和我的代码:

data_hrs <- rnorm(513,20,10)
hours <- nrow(data_hrs)
days <- ceiling(hours/24)
days_hours <- days*24

data_hrs[days_hours] <- NA ## padding with NA to reach full no of days
hours <- length(data_hrs)
hours_count <- seq(1:24)
hours_count <- rep(hours_count,days)

count_plot <- seq(1:48) ## 48 values are plotted
count_plot <- rep(count_plot, days/2)

day_count <- NA ## counts the days
for (t in 1:(days/2)){
day_count[((t-1)*48+1):(t*24*2)] <- rep(t,24)
}

mf_labeller <- function(variable, value){
value <- day_count
if (variable == "day_count"){
  value[value == 1] <- "Days 1-2"
  value[value == 2] <- "Days 3-4"
  value[value == 3] <- "Days 5-6"
  value[value == 4] <- "Days 7-8"
  value[value == 5] <- "Days 9-10"
  value[value == 6] <- "Days 11-12"
  value[value == 7] <- "Days 13-14"
  value[value == 8] <- "Days 15-16"
  value[value == 9] <- "Days 17-18"
  value[value == 10] <- "Days 19-20"
  value[value == 11] <- "Days 21-22"
  value[value == 12] <- "Days 23-24"
  value[value == 13] <- "Days 25-26"
  value[value == 14] <- "Days 27-28"
  value[value == 15] <- "Days 29-30"
  }
}

df_plot <- data.frame(day_count, hours_count, count_plot, data_hrs)

print(ggplot(df_plot, aes(x=count_plot, y = data_hrs))+ 
      geom_bar(stat="identity", width = 1, position =     position_dodge(width = 0.5))+
      theme_bw()+
      facet_grid(day_count ~ ., labeller = mf_labeller)+
      scale_x_discrete(limits = c(seq(1:48)), breaks = seq(2,48,2))+
      xlab("Hours")+
      ylab("Movement Intensity")+
      ggtitle("Actigraphy Plot (48 hrs)")+
      theme(plot.margin=unit(c(1,18,1,18),"cm"),
            axis.text.x = element_blank(),
            axis.title.x = element_text(face="bold", size=14),
            axis.title.y = element_text(face="bold", size=14, vjust = 1.2),
            plot.title = element_text(face="bold", size=16, vjust = 1)))

谢谢!

问题出在你的标签功能上。它缺少 return 语句,并且 ggplot 将 value 参数传递给标签函数,因此当您执行 value <- day_count 时,您将用不正确的向量覆盖 ggplot 传递函数的参数.以下非常轻微的调整有效:

mf_labeller <- function(variable, value){
  if (variable == "day_count"){
    value[value == 1] <- "Days 1-2"
    value[value == 2] <- "Days 3-4"
    value[value == 3] <- "Days 5-6"
    value[value == 4] <- "Days 7-8"
    value[value == 5] <- "Days 9-10"
    value[value == 6] <- "Days 11-12"
    value[value == 7] <- "Days 13-14"
    value[value == 8] <- "Days 15-16"
    value[value == 9] <- "Days 17-18"
    value[value == 10] <- "Days 19-20"
    value[value == 11] <- "Days 21-22"
    value[value == 12] <- "Days 23-24"
    value[value == 13] <- "Days 25-26"
    value[value == 14] <- "Days 27-28"
    value[value == 15] <- "Days 29-30"
  }
  return(value)
}

结果看起来like this

对于较新版本的 ggplot2,您首先必须做一个命名向量,其名称是分面变量的原始值,其值是您想要的标签。

对于你的例子,你可以这样做

f <- function(n) paste0("Days ", 2*n-1, "-", 2*n)
labels_days <- setNames(sapply(unique(df_plot$day_count), f), unique(df_plot$day_count))

获取这个命名向量:

> labels_days
           1            2            3            4            5            6            7 
  "Days 1-2"   "Days 3-4"   "Days 5-6"   "Days 7-8"  "Days 9-10" "Days 11-12" "Days 13-14" 
           8            9           10           11 
"Days 15-16" "Days 17-18" "Days 19-20" "Days 21-22" 

然后做:

ggplot(df_plot, aes(x=count_plot, y = data_hrs))+ 
  geom_bar(stat="identity", width = 1, position = position_dodge(width = 0.5)) +
  facet_grid(day_count ~ ., labeller = labeller(day_count=labels_days))

请参阅 Facets chapter of the Cookbook for R graphs 中的 修改构面标签文本 部分。