简体   繁体   中英

Missing x axis labels using facet_wrap_paginate in R ggplot2 and ggforce

First of all, I am very new to both R and stackoverflow so I apologize for any formatting issues. I am trying to plot many individuals separately, but when I use facet_wrap , there are too many plots, so they are all very small and impossible to see. I switched to facet_wrap_paginate and the plots are almost perfect... except the x axis label is missing!

My code (using ggplot2 and 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")

I have tried both with and without scales = "free x" , I also tried fixed. None of these make a difference. This is what it looks like currently with the missing x axis labels:

Facet_wrap_paginate 缺少 x 轴标签

This is what I would like the label to look like, and I was able to do it using a single column with facet_wrap for a single individual: Facet_wrap 带有正确 x 轴标签的单个动物

For reference so you know what kind of data set this is, Baseline.df_long looks like this, but obviously with much more data

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

The issue is that you use scale_x_discrete for continuous or numeric data. Simply switch to scale_x_continuous and the x-axis and labels will show up:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM