ljh2001
ljh2001

Reputation: 463

How to force common x-axis labels/limits among facets in ggplot2?

Here's some example data.

structure(list(Transmitter = c(1675L, 1675L, 1675L, 1675L, 1681L, 
1681L, 1681L, 1681L, 1685L, 1685L, 1685L, 1685L, 1685L, 9782L, 
9782L, 9782L, 24166L, 24166L, 24166L, 24166L, 24184L, 24184L, 
24184L, 24184L), Date = structure(c(17392, 17721, 17722, 17393, 
17734, 17729, 17391, 17717, 17392, 17390, 17391, 17381, 17382, 
18079, 18110, 17762, 17751, 18097, 18090, 18091, 18097, 18068, 
18082, 18088), class = "Date"), Year = c(2017L, 2018L, 2018L, 
2017L, 2018L, 2018L, 2017L, 2018L, 2017L, 2017L, 2017L, 2017L, 
2017L, 2019L, 2019L, 2018L, 2018L, 2019L, 2019L, 2019L, 2019L, 
2019L, 2019L, 2019L), DirectionGroups = structure(c(3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 1L, 1L, 1L, 1L), .Label = c("Both", "Marine", "River"), class = "factor"), 
    `min(Year)` = c(2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
    2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
    2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L
    ), TagYear = c(2017, 2017, 2017, 2017, 2017, 2017, 2017, 
    2017, 2017, 2017, 2017, 2017, 2017, 2018, 2018, 2018, 2018, 
    2018, 2018, 2018, 2018, 2018, 2018, 2018)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -24L), groups = structure(list(
    Transmitter = c(1675L, 1681L, 1685L, 9782L, 24166L, 24184L
    ), `min(Year)` = c(2017L, 2017L, 2017L, 2017L, 2017L, 2017L
    ), .rows = list(1:4, 5:8, 9:13, 14:16, 17:20, 21:24)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE))

Here's the code I'm using to plot.

ggplot(data = AbPlot3, aes(x = Date, y = factor(Transmitter), color = DirectionGroups)) + geom_point()+theme_bw()+ylab("Transmitter")+
  scale_color_manual(values = c('grey70', 'black', 'grey40'), labels = c('Resident', 'External', 'Transient'))+
  theme(axis.text.y = element_blank(), axis.title = element_text(size = 16),
        axis.text.x = element_text(size = 14), legend.text = element_text(size = 14),
        legend.title = element_text(size = 16)) + guides(color = guide_legend(override.aes = list(size = 5)))+facet_grid(.~Year, scales = 'free')

Hopefully you can see when you plot the figure that the three facets have different x-axis limits/labels (the purpose of scales = 'free'). If you were to remove that argument, you would get the same axes limits/labels, but the axis range would cover the entire three years (2017,2018,2019), and there is a lot of white space. I'm trying to do something in between these two options such that the x axis limits/labels are the same, but separated by year with minimal white space in the plots. An x axis range of June 20 to August 20 for each year might work. I've tried manually setting the x axis limits to no avail.

Upvotes: 1

Views: 99

Answers (2)

Dave2e
Dave2e

Reputation: 24079

Since the labels on the x axis is just the month and day, one option is to convert the all the dates to the same year: as.Date(paste0("2020-", format(AbPlot3$Date, "%m-%d")))
Now even with the "fixed" scale all facets with plot equally.

ggplot(data = AbPlot3, aes(x = as.Date(paste0("2020-", format(Date, "%m-%d"))), y = factor(Transmitter), color = DirectionGroups)) + 
   geom_point()+theme_bw()+
   ylab("Transmitter")+
   xlab("Date") +
   scale_color_manual(values = c('grey70', 'black', 'grey40'), labels = c('Resident', 'External', 'Transient'))+
   theme(axis.text.y = element_blank(), axis.title = element_text(size = 16),
         axis.text.x = element_text(size = 12), legend.text = element_text(size = 14),
         legend.title = element_text(size = 16)) + 
   guides(color = guide_legend(override.aes = list(size = 5)))+
   facet_grid(.~Year)

enter image description here

Upvotes: 1

Duck
Duck

Reputation: 39595

Maybe this can help. You can play around scale_x_date(). Hoping this is useful:

ggplot(data = AbPlot3, aes(x = Date, y = factor(Transmitter), color = DirectionGroups)) +
  scale_x_date(date_labels="%d-%b",breaks = '5 days',limits = c(),
               expand = c(0.01,0),
               labels = seq(from=as.Date('2020-06-01'),to=as.Date('2020-08-31'),by='2 days'))+
  geom_point()+theme_bw()+ylab("Transmitter")+
  scale_color_manual(values = c('grey70', 'black', 'grey40'),
                     labels = c('Resident', 'External', 'Transient'))+
  theme(axis.text.y = element_blank(), axis.title = element_text(size = 16),
        axis.text.x = element_text(angle=-90,size = 14,vjust=0.5), legend.text = element_text(size = 14),
        legend.title = element_text(size = 16)) + guides(color = guide_legend(override.aes = list(size = 5)))+facet_grid(.~Year, scales = 'free')

enter image description here

Upvotes: 1

Related Questions