简体   繁体   中英

labeling x-axis with dates in geom_line in ggplot2

I want to make a time series plot for weekdays (ie excluding weekends and holidays): If I simply use ggplot with date on the x-axis and y on the y-axis the distance between a Monday and Tuesday will not be the same as the distance between Friday and Monday. There is a daily data set bellow with a date column.

df <- structure(list(PROCEDURE_DATO_DATO = structure(c(17533, 17534, 17535, 17536, 17539, 
                                                       17540, 17541, 17542, 17543, 17546, 
                                                       17547, 17548, 17549, 17550, 17553, 
                                                       17554, 17555, 17556, 17557, 17560), 
                                                     class = "Date"), 
                     Antal_akutte = c(17, 31, 22, 18, 25,
                                      26, 20, 20, 21, 19, 
                                      25, 26, 27, 14, 14, 
                                      39, 21, 23, 20, 13), 
                     Antal_besog = c(42L, 60L, 58L, 58L, 56L, 
                                     61L, 44L, 48L, 47L, 44L, 
                                     58L,60L, 58L, 45L, 38L, 
                                     73L, 49L, 50L, 53L, 40L), 
                     Andel = c(0.404761904761905, 0.516666666666667, 0.379310344827586, 
                               0.310344827586207, 0.446428571428571, 0.426229508196721, 
                               0.454545454545455, 0.416666666666667, 0.446808510638298, 
                               0.431818181818182, 0.431034482758621, 0.433333333333333, 
                               0.46551724137931, 0.311111111111111, 0.368421052631579, 
                               0.534246575342466, 0.428571428571429, 0.46, 0.377358490566038, 0.325)), 
                .Names = c("PROCEDURE_DATO_DATO", "Antal_akutte", "Antal_besog", "Andel"), 
                row.names = c(NA, -20L), class = c("tbl_df", "tbl", "data.frame"))

If I simply make a row_number then I loose the dates on the axis. How can I use the row number, but label the axis with the date column?

df %>% 
  mutate(row = row_number()) %>% 
  ggplot(aes(row, Antal_akutte)) +
  geom_line()

在此处输入图片说明 If I try to create a label with scale_x_continues I get an error:

data %>% 
  mutate(row = row_number(), 
         PROCEDURE_DATO_DATO = as.character(PROCEDURE_DATO_DATO)) %>%
  ggplot(aes(row, Antal_akutte)) +
    geom_line() +
    scale_x_continuous(labels = seq.Date(as.Date("2018-01-02"), as.Date("2018-12-31"), by = "q"))

Error in f(..., self = self) : Breaks and labels are different lengths

You can transform your data to an eXtensible time series (xts) object which makes work with time series pretty easy. Then use autoplot to plot the xts object using ggplot2:

# load libraries
library(ggplot2)
library(xts)

# create an xts object (an xts object is formed by the matrix of observations, ordered by an index of dates - in your case `df$PROCEDURE_DATO_DATO`)
df_xts <- xts(df[,-1], order.by =df$PROCEDURE_DATO_DATO)

# make the plot
autoplot(df_xts, geom="line") 

Let's plot a few observations including the first weekend from January:

> df_xts[3:6,]
           Antal_akutte Antal_besog     Andel
2018-01-04           22          58 0.3793103
2018-01-05           18          58 0.3103448
2018-01-08           25          56 0.4464286
2018-01-09           26          61 0.4262295

I'll use geom = "point" to clearly indicate the missing data points during the weekend.

autoplot(df_xts[3:6,], geom="point") :

在此处输入图片说明

Update: To plot without the weekend dates, your solution should work:

df <- df[3:6,] %>% mutate(row=row_number(), PROCEDURE_DATO_DATO=as.character(PROCEDURE_DATO_DATO))

ggplot(df, aes(row, Antal_akutte)) + geom_line() + scale_x_continuous(breaks = df$row, labels=df$PROCEDURE_DATO_DATO)

在此处输入图片说明

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