在 R ggplot2 和 ggforce 中使用 facet_wrap_paginate 缺少 x 轴标签
Missing x axis labels using facet_wrap_paginate in R ggplot2 and ggforce
首先,我对 R 和 Whosebug 都很陌生,所以对于任何格式问题我深表歉意。我试图分别绘制许多个体,但是当我使用 facet_wrap
时,绘图太多,所以它们都非常小,无法看到。我切换到 facet_wrap_paginate
并且绘图几乎完美...除了缺少 x 轴标签!
我的代码(使用 ggplot2 和 ggforce):
library(ggplot2)
library(ggforce)
lb <- c("8AM", "2PM", "8PM", "2AM", "8AM")
L <- 1440
xat <- c(1, L/4, L/2, 3*L/4, L)
Baseline1 <- ggplot(Baseline.df_long,
aes(x = Minute, y = value, color = key, group = key)) +
facet_wrap_paginate(~ key, ncol = 3, nrow = 3, page = 1, scales = "free_x") +
geom_bar(stat = "identity") +
scale_y_continuous(breaks=seq(0, 2500, 500), limits =c(0,2500)) +
scale_x_discrete(name="Time", breaks=c(xat), labels=c(lb)) +
labs(title ="Piglet Activity", y = "Activity") +
theme_bw(base_size = 14) +
theme(legend.position = "none")
有和没有scales = "free x"
我都试过了,我也试过修复。 None 其中有所作为。这是当前缺少 x 轴标签的样子:
这就是我希望标签看起来的样子,我可以使用带有 facet_wrap 的单个列来为单个个体完成此操作:
作为参考,让你知道这是什么类型的数据集,Baseline.df_long
看起来像这样,但显然有更多的数据
structure(list(Minute = 1:50, key = c("Pig_03", "Pig_03", "Pig_03",
"Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03",
"Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03",
"Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03",
"Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03",
"Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03",
"Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03",
"Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03"), value = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 165L, 23L, 0L, 0L,
0L, 0L, 12L, 0L, 0L, 0L, 0L, 0L, 0L, 81L, 0L, 0L, 12L, 12L, 0L,
0L, 47L, 0L, 0L, 12L, 23L, 0L, 0L, 0L, 0L, 0L, 0L, 105L, 70L,
47L, 0L, 0L, 0L, 0L)), row.names = c(NA, -50L), class = "data.frame")
问题是您将 scale_x_discrete
用于连续数据或数字数据。只需切换到 scale_x_continuous
,x-axis 和标签就会显示:
lb <- c("8AM", "2PM", "8PM", "2AM", "8AM")
L <- 1440
xat <- c(1, L/4, L/2, 3*L/4, L)
library(ggplot2)
library(ggforce)
ggplot(Baseline.df_long,
aes(x = Minute, y = value, color = key, group = key)) +
facet_wrap_paginate(~ key, page = 1, scales = "free_x") +
geom_bar(stat = "identity") +
scale_y_continuous(breaks=seq(0, 2500, 500), limits =c(0,2500)) +
scale_x_continuous(name="Time", breaks = xat, labels = lb) +
labs(title ="Piglet Activity", y = "Activity") +
theme_bw(base_size = 14) +
theme(legend.position = "none")
首先,我对 R 和 Whosebug 都很陌生,所以对于任何格式问题我深表歉意。我试图分别绘制许多个体,但是当我使用 facet_wrap
时,绘图太多,所以它们都非常小,无法看到。我切换到 facet_wrap_paginate
并且绘图几乎完美...除了缺少 x 轴标签!
我的代码(使用 ggplot2 和 ggforce):
library(ggplot2)
library(ggforce)
lb <- c("8AM", "2PM", "8PM", "2AM", "8AM")
L <- 1440
xat <- c(1, L/4, L/2, 3*L/4, L)
Baseline1 <- ggplot(Baseline.df_long,
aes(x = Minute, y = value, color = key, group = key)) +
facet_wrap_paginate(~ key, ncol = 3, nrow = 3, page = 1, scales = "free_x") +
geom_bar(stat = "identity") +
scale_y_continuous(breaks=seq(0, 2500, 500), limits =c(0,2500)) +
scale_x_discrete(name="Time", breaks=c(xat), labels=c(lb)) +
labs(title ="Piglet Activity", y = "Activity") +
theme_bw(base_size = 14) +
theme(legend.position = "none")
有和没有scales = "free x"
我都试过了,我也试过修复。 None 其中有所作为。这是当前缺少 x 轴标签的样子:
这就是我希望标签看起来的样子,我可以使用带有 facet_wrap 的单个列来为单个个体完成此操作:
作为参考,让你知道这是什么类型的数据集,Baseline.df_long
看起来像这样,但显然有更多的数据
structure(list(Minute = 1:50, key = c("Pig_03", "Pig_03", "Pig_03",
"Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03",
"Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03",
"Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03",
"Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03",
"Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03",
"Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03",
"Pig_03", "Pig_03", "Pig_03", "Pig_03", "Pig_03"), value = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 165L, 23L, 0L, 0L,
0L, 0L, 12L, 0L, 0L, 0L, 0L, 0L, 0L, 81L, 0L, 0L, 12L, 12L, 0L,
0L, 47L, 0L, 0L, 12L, 23L, 0L, 0L, 0L, 0L, 0L, 0L, 105L, 70L,
47L, 0L, 0L, 0L, 0L)), row.names = c(NA, -50L), class = "data.frame")
问题是您将 scale_x_discrete
用于连续数据或数字数据。只需切换到 scale_x_continuous
,x-axis 和标签就会显示:
lb <- c("8AM", "2PM", "8PM", "2AM", "8AM")
L <- 1440
xat <- c(1, L/4, L/2, 3*L/4, L)
library(ggplot2)
library(ggforce)
ggplot(Baseline.df_long,
aes(x = Minute, y = value, color = key, group = key)) +
facet_wrap_paginate(~ key, page = 1, scales = "free_x") +
geom_bar(stat = "identity") +
scale_y_continuous(breaks=seq(0, 2500, 500), limits =c(0,2500)) +
scale_x_continuous(name="Time", breaks = xat, labels = lb) +
labs(title ="Piglet Activity", y = "Activity") +
theme_bw(base_size = 14) +
theme(legend.position = "none")