2015-02-06 29 views
2

我有一個數據幀,看起來像:定製圖例

## Data 
df <- data.frame(label = c("A", "B", "C", "D"), 
       color = c("red", "red", "blue", "green"), 
       y = c(10, 11, 12, 13)) 

「A」和「B」是相同的類別的一部分,而「C」和「d」是的一部分單獨的類別。

我想在類別標籤的圖表上添加圖例。

## Highchart without Legend 

## Basic highchart 
h <- rCharts:::Highcharts$new() 
h$chart(type = "column") 

## Highchart data: 
h$series(showInLegend = FALSE, data = rCharts::toJSONArray2(df[, c("label", "color", "y")], json = FALSE, names = TRUE)) 

## Highchart options: 
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 highchart 

enter image description here

我還沒有找到讓任何意義來解決這個問題的方法。

任何幫助,將不勝感激。

回答

1

這就是我解決它的方法。

每個'類別'被分成一個單獨的系列和一個x值,以確定它在圖表上的位置(高圖因爲某些原因需要它,沒有它,圖表堆棧)。

下面是一個示例代碼的工作原理:

## 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 

enter image description here

我希望這可以幫助別人。

3

聲明:我知道這個問題說rCharts,我只是想添加一個使用highcharter包的替代方案。

就像@Optimus說的,問題是添加倍數系列。如果您的系列數量較多(例如顏色),並且想自動添加,您可以使用highcharter,允許您使用hc_add_series_list函數從數據系列列表中添加多個系列。

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() 

這裏list.parse3類似於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" 

最後:

highchart() %>% 
    hc_chart(type = "column") %>% 
    hc_add_series_list(series) %>% 
    hc_xAxis(categories = df$label) %>% 
    hc_plotOptions(column = list(grouping = FALSE)) 

結果將是:

result

+0

我知道你是[highcharter的開發(http://chat.stackoverflow.com/transcript/25312?m = 31229611#31229611),但是,當您使用該軟件包時,請鏈接到[CRAN頁面](https://cran.r-project.org/web/packages/highcharter/index。 HTML)首先,然後到主頁。在meta上有些情況下,用戶將合法答案標記爲「過度推銷」,認爲用戶正在推銷某些軟件包。一般用戶可能不知道你是這個軟件包的開發者。如果我在這方面犯錯了,請下次小心並道歉。 Regards – 2016-06-20 18:24:17

+0

@BhargavRao,完全同意。合理。事實上,部分提供有關其他軟件包的交流方式,使得某些任務更容易(在這種情況下,添加多個數據系列)。我將編輯響應以鏈接CRAN站點,並在答案結尾處移動主頁。謝謝! – jbkunst 2016-06-20 18:28:33

+1

謝謝你,請訪問[R Public](http://chat.stackoverflow。com/rooms/25312/r-public)聊天室,只要你是免費的。這是一個公共聊天,我們可以幫助新用戶學習R.(昨天有一位使用highcharter的新用戶遇到問題,該問題已經整理出來)。 – 2016-06-20 18:34:12