我想重新标记由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)))
谢谢!
答案 0 :(得分:1)
问题在于您的贴标机功能。它缺少一个return语句,并且ggplot将value
参数传递给labeller函数,因此当你执行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。
答案 1 :(得分:0)
使用较新版本的ggplot2
,首先必须执行一个命名向量,其名称是facetting变量的原始值,其值是您想要的标签。
对于您的示例,您可以执行
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的Cookbook for R graphs中的修改构面标签文字部分。