Christine Blume Christine Blume - 6 days ago 6
R Question

ggplot facet_grid labeller function help needed

I would like to relabel the groups generated by facet_grid, however, all labels do get the same name with my function. I just seem to be blind to the mistake there...

Here is some sample data and my code:

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)))


Thanks!

Answer

The problem is with your labeller function. It's missing a return statement, and ggplot passes the value argument to the labeller function, so when you execute value <- day_count, you're overwriting the argument that ggplot passed the function with an incorrect vector. The following very slight adjustment works:

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)
}

The result looks like this.

Comments