Mostrando entradas con la etiqueta scale_x_continuous. Mostrar todas las entradas
Mostrando entradas con la etiqueta scale_x_continuous. Mostrar todas las entradas

2020-03-08

Calendar heatmap by hour and weekday in ggplot2

Problem

We want to create a calendar heatmap using ggplot2.

Solution

In our example we generate random dates, and then group the results by hour of the day and day of the week.

library(tidyverse)
library(lubridate)
# Data
set.seed(2020)
df <-
  data.frame(dates = sample(seq(
    as.POSIXct('2019/01/01', tz = "CET"),
    as.POSIXct('2019/12/31', tz = "CET"),
    by = "sec"
  ), 1000))

# Data manipulation
df %>%
  mutate(days = factor(weekdays(dates, abbreviate = TRUE),
                       levels = rev(
                         c('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat')
                       ))) %>%
  group_by(hours= hour(dates), days) %>%
  summarise(sessions = n()) %>%

# Plot
ggplot(aes(hours, days)) +
  geom_tile(aes(fill = sessions), colour = "white") +
  scale_fill_distiller(palette = "YlGnBu", direction = 1) +
  scale_x_continuous(breaks = 0:23) +
  theme_minimal() +
  theme(
    legend.position = "bottom",
    legend.key.width = unit(2, "cm"),
    panel.grid = element_blank()
  ) +
  coord_equal()

Results

Related posts

References

2019-11-04

How to remove space between axis and plot area in ggplot2

Title

Problem

We want to remove the space between the axis and the plotting area. The space between the axis and the tick marks.

# data
set.seed(0)
the.df <- data.frame( x = rnorm(800, 50, 10), group = rep(letters[1:8], each = 100))

# Original plot
p <- ggplot(the.df) + 
  stat_density(aes(x = x, linetype = group), geom = "line", position = "identity") +
  xlim(10, 90) + ylim(0, 0.06) +
  scale_linetype_manual(values = c("11", "12", "13", "14", "21", "22", "23", "24")) +
  geom_segment(aes(x = 10, y = 0, xend = 90, yend = 0)) +
  geom_segment(aes(x = 10, y = 0, xend = 10, yend = 0.06))

Solution

