2016-08-10 7 views
1

パイと棒グラフを含むハイハターオブジェクトを使用しましたが、ラベル(パーセント)はマウスだけを表示します。以下のコードを参照してください:r-ハイハターオブジェクトに常にラベルを表示する方法

library(highcharter) 
data("favorite_bars") 
data("favorite_pies") 

highchart() %>% 
hc_title(text = "This is a bar graph describing my favorite pies 
     including a pie chart describing my favorite bars") %>% 
hc_subtitle(text = "In percentage of tastiness and awesomeness") %>% 
hc_add_series_labels_values(favorite_pies$pie, favorite_pies$percent, name = "Pie", 
          colorByPoint = TRUE, type = "column") %>% 
hc_add_series_labels_values(favorite_bars$bar, favorite_bars$percent, 
          colors = substr(terrain.colors(5), 0 , 7), type = "pie", 
          name = "Bar", colorByPoint = TRUE, center = c('35%', '10%'), 
          size = 100, dataLabels = list(enabled = FALSE)) %>% 
hc_yAxis(title = list(text = "percentage of tastiness"), 
     labels = list(format = "{value}%"), max = 100) %>% 
hc_xAxis(categories = favorite_pies$pie) %>% 
hc_legend(enabled = FALSE) %>% 
hc_tooltip(pointFormat = "{point.y}%") %>% 
hc_credits(enabled = TRUE, text = "Source: HIMYM", 
     href = "https://www.youtube.com/watch?v=f_J8QU1m0Ng", 
     style = list(fontSize = "12px")) 

そして、マウスの移動ではなく、常にパーセンテージの表示が必要です。あなたがhighcharts websiteにあなたが表示オプションをクリックして貼り付けた円グラフを作成するために使用されるオプションを見ることができます enter image description here enter image description here

答えて

5

:私はhighcharts公式サイトからもらった写真を参照してください。これは、あなたがdataLabelsオプションを有効にしてフォーマッタを提供しなければならないことを意味

plotOptions: { 
      pie: { 
       allowPointSelect: true, 
       cursor: 'pointer', 
       dataLabels: { 
        enabled: true, 
        format: '<b>{point.name}</b>: {point.percentage:.1f} %', 
        style: { 
         color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black' 
        } 
       } 
      } 
     }, 

あなたは、他の人の間で、次のコードが表示されます。あなたが提供するコードでは、実際にdataLabels enabledオプションがFALSEに設定されていることを二hc_add_series_labels_valuesの呼び出しで見ることができます:あなたはTRUEに変更した場合

hc_add_series_labels_values(favorite_bars$bar, favorite_bars$percent, 
          colors = substr(terrain.colors(5), 0 , 7), type = "pie", 
          name = "Bar", colorByPoint = TRUE, center = c('35%', '10%'), 
          size = 100, dataLabels = list(enabled = FALSE)) 

あなたはラベルが表示されます。あなたは数字が同様に表示したい場合は、そのように、リスト内format要素に関数を渡す必要があります:

hc_add_series_labels_values(favorite_bars$bar, favorite_bars$percent, 
           colors = substr(terrain.colors(5), 0 , 7), type = "pie", 
           name = "Bar", colorByPoint = TRUE, center = c('35%', '10%'), 
           size = 100, dataLabels = list(enabled = TRUE, 
                  format = '{point.name}: {point.percentage:.1f} %')) 

あなたがそれらを表示するためにdataLabelsに異なるオプションでプレイする必要がありますただし、棒グラフタイプの場合はHighcharts APIを参照してください。

+0

答えに感謝し、どのようにフォントサイズを調整するか知っていますか?ありがとう。 – Samoth

+2

[Highcharts API](http://api.highcharts.com/highcharts/series%3Cpie%3E.dataLabels.style)には、 'dataLabels'の' style'パラメータがあります。 'style = list(fontSize =" 6px ")のように、これをRの' dataLabels'リストに追加する必要があります。 – Tutuchan

関連する問題