score:1
And here's how I solved it.
Each 'category' is split into a separate series with an x value to determine its location on the graph (highcharts requires this for some reason. Without it, the graphs stack).
Here's a sample code that works:
## Highchart with Legend
## Remark: simply switching showInLegend to TRUE will not work
df$x <- c(0, 1, 2, 3) # add an index to dataframe (starting from 0)
# and pass it to h$series data
h <- rCharts:::Highcharts$new()
h$chart(type = "column")
h$series(name = "Category 1 (Red)", color = "red", data = rCharts::toJSONArray2(df[df$color == "red", c("label", "color", "x", "y")], json = FALSE, names = TRUE))
h$series(name = "Category 2 (Blue)", color = "blue", data = rCharts::toJSONArray2(df[df$color == "blue", c("label", "color", "x", "y")], json = FALSE, names = TRUE))
h$series(name = "Category 3 (Green)", color = "green", data = rCharts::toJSONArray2(df[df$color == "green", c("label", "color", "x", "y")], json = FALSE, names = TRUE))
h$xAxis(categories = unique(df$label), labels = list(rotation = 0, align = 'center', style = list(fontSize = '12px', fontFamily = 'Verdana, sans-serif')), replace = FALSE)
h$tooltip(formatter = "#! function() {return this.x + ': ' + this.y; } !#")
h$plotOptions(series = list(color = df$color), column = list(grouping = FALSE))
h # display chart
I hope this helps someone else.
score:4
Disclaimer: I know the question says rCharts
, I just want to add an alternative using highcharter package.
Like @Optimus said, the issue is add multiples series. In case you have arbitraty number of series (colors in your example) and want add it automatically you can use highcharter
with allow you to add multiples series from a list of data series with the hc_add_series_list
function.
library(highcharter)
library(dplyr)
df <- data_frame(label = c("A", "B", "C", "D"),
color = c("red", "red", "blue", "green"),
y = c(10, 11, 12, 13),
x = c(1:4)-1)
head(df)
series <- df %>%
group_by(color) %>% # each serie is from one color
do(data = list.parse3(.)) %>%
ungroup() %>%
mutate(name = paste("I'm color", color)) %>%
list.parse3()
Here list.parse3
is similar to toJSONArray2
.
series[[1]]
$color
[1] "blue"
$data
$data[[1]]
$data[[1]]$label
[1] "C"
$data[[1]]$color
[1] "blue"
$data[[1]]$y
[1] 12
$data[[1]]$x
[1] 2
$name
[1] "I'm color blue"
Finally:
highchart() %>%
hc_chart(type = "column") %>%
hc_add_series_list(series) %>%
hc_xAxis(categories = df$label) %>%
hc_plotOptions(column = list(grouping = FALSE))
The result will be:
Source: stackoverflow.com
Related Query
- Highcharts Legend Custom HTML
- Custom Highcharts legend symbol
- Angular Highcharts Custom Legend Label In Area Chart
- Custom legend in rCharts (highcharts)
- Highcharts styling, Legend & custom fonts
- Set custom legend item symbol (or icon) in HIghcharts
- Highcharts legend custom css style / format using only highcharts options
- highcharts - how do I do custom labels on heatmap legend
- Highcharts custom symbol legend
- Highcharts Custom SVG Marker Symbol is Shaped Different in Legend
- Highcharts type flags with custom legend icon
- Display custom tooltip on highcharts legend
- Highcharts custom legend SVG symbol does not fade when you click to turn off series
- highcharts - custom legend width
- Highcharts : remove custom legend tooltip from export
- Highcharts column graph custom legend formatting not showing legend symbols
- Set custom symbol to HighCharts legend
- highcharts - Custom legend - sending the name of the filter to a php file
- Highcharts donut custom legend text before legendSymbol using labelFormatter
- Highcharts Sunburst custom legend and click functionality
- Add symbol to custom legend layout - Highcharts React
- Highcharts columnrange custom legend
- HighCharts Hide Series Name from the Legend
- How to modify highcharts legend item click event?
- Hiding a Highcharts series without using the legend
- Disable-Click on Legend in HighCharts Column Graph
- Highcharts - Change legend index order
- Highcharts legend font sizes
- Highcharts - How to programmatically toggle legend items and determine which items are selected
- Add series total to legend in Highcharts
More Query from same tag
- Plot Bar chart and Line series chart on separate x-axis in Highcharts out of common dataset
- Highcharts two charts on page
- Highcharts dynamically restrict dragging limits
- HighCharts Hours/Minutes
- trouble with highchart and .clone()
- Network chart nodes relation nodes merging -> highcharts
- Create Table using Highcharts GWT
- How can I do to show a div of an element which is "automat" selected?
- Highcharts column chart, disable interpolation when loading from data table
- Highcharts - Passing in Color with JSON
- 3D-Highchart Dynamic Series Add issues
- Highcharts, datetime, xAxis label. How to show all dates on x axis, not only even dates?
- HighChart Line Graph not Showing
- Legend for x-axis categories in highcharts
- How to make the highchart fully occupied with the div that had set?
- passing $_POST["City"] mysql using highcharts
- highcharts hide zoom reset button, call zoom reset programmatically
- How to get access to previous value on formatter of xAxis label , Highcharts?
- Highcharts Activity gauge: series value formatter
- Moving javascript data series to html data attribute breaks Highchart
- Highcharts shared tooltip between charts with multiple series and shared tooltip
- Stacked, grouped column chart with variable width of x points
- my highchart is currently is not working with pie spline chart in updating after rendering chart
- highcharts export server not working out of the box (Java)
- Configuring Data Table from Highcharts Basic Column Chart
- Highmaps downloadXLS() issue with MacOS Safari
- How to show/hide labels customizely in highcharts
- Staged labels for xaxis with zero values
- How to add footer for each series stacked horizontal bars in highcharts?
- Highcharts. Highlight line chart date interval on hover