简体   繁体   中英

Using Academic Years as x-Axis Labels

Consider the following example:

library(ggplot2)
library(dplyr)
set.seed(30)

data <- data.frame(group = factor(1:11), 
                   year = c(rep("2013-2014", times = 11), 
                            rep("2014-2015", times = 11), 
                            rep("2015-2016", times = 11), 
                            rep("2016-2017", times = 11)), 
                   value = runif(44),
                   stringsAsFactors = FALSE)

data$plot_year <- as.Date(paste0("01/01/", substr(data$year, start = 1, stop = 4)), 
                     format = "%m/%d/%Y")

ggplot(data, aes(x = plot_year, y = value, color = group)) +
  geom_point() + 
  geom_line(linetype = "dotted") + 
  geom_line(data= data %>% 
                  filter(as.numeric(substr(plot_year, start = 1, stop = 4)) < 2015), 
            aes(x = plot_year, y = value, color = group)) + 
  theme_bw()

在此处输入图片说明

As we can see above, 2013 in the x-axis corresponds with 2013-2014 , 2014 corresponds with 2014-2015 , and so forth.

How can I use these axis labels - ie, 2013-2014 , 2014-2015 , etc. - in place of the current x-axis labels? Every solution that I've found online has said to use as.Date() in some shape or form, but these are academic years and not a fixed date.

You can use the academic year directly as the x-value the plot. You can use comparison operators (like <= ) for subsetting as long as year is a character or ordered factor (but not if year is a non-ordered factor). As a character variable, the ordering will be alphabetic. I prefer an ordered factor so that I can specify the order:

data$year = factor(data$year, levels=sort(unique(data$year)), ordered=TRUE)

ggplot(data, aes(x = year, y = value, color = group, group=group)) +
  geom_point() + 
  geom_line(linetype = "dotted") + 
  geom_line(data= data %>% filter(year <= "2014-2015")) + 
  theme_bw()

在此处输入图片说明

Although I prefer using the ordering of year for subsetting, you can also explicitly specify the years to be included:

ggplot(data, aes(x = year, y = value, color = group, group=group)) +
  geom_point() + 
  geom_line(linetype = "dotted") + 
  geom_line(data= data %>% filter(year %in% c("2013-2014","2014-2015"))) + 
  theme_bw()

You can convert the date to numeric and then use scale_x_continuous with breaks and labels parameter:

library(ggplot2)
library(lubridate)

# calculate the breaks as numeric corresponding to the dates
br <- as.numeric(as.Date(c("2013-01-01", "2014-01-01", "2015-01-01", "2016-01-01")))
# calculate the labels at each break
lb <- c("2013-2014", "2014-2015", "2015-2016", "2016-2017")

ggplot(data, aes(x = as.numeric(plot_year), y = value, color = group)) +
          geom_point() + 
          geom_line(linetype = "dotted") + 
          geom_line(data= data %>% filter(year(plot_year) < 2015), 
                aes(x = as.numeric(plot_year), y = value, color = group)) + 
          theme_bw() + 
          scale_x_continuous(breaks = br, labels = lb) + xlab("year")

在此处输入图片说明

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