Two alternatives

  • Scale continuous an expand
  • Instead of xlim(10, 90) + ylim(0, 0.06) we use a continous scale for both axis scale_x_continuous y scale_x_continuous, specifying expand = c(0, 0).

    p <- ggplot(the.df) + 
      stat_density(aes(x = x, linetype = group), geom = "line", position = "identity") +
      scale_linetype_manual(values = c("11", "12", "13", "14", "21", "22", "23", "24")) +
      scale_x_continuous(limits=c(10, 90), expand = c(0, 0)) +
      scale_y_continuous(limits=c(0, 0.06), expand = c(0, 0)) +
      geom_segment(aes(x = 10, y = 0, xend = 90, yend = 0)) +
      geom_segment(aes(x = 10, y = 0, xend = 10, yend = 0.06))
    
  • coord_cartesian
  • We can user coord_cartesian instead of scale_x_continuous y scale_x_continuous.

    p <- ggplot(the.df) + 
      stat_density(aes(x = x, linetype = group), geom = "line", position = "identity") +
      scale_linetype_manual(values = c("11", "12", "13", "14", "21", "22", "23", "24")) +
      geom_segment(aes(x = 10, y = 0, xend = 90, yend = 0)) +
      geom_segment(aes(x = 10, y = 0, xend = 10, yend = 0.06))+
      coord_cartesian(xlim = c(10, 90), ylim = c(0, .06))
    

    Another example

    uniq <- structure(list(year = c(1986L, 1987L, 1991L, 1992L, 1993L, 1994L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 1986L, 1987L, 1991L, 1992L, 1993L, 1994L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 1986L, 1987L, 1991L, 1992L, 1993L, 1994L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L), uniq.loc = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("u.1", "u.2", "u.3"), class = "factor"), uniq.n = c(1, 1, 1, 2, 5, 4, 2, 16, 16, 10, 15, 14, 8, 12, 20, 11, 17, 30, 17, 21, 22, 19, 34, 44, 56, 11, 0, 0, 3, 3, 7, 17, 12, 21, 18, 10, 12, 9, 7, 11, 25, 14, 11, 17, 12, 24, 59, 17, 36, 50, 59, 12, 0, 0, 0, 1, 4, 6, 3, 3, 9, 3, 4, 2, 5, 2, 12, 6, 8, 8, 3, 2, 9, 5, 20, 7, 10, 8), uniq.p = c(100, 100, 25, 33.3, 31.2, 14.8, 11.8, 40, 37.2, 43.5, 48.4, 56, 40, 48, 35.1, 35.5, 47.2, 54.5, 53.1, 44.7, 24.4, 46.3, 37.8, 43.6, 44.8, 35.5, 0, 0, 75, 50, 43.8, 63, 70.6, 52.5, 41.9, 43.5, 38.7, 36, 35, 44, 43.9, 45.2, 30.6, 30.9, 37.5, 51.1, 65.6, 41.5, 40, 49.5, 47.2, 38.7, 0, 0, 0, 16.7, 25, 22.2, 17.6, 7.5, 20.9, 13, 12.9, 8, 25, 8, 21.1, 19.4, 22.2, 14.5, 9.4, 4.3, 10, 12.2, 22.2, 6.9, 8, 25.8)), .Names = c("year", "uniq.loc", "uniq.n", "uniq.p"), class = "data.frame", row.names = c(NA, -78L))
    
  • Original plot
  • ggplot(data = uniq) + 
      geom_area(aes(x = year, y = uniq.p, fill = uniq.loc), stat = "identity", position = "stack") +
      scale_x_continuous(limits=c(1986,2014)) +
      scale_y_continuous(limits=c(0,101)) +
      theme_bw()
    
  • Plot without spaces
  • # Scale_x_continuous y expand = c(0, 0)
    ggplot(data = uniq) + 
      geom_area(aes(x = year, y = uniq.p, fill = uniq.loc), stat = "identity", position = "stack") +
      scale_x_continuous(limits=c(1986,2014), expand = c(0, 0)) +
      scale_y_continuous(limits=c(0,101), expand = c(0, 0)) +
      theme_bw() + theme(panel.grid=element_blank(), panel.border=element_blank())
    
     # coord_cartesian
    ggplot(data = uniq) +  
      geom_area(aes(x = year, y = uniq.p, fill = uniq.loc), stat = "identity", position = "stack") +  
      coord_cartesian(xlim = c(1986,2014), ylim = c(0,101))+
      theme_bw() + theme(panel.grid=element_blank(), panel.border=element_blank())
    

    References

    2018-03-13

    Generar un mapa de calor por días y horas con ggplot2

    Problema

    Queremos crear un mapa de calor, con teselas coloreadas en función de la frecuencia en ggplot2.

    Solución

    library(dplyr)
    library(lubridate)
    library(ggplot2)
    
    # Generamos fechas aleatorias
    set.seed(2015)
    df <-
      data.frame(fechas = sample(seq(
        as.POSIXct('2018/01/01', tz = "CET"),
        as.POSIXct('2018/12/31', tz = "CET"),
        by = "sec"
      ), 1000))
    
    # Manipulamos el data frame 
    # 1. Cambiamos el nombre de los días de la semana
    # 2. Agrupamos por horas y días
    df %>%
      mutate(Días = factor(
        weekdays(fechas),
        levels = rev(
          c(
            "Monday",
            "Tuesday",
            "Wednesday",
            "Thursday",
            "Friday",
            "Saturday",
            "Sunday"
          )
        ),
        labels = c(
          "Domingo",
          "Sábado",
          "Viernes",
          "Jueves",
          "Miércoles",
          "Martes",
          "Lunes"
        )
      )) %>%
      group_by(Horas = hour(fechas), Días) %>%
      summarise(Sesiones = n()) %>%
    # Creamos el gráfico
      ggplot(aes(Horas, Días)) +
      geom_tile(aes(fill = Sesiones), colour = "white") +
      scale_fill_distiller(palette = "YlGnBu", direction = 1) +
      scale_x_continuous(breaks = 0:23) +
      theme_minimal() +
      theme(
        legend.position = "bottom",
        legend.key.width = unit(2, "cm"),
        panel.grid = element_blank()
      ) +
      coord_equal()
    

    Resultado

    Referencias

    2015-08-21

    Cómo eliminar el espacio entre los ejes y el área del gráfico en ggplot2

    Title

    Problema

    Deseamos eliminar el espacio que existe entre los ejes y el área del gráfico (plotting area). Como se aprecia en el gráfico de más abajo, hay un espacio entre los ejes y el área gris que no comienza en las coordenadas (0, 0).

    # Datos
    set.seed(0)
    the.df <- data.frame( x = rnorm(800, 50, 10), group = rep(letters[1:8], each = 100))
    
    
    # Gráfico original
    p <- ggplot(the.df) + 
      stat_density(aes(x = x, linetype = group), geom = "line", position = "identity") +
      xlim(10, 90) + ylim(0, 0.06) +
      scale_linetype_manual(values = c("11", "12", "13", "14", "21", "22", "23", "24")) +
      geom_segment(aes(x = 10, y = 0, xend = 90, yend = 0)) +
      geom_segment(aes(x = 10, y = 0, xend = 10, yend = 0.06))
    

    Solución

    Dos opciones:

  • Scale continuous y expand
  • En lugar de xlim(10, 90) + ylim(0, 0.06) empleamos escalas continuas, scale_x_continuous y scale_x_continuous y dentro incluimos expand = c(0, 0).

    p <- ggplot(the.df) + 
      stat_density(aes(x = x, linetype = group), geom = "line", position = "identity") +
      scale_linetype_manual(values = c("11", "12", "13", "14", "21", "22", "23", "24")) +
      scale_x_continuous(limits=c(10, 90), expand = c(0, 0)) +
      scale_y_continuous(limits=c(0, 0.06), expand = c(0, 0)) +
      geom_segment(aes(x = 10, y = 0, xend = 90, yend = 0)) +
      geom_segment(aes(x = 10, y = 0, xend = 10, yend = 0.06))
    
  • coord_cartesian
  • Usar coord_cartesian en lugar de scale_x_continuous y scale_x_continuous.

    p <- ggplot(the.df) + 
      stat_density(aes(x = x, linetype = group), geom = "line", position = "identity") +
      scale_linetype_manual(values = c("11", "12", "13", "14", "21", "22", "23", "24")) +
      geom_segment(aes(x = 10, y = 0, xend = 90, yend = 0)) +
      geom_segment(aes(x = 10, y = 0, xend = 10, yend = 0.06))+
      coord_cartesian(xlim = c(10, 90), ylim = c(0, .06))
    

    Otro ejemplo

    uniq <- structure(list(year = c(1986L, 1987L, 1991L, 1992L, 1993L, 1994L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 1986L, 1987L, 1991L, 1992L, 1993L, 1994L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 1986L, 1987L, 1991L, 1992L, 1993L, 1994L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L), uniq.loc = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("u.1", "u.2", "u.3"), class = "factor"), uniq.n = c(1, 1, 1, 2, 5, 4, 2, 16, 16, 10, 15, 14, 8, 12, 20, 11, 17, 30, 17, 21, 22, 19, 34, 44, 56, 11, 0, 0, 3, 3, 7, 17, 12, 21, 18, 10, 12, 9, 7, 11, 25, 14, 11, 17, 12, 24, 59, 17, 36, 50, 59, 12, 0, 0, 0, 1, 4, 6, 3, 3, 9, 3, 4, 2, 5, 2, 12, 6, 8, 8, 3, 2, 9, 5, 20, 7, 10, 8), uniq.p = c(100, 100, 25, 33.3, 31.2, 14.8, 11.8, 40, 37.2, 43.5, 48.4, 56, 40, 48, 35.1, 35.5, 47.2, 54.5, 53.1, 44.7, 24.4, 46.3, 37.8, 43.6, 44.8, 35.5, 0, 0, 75, 50, 43.8, 63, 70.6, 52.5, 41.9, 43.5, 38.7, 36, 35, 44, 43.9, 45.2, 30.6, 30.9, 37.5, 51.1, 65.6, 41.5, 40, 49.5, 47.2, 38.7, 0, 0, 0, 16.7, 25, 22.2, 17.6, 7.5, 20.9, 13, 12.9, 8, 25, 8, 21.1, 19.4, 22.2, 14.5, 9.4, 4.3, 10, 12.2, 22.2, 6.9, 8, 25.8)), .Names = c("year", "uniq.loc", "uniq.n", "uniq.p"), class = "data.frame", row.names = c(NA, -78L))
    
  • Gráfico original
  • ggplot(data = uniq) + 
      geom_area(aes(x = year, y = uniq.p, fill = uniq.loc), stat = "identity", position = "stack") +
      scale_x_continuous(limits=c(1986,2014)) +
      scale_y_continuous(limits=c(0,101)) +
      theme_bw()
    
  • Gráfico sin espacios
  • # Scale_x_continuous y expand = c(0, 0)
    ggplot(data = uniq) + 
      geom_area(aes(x = year, y = uniq.p, fill = uniq.loc), stat = "identity", position = "stack") +
      scale_x_continuous(limits=c(1986,2014), expand = c(0, 0)) +
      scale_y_continuous(limits=c(0,101), expand = c(0, 0)) +
      theme_bw() + theme(panel.grid=element_blank(), panel.border=element_blank())
    
     # coord_cartesian
    ggplot(data = uniq) +  
      geom_area(aes(x = year, y = uniq.p, fill = uniq.loc), stat = "identity", position = "stack") +  
      coord_cartesian(xlim = c(1986,2014), ylim = c(0,101))+
      theme_bw() + theme(panel.grid=element_blank(), panel.border=element_blank())
    

    Referencias

    Nube de datos