Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shortcut for density plots #106

Closed
nuno-agostinho opened this issue May 20, 2016 · 2 comments
Closed

Add shortcut for density plots #106

nuno-agostinho opened this issue May 20, 2016 · 2 comments

Comments

@nuno-agostinho
Copy link
Contributor

nuno-agostinho commented May 20, 2016

Hey there @jbkunst!

I created the following function to get density plots from density objects:

hchart.density <- function (..., area = FALSE) {
    type <- ifelse(area, "area", "spline")
    args <- list(...)
    data <- lapply(args, function(i) {
        x <- i$x
        y <- i$y
        list.parse3(data.frame(cbind(x = x, y = y)))
    })
    hc <- highchart()
    for (d in data)
        hc <- hc %>% hc_add_series(data = d, type = type)
    return(hc)
}

Which would make it simple to plot multiple density objects.

d1 <- density(mtcars$mpg[1:32/2])
d2 <- density(mtcars$mpg[32/2:32])
d3 <- density(mtcars$mpg[5:(16+5)])
hchart(d1, d2, d3, area = TRUE)

However, I thought the function does not share the same philosophy you apply to the other hchart methods. In that sense, I tried to make it more like one of your shortcuts:

hc_add_series_density <- function (hc, x, area = FALSE, ...) {
    type <- ifelse(area, "area", "spline")
    data <- list.parse3(data.frame(cbind(x = x$x, y = x$y)))
    return(hc %>% hc_add_series(data = data, type = type))
}

And the use would be more like this:

highchart() %>%
    hc_add_series_density(d1, area = TRUE) %>%
    hc_add_series_density(d2, area = TRUE) %>%
    hc_add_series_density(d3, area = TRUE) 

But after coming with this, I thought this was maybe too simple to add to your package. So what do you think? Should I add the code of hc_add_series_density in a pull request?

@jbkunst
Copy link
Owner

jbkunst commented May 20, 2016

Hi @nuno-agostinho ,

Really nice addition. It's look very nice.

I think we can do:

  • The helper: hc_add_series_density here. Can we add in this function the lineif(is.numeric(x)) x <- density(x)?. I think it would be nice give a numeric object which I want to show as a density.
  • Then the generic function hchart.density <- function (object,..., area = FALSE) { hc_add_series_density(highcharts(), object, ...)} here
  • And finally the shortcut hcdensity <- function(x, ...) { hchart.density(x, ...)} here due highcharts version for common plots #99

So we can do:

x <- rnorm(1000)
y <- rexp(1000)

hcdensity(x) %>%
  hc_add_series_density(y)

# and
hcdensity(density(x)) %>% # not the correct usage but its possible.
  hc_add_series_density(density(y))

# or
hchart(density(x)) %>%
  hc_add_series_density(density(y))

What do you think?

Thanks :D

@nuno-agostinho
Copy link
Contributor Author

I think your tips are great! 👍 I've included them in the pull request #109.

I'll close this issue now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants