2020-12-23

How to create a time series by 30 minute intervals

Title

Problem

We want to create a time series by 30 minute intervals.

Example

[1] "2017-01-01 00:00:00 UTC"
[2] "2017-01-01 00:30:00 UTC"
[3] "2017-01-01 01:00:00 UTC"
[4] "2017-01-01 01:30:00 UTC"
[5] "2017-01-01 02:00:00 UTC"
[6] "2017-01-01 02:30:00 UTC"

Solution

We use the function seq and specify minutes in the by argument, and pass the time zone "UTC". Type ?seq.POSIXt for more details about the by argument specified as a character string:

A character string, containing one of "sec", "min", "hour", "day", "DSTday", "week", "month", "quarter" or "year". This can optionally be preceded by a (positive or negative) integer and a space, or followed by "s".

seq(as.POSIXct("2017-01-01", tz = "UTC"),
    as.POSIXct("2017-01-02", tz = "UTC"),
    by = "30 min")

References

2020-12-21

How to create a symmetric matrix in R

Title

Problem

We want to create a symmetric matrix based on the following example.

Example

  V1 V2 V3 V4 V5
1  0  2  3  4  5
2  0  0  6  8 10
3  0  0  0 12 15
4  0  0  0  0 20
5  0  0  0  0  0
df <- structure(list(V1 = c(0L, 0L, 0L, 0L, 0L), V2 = c(2L, 0L, 0L, 
0L, 0L), V3 = c(3L, 6L, 0L, 0L, 0L), V4 = c(4L, 8L, 12L, 0L, 
0L), V5 = c(5L, 10L, 15L, 20L, 0L)), .Names = c("V1", "V2", "V3", 
"V4", "V5"), class = "data.frame", row.names = c("1", "2", "3", 
"4", "5"))

Solution

  • Adding the transpose of the matrix.
  • df + t(df)
  • Function lower.tri
  • We assign to the lower triangle of the matrix the transpose of the lower triangle.

    df[lower.tri(df)] <- t(df)[lower.tri(df)]
      V1 V2 V3 V4 V5
    1  0  2  3  4  5
    2  2  0  6  8 10
    3  3  6  0 12 15
    4  4  8 12  0 20
    5  5 10 15 20  0

    Using upper.tri would not create a symmetrical matrix.

    df[lower.tri(df)] <- df[upper.tri(df)]
      V1 V2 V3 V4 V5
    1  0  2  3  4  5
    2  2  0  6  8 10
    3  3  8  0 12 15
    4  6 12 10  0 20
    5  4  5 15 20  0
    

References

2020-12-18

How to change the spacing between the legend and the panel in ggplot2

Title

Problema

We want to increase the spacing between the legend and the panel in ggplot2.

library(ggplot2)
xy <- data.frame(x = 1:10, y = 10:1, type = rep(LETTERS[1:2], each=5))
plot <- ggplot(data = xy) +
        geom_point(aes(x = x, y = y, colo r= type))

Solution

We use the argument legend.box.margin to add 20 points to the left.

plot <- plot + theme(legend.box.margin = margin(0, 0, 0, 20))

References

2020-12-17

Cómo eliminar todos los comentarios en Excel

Title

Problema

Queremos borrar todos los comentarios en Excel. Veremos tres alternativas, en la última usaremos VBA

Solución

  • Opción 1
    1. En la pestaña Revisar clic en Mostrar todos los comentarios
    2. Press Ctrl+E para seleccionar todos los comentarios
    3. Clic en Eliminar
  • Opción 2
    1. Presionamos F5 o Ctrl + I para abrir el cuadro de diálogo Ir a, clic en Especial
    2. Todos los comentarios serán seleccionados
    3. Clic con el botón derecho para abrir el menú contextual y clic en Eliminar comentario
  • Opción 3 - VBA
    1. Borra todos los comentarios de la hoja activa
    2. Sub Borra_Comentarios_de_la_Hoja()
       Cells.ClearComments
      End Sub
      
    3. Borra todos los comentarios del libro de Excel
    4. Sub Borra_todos_los_Comentarios()
          Dim ws As Worksheet
      
          For Each ws In Worksheets
              ws.UsedRange.ClearComments
          Next
      
      End Sub
      

2020-12-14

How to remove all comments in Excel

Title

Problem

We want to delete all comments in Excel. We will see three alternative, the last one using VBA.

Solution

  • Option 1
    1. On the Review tab click on Show all comments
    2. Press Ctrl+A to select all comments
    3. Click on Delete
  • Option 2
    1. Press F5 or Ctrl + G to open Go To dialog box, click on Special and then OK
    2. All comments will be selected
    3. Right click to open context menu and click on Delete Comment
  • Option 3 - VBA
    1. Delete all comments from active sheet
    2. Sub Delete_Comments_from_Sheet()
       Cells.ClearComments
      End Sub
      
    3. Delete all comments from the workbook
    4. Sub Delete_All_Comments()
          Dim ws As Worksheet
      
          For Each ws In Worksheets
              ws.UsedRange.ClearComments
          Next
      
      End Sub
      

2020-12-11

How to modify whiskers of a boxplot in ggplot2?

Title

Problem

We want to modify the colour of the whiskers in a boxplot using ggplot2. In our example, the default colour (black) to red.

library(ggplot2)
p <- ggplot(mtcars, aes(factor(cyl), mpg, fill = factor(am)))
p + geom_boxplot()

Solution

  • Option 1
  • We plot error bars in red and then superimpose the boxplots in black.

    library(ggplot)
    p + stat_boxplot(
      geom = "errorbar",
      colour = "red",
      width = 0,
      position = position_dodge(0.75)
    ) +
      geom_boxplot(coef = 0, outlier.shape = NA)
    
  • Option 2
  • We superimpose two boxplots on top of each other. The first one with red borders and the secong one without whiskers in black.

    p + geom_boxplot(color="red") + 
      geom_boxplot(aes(ymin=..lower.., ymax=..upper..)) 
    

References

2020-12-10

Cómo reducir el espacio entre barras de un diagramas de barra con ggplot2

Title

Problema

Queremos reducir el espacio entre las barras de un diagrama de barras con ggplot2.

Diagrama de barras vertical

Diagrama de barras horizontal

library(ggthemes)
library(ggplot2)
df <- data.frame(x = c("firm", "vendor"), y = c(50, 20))

# Vertical
ggplot(df, aes(x = x, y = y)) + 
  geom_bar(stat = "identity", width = 0.4) + 
  theme_tufte() + 
  labs(x = "", y = "")

# Horizontal
ggplot(df, aes(x = x, y = y)) + 
  geom_bar(stat = "identity", width = 0.4) + 
  theme_tufte() +  coord_flip() +
  labs(x = "", y = "")

Solución

Tan sólo necesitamos añadir theme(aspect.ratio = xx), y luego necesitamos ajustar el ese ratio para conseguir las distancia deseada. Con coord_flip() podemos voltear el gráfico


# Vertical
ggplot(df, aes(x = x, y = y)) + 
  geom_bar(stat = "identity") + 
  theme_tufte() + theme(aspect.ratio = 2) +
  labs(x = "", y = "")

# Horizontal
ggplot(df, aes(x = x, y = y)) + 
  geom_bar(stat = "identity") + 
  theme_tufte() + theme(aspect.ratio = .2) +
  coord_flip() +
  labs(x = "", y = "")

Resultados

Diagrama de barras vertical

Diagrama de barras horizontal

Nube de datos