2017-01-02 28 views
2

我試圖在使用hchart()函數繪圖時更改barplot酒吧的默認顏色。從文檔我認爲添加一個「顏色」參數應該工作,但下面的代碼生成紫羅蘭色條而不是黑色。直接在highcharters'hcchart函數中更改欄顏色

hchart(head(mtcars), "column", x = rownames(head(mtcars)), y = disp, color = '#000000') 

我可以使用API​​創建圖表達到預期的效果,但是我發現使用hchart()函數,真的很方便,我想知道,如果顏色變化是可以實現的這種方式。

+1

在這種情況下,顏色從預定義的梯度值進行調整。您可以看到使用此代碼'> myc = seq(from = 0,to = 1000) > myd = data.frame(y = myc) > hchart(myd,「column」,y = y,color = y) '。使用'highchart()%>%'的設置選項效果更好 - [更多信息](http://jkunst.com/highcharter/highcharts.html) - 也許你可以使用這種格式? –

+2

正如@nevrome所說,0.4.0v的hchart函數並不那麼聰明。我會推薦使用即將發佈的開發版本並修復一些細節。試試這個例子http://rpubs.com/jbkunst/questions-41429071 – jbkunst

回答

2

據我瞭解,有一個在功能hchart.data.frame和輔助功能,在包裝highcharterget_hc_series_from_dfcolorize的執行錯誤。目前github version以一種非常不同的方式處理這個問題,但我認爲這個版本還沒有準備好生產。所以目前我提出一個很醜陋的解決方案:

變化通過fixInNamespace("colorize", "highcharter")colorize實施從

function (x, colors = c("#440154", "#21908C", "#FDE725")) 
{ 
    nuniques <- length(unique(x)) 
    palcols <- (grDevices::colorRampPalette(colors))(nuniques) 
    if (!is.numeric(x) | nuniques < 10) { 
     y <- as.numeric(as.factor(x)) 
     xcols <- palcols[y] 
    } 
    else { 
     ecum <- ecdf(x) 
     xcols <- palcols[ceiling(nuniques * ecum(x))] 
    } 
    xcols 
} 

function (x, colors = c("#000000", "#21908C", "#FDE725")) 
{ 
    nuniques <- length(unique(x)) 
    palcols <- (grDevices::colorRampPalette(colors))(nuniques) 
    if (!is.numeric(x) | nuniques < 10) { 
     y <- as.numeric(as.factor(x)) 
     xcols <- palcols[y] 
    } 
    else { 
     ecum <- ecdf(x) 
     xcols <- palcols[ceiling(nuniques * ecum(x))] 
    } 
    xcols 
} 
+0

這個解決方案的工作原理確實很醜陋。最後,對我來說最簡單的解決方案是回到API表示法,並執行類似'highchart()%> hc_chart(type =「column」)%>% hc_add_series(data = head(mtcars)$ disp,color ='#000000')%>% hc_xAxis(categories = rownames(head(mtcars)))'這有點長,但適用於當前穩定的0.4.0版本。 – Mikolaj