1

我想在 Highcharter 中创建一个这样的图表以在 R 中使用。

理想情况下,我想做的是一个条形图,其中条形的宽度是事件发生天数的分布。我尝试过使用“pointWidth”,但是,我需要根据事件的长度来改变条形宽度。

这是我要绘制的数据框:

df <- data.frame(event = c("wedding", "party", "concert"), start_date = as.Date(c("2017-10-01", "2017-11-01", "2017-11-20")), end_date = as.Date(c("2017-10-10", "2017-11-18","2017-12-01")), event_days = c(9,17,11), customers = c("400", "150", "3000"))

以下是我通常在 ggplot 中的操作方式:

ggplot(df, aes(start_date, customers, width = event_days)) + geom_bar(stat = "identity")

我正在尝试做的是从技术上使条形的宽度超过事件发生的日子。

这就是我想要实现的 - x 轴成为时间线

Highcharts.chart('container', {

    chart: {
        type: 'variwide'
    },

    title: {
        text: 'Labor Costs in Europe, 2016'
    },

    subtitle: {
        text: 'Source: <a href="http://ec.europa.eu/eurostat/web/' +
            'labour-market/labour-costs/main-tables">eurostat</a>'
    },

    xAxis: {
        type: 'category',
        title: {
            text: 'Column widths are proportional to GDP'
        }
    },

    legend: {
        enabled: false
    },

    series: [{
        name: 'Labor Costs',
        data: [
            ['Norway', 50.2, 335504],
            ['Denmark', 42, 277339],
            ['Belgium', 39.2, 421611],
            ['Sweden', 38, 462057],
            ['France', 35.6, 2228857],
            ['Netherlands', 34.3, 702641],
            ['Finland', 33.2, 215615],
            ['Germany', 33.0, 3144050],
            ['Austria', 32.7, 349344],
            ['Ireland', 30.4, 275567],
            ['Italy', 27.8, 1672438],
            ['United Kingdom', 26.7, 2366911],
            ['Spain', 21.3, 1113851],
            ['Greece', 14.2, 175887],
            ['Portugal', 13.7, 184933],
            ['Czech Republic', 10.2, 176564],
            ['Poland', 8.6, 424269],
            ['Romania', 5.5, 169578]

        ],
        dataLabels: {
            enabled: true,
            format: '€{point.y:.0f}'
        },
        tooltip: {
            pointFormat: 'Labor Costs: <b>€ {point.y}/h</b><br>' +
                'GDP: <b>€ {point.z} million</b><br>'
        },
        colorByPoint: true
    }]

});
#container {
	max-width: 800px;
	min-width: 380px;
	margin: 0 auto;
}
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/variwide.js"></script>

<div id="container"></div>

4

2 回答 2

0

pointRange属性(属于系列)负责为列提供适当的跨度。

我想到了两个简单的解决方案:

1.为每个点创建一个单独的系列,应用pointRange和禁用grouping:http: //jsfiddle.net/BlackLabel/x8c3Ldwc/

2.忽略pointRange并改用面积系列(每列使用单独的系列)。必须应用一些特殊配置(plotOptions在演示中)使其看起来像一个柱形图:http: //jsfiddle.net/BlackLabel/o1o5Ly1y/

于 2018-03-21T13:16:52.983 回答
0

因此,我设法通过以下方式将 Kamil 在 jsfiddle 上提供的内容翻译为 Highcharter。

首先,数据必须采用与我在问题中描述的方式不同的格式。

   df2 <- data.frame(start_date = as.Date(c("2017-10-01", "2017-10-10", "2017-11-01", "2017-11-02", "2017-12-01", "2017-12-15")),
              customers = c(400,400, 200, 200,150,150),
              event = c("wedding","wedding", "party", "party", "concert","concert")) 

其次,图表类型必须设置为“区域”而不是列:

highchart() %>% 
    hc_add_series(df2, "area", hcaes(x = datetime_to_timestamp(start_date), 
    y = customers, group = event)) %>% 
hc_xAxis(type = "datetime",
       title = "",
       showFirstLabel = FALSE,
       showLastLabel = FALSE,
       enableMouseTracking = TRUE) %>% 
hc_plotOptions(area = list(marker = list(
       enabled = FALSE,
       lineWidth = 0,
       states = list(
       hover = list(enabled = FALSE))))) %>% 
hc_tooltip(followPointer = TRUE)
于 2018-03-22T13:23:59.600 回答