rCharts HighCharts how to color zones

888 views Asked by At

I am running into a problem where I cannot color different sections of a graph I am working on. I want to change the color based on the threshold on the y axis. It seems easy from looking at the documentation from the high charts javascript, but it fails when I try to translate it into R to use rCharts. Here is what I have, with an example data frame called final.

library(rCharts)
final <- data.frame(x = c(1,2,3,4,5), y = c(15,17,20,21,22), radius = rep(3,5))
temp <- apply(final[,c("x","y","radius"])], 1, as.list)
a <- Highcharts$new()
a$series(animation = FALSE, name = "placeholder", data = temp, type = "scatter", 
lineWidth = 1, zoneAxis = "y", zones = list(list(value = 20, 
                                                 color = "rgba(223, 83, 83, 0.5)"),
                                            list(color = "rgba(50, 205, 50, 0.5)")))

This does not change the plot from the default color, so I'm very confused and would greatly appreciate any help or possibly another solution. Thanks!

This is modeled off of the documentation for zones here: http://www.highcharts.com/docs/chart-concepts/series#4aaN

1

There are 1 answers

2
SabDeM On BEST ANSWER

Here are the points I do not understand: This code has a ] inside the c() without a sense;

temp <- apply(final[,c("x","y","radius"])], 1, as.list)

Your intent I guess, was to select all columns, which can be achieved with a simple:

temp <- apply(final[ , ], 1, as.list)

But this code also seems to be strange for me because the same results can be obtained with just:

temp <- apply(final, 1, as.list)
temp
[[1]]
[[1]]$x
[1] 1

[[1]]$y
[1] 15

[[1]]$radius
[1] 3


[[2]]
[[2]]$x
[1] 2

[[2]]$y
[1] 17

[[2]]$radius
[1] 3


[[3]]
[[3]]$x
[1] 3

[[3]]$y
[1] 20

[[3]]$radius
[1] 3


[[4]]
[[4]]$x
[1] 4

[[4]]$y
[1] 21

[[4]]$radius
[1] 3


[[5]]
[[5]]$x
[1] 5

[[5]]$y
[1] 22

[[5]]$radius
[1] 3

If of course you wanted that.

Edit: Color changed (another approach)

I changed the color even though I have to admit I achieved this solution due to my ignorance of which package the rgba functions belongs to. I change the rgba to rgb, I have unquoted it (are you sure it should be quoted?) and I set the max = 255 to the rgb. Here is the code.

a$series( animation = FALSE, name = "placeholder",
            data = temp, type = "scatter",
            lineWidth = 1, zoneAxis = "y",
            zones = list(list(value = 20,
                        color = rgb(223, 83, 83, 0.5, max = 255)),
                      list(color = rgb(50, 205, 50, 0.5, max = 255))
                      )
            )