Cómo crear un tema en highcharter

Es el segundo día del calendario de adviento 2022 y traemos un nuevo post sobre cómo crear un tema en highcharter, la librería de R para hacer gráficos interactivos. Si quieres saber qué es highcharter, puedes consultar el post anterior!

Aplicar un tema predeterminado en highcharter

A pesar de que el tema por defecto de highcharter ya sea bastante molón, existen otros temas predeterminados que se pueden aplicar directamente con la función hc_add_theme(). En la web de highcharter tenéis al lista completa pero aquí destacamos algunos de ellos.

Añadir el tema Economist en highcharter

library(highcharter)

hchart(iris, "scatter",
  hcaes(x = Sepal.Length, y = Sepal.Width, group = Species)) |>
  hc_title(text = "Iris") |>
  hc_subtitle(text = "Fuente: datasets de R") |>
  hc_credits(enabled = TRUE, text = "http://elartedeldato.com") -> hc

hc |>
  hc_add_theme(hc_theme_economist()) # Añadir un tema

Añadir el tema Financial Times en highcharter

hc |>
  hc_add_theme(hc_theme_ft()) # Añadir un tema

Añadir el tema Google en highcharter

hc |>
  hc_add_theme(hc_theme_google()) # Añadir un tema

Cómo crear un tema personalizado en highcharter

Por otro lado, también es posible crear tu propio tema personalizado. Aquí, hemos creado una para el blog El arte del dato bien contado.

elartedeldato_theme <- hc_theme(
  colors = c("#fffb00","#aedcc0", "#206999", "#ffab44", "#1b314b"),
  chart = list(
    backgroundColor = NULL
  ),
  title = list(
    style = list(
      color = "#000",
      fontFamily = "Bebas Neue",
      fontSize = "30px"
    )
  ),
  subtitle = list(
    style = list(
      color = "#666666",
      fontFamily = "Roboto",
      fontSize = "15px"
    )
  ),
  legend = list(
    itemStyle = list(
      fontFamily = "Roboto",
      color = "black"
    ),
    itemHoverStyle = list(
      color = "gray"
    )
  ),
  xAxis = list(
    gridLineWidth = 1,
    gridLineColor = "#F3F3F3",
    lineColor = "#F3F3F3",
    minorGridLineColor = "#F3F3F3",
    title = list(
      style = list(
        color = "#000",
        fontFamily = "Roboto"
        )
      ),
    labels = list(
      style = list(
        color = "#666666",
        fontFamily = "Roboto"
        )
      )
    ),
 yAxis = list(
    gridLineWidth = 1,
    gridLineColor = "#F3F3F3",
    lineColor = "#F3F3F3",
    minorGridLineColor = "#F3F3F3",
    title = list(
      style = list(
        color = "#000",
        fontFamily = "Roboto"
        )
      ),
    labels = list(
      style = list(
        color = "#666666",
        fontFamily = "Roboto"
        )
      )
    )
)

hc |>
  hc_add_theme(elartedeldato_theme)

Fusionar dos temas

Otra opción disponible es fusionar dos temas con hc_theme_merge(). Por ejemplo, podríamos fusionar uno predeterminado y otro personalizado.

hc |>
  hc_add_theme(
    hc_theme_merge(
      hc_theme_google(),
      hc_theme(
        chart = list(
          backgroundColor = "black"
        ),
        title = list(
          style = list(
            color = "white"
          )
        )
      )
    )
  )
Paula L. Casado
Paula L. Casado
Data Scientist

Científica de datos especializada en visualización de datos.

Relacionado