Skip to content

Commit

Permalink
updating #302 script
Browse files Browse the repository at this point in the history
  • Loading branch information
jbkunst committed Aug 23, 2017
1 parent 7d99e8a commit 55c34ad
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 30 deletions.
105 changes: 75 additions & 30 deletions dev/issues/issue-302.R
Original file line number Diff line number Diff line change
@@ -1,54 +1,99 @@
library(dplyr)
library(tidyr)
#+include=FALSE
knitr::opts_chunk$set(message = FALSE, warning = FALSE)
#+
library(tidyverse)
library(highcharter)

# example 1 ---------------------------------------------------------------
data(citytemp)
citytemp$int <- seq_len(12L)

citytemp2 <- citytemp %>%
select(-new_york, -london) %>%
gather(city, temp, berlin, tokyo)

highchart() %>%
hc_xAxis(categories = citytemp$int) %>%
hc_add_series(name = "Berlin", data = citytemp$berlin) %>%
hc_add_series(name = "Tokyo", data = citytemp$tokyo)
head(citytemp2)

highchart() %>%
#' # Dataframe with x as integer
hc1 <- highchart() %>%
hc_xAxis(categories = citytemp$int) %>%
hc_add_series(citytemp2, "line", hcaes(x = int, y = temp, group = city))
hc_add_series(citytemp2, "line", hcaes(x = int, y = temp, group = city))
hc1

highchart() %>%
#' This look weird! Why? Because you are adding a categories and a using numeric
#' value in the data.
#'
hc1$x$hc_opts$xAxis

#' Look the data
hc1$x$hc_opts$series[[1]]$data %>% head(1)

#' `int` is copied to the `x` value so x is numeric! so due javascript is 0-based
#' the number 1 match the second value in the categories. You'll want avoid this mix.
#'
#' # Dataframe with x as character
#'
hc2 <- highchart() %>%
hc_xAxis(categories = citytemp$int) %>%
hc_add_series(citytemp2, "line", hcaes(x = as.character(int), y = temp, group = city))
hc2

#' This look as expected. This is the same result as:
hchart(citytemp2, "line", hcaes(x = as.character(int), y = temp, group = city)) %>%
hc_xAxis(title = list(text = NULL))
#' Internally `hchart` with categorical `x` add `hc_xAxis(categories = )`

hchart(citytemp2, "line", hcaes(x = as.character(int), y = temp, group = city))
hchart(citytemp2, "line", hcaes(x = as.integer(int), y = temp, group = city))
hc2$x$hc_opts$xAxis

# example 2 ---------------------------------------------------------------
year <- seq.int(from = 1750, to = 2050, by = 50)
Asia <- c(502, 635, 809, 947, 1402, 3634, 5268)
Africa <- c(106, 107, 111, 133, 221, 767, 1766)
Europe <- c(163, 203, 276, 408, 547, 729, 628)
America <- c(18, 31, 54, 156, 339, 818, 1201)
Oceania <- c(2, 2, 2, 6, 13, 30, 46)
df_area <- data.frame(year,Asia,Africa,Europe,America,Oceania) %>% gather(continent, pop, -year)
#' Look the data, now `x` doesn't exists because `hc_add_series.data.frame`
#' change the `as.character(int)` value to `name` to match the categories
hc2$x$hc_opts$series[[1]]$data %>% head(1)

hchart(df_area, "area", hcaes(x = year, y = pop, group = continent))
#' # Vector with x as integer
#'
hc3 <- highchart() %>%
hc_xAxis(categories = citytemp$int) %>%
hc_add_series(name = "berlin", data = citytemp$berlin) %>%
hc_add_series(name = "tokyo", data = citytemp$tokyo)
hc3

#' The result is expected due the data don't have structure so the value
#' are showed in order.
hc3$x$hc_opts$xAxis
hc3$x$hc_opts$series[[1]]

#' The next chart start at 0 due javascript is 0-based.
highchart() %>%
hc_add_series(name = "berlin", data = citytemp$berlin)

#' But you can do:
highchart() %>%
hc_add_series(df_area, "area", hcaes(x = year, y = pop, group = continent))
hc_add_series(name = "berlin", data = citytemp$berlin, pointStart = 1)

hchart(df_area, "area",
hcaes(x = year, y = pop, group = continent)) %>%
hc_plotOptions(area = list(marker = list(enabled = FALSE)))
#' Which is same as `hc5` (see below).
#'
#' # hchart with x as character
#'
hc4 <- hchart(citytemp2, "line", hcaes(x = as.character(int), y = temp, group = city))
hc4
hc4$x$hc_opts$xAxis
hc4$x$hc_opts$series[[1]]$data %>% head(1)

#' As we said, same as `hc2`
#'
#' # hchart with x as integer
#'
hc5 <- hchart(citytemp2, "line", hcaes(x = int, y = temp, group = city))
hc5

#' This is the expected result due `x` is integer so you don't need to use
#' `hc_xAxis(categories = )`
hc5$x$hc_opts$xAxis
hc5$x$hc_opts$series[[1]]$data %>% head(1)

#' As we said, same as:
highchart() %>%
hc_add_series(df_area, "area",
hcaes(x = year, y = pop, group = continent)) %>%
hc_plotOptions(area = list(marker = list(enabled = FALSE)))
hc_add_series(name = "berlin", data = citytemp$berlin, pointStart = 1)

highchart() %>%
hc_add_series(citytemp2, "line", hcaes(x = int, y = temp, group = city))

# session -----------------------------------------------------------------
devtools::session_info()
2 changes: 2 additions & 0 deletions dev/treemap2.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ title: "Demostration of New Function `hctreemap2`"
author: "Alex Hayes"
date: "June 18, 2017"
output: html_document
editor_options:
chunk_output_type: console
---

```{r setup, include = FALSE}
Expand Down

0 comments on commit 55c34ad

Please sign in to comment.