如何在R中使用Highchart绘制每个国家的多个数值在世界地图上。

huangapple go评论81阅读模式
英文:

How to plot multiple values of each country on worldmap with Highchart for R

问题

I have a data of co2 emissions as below and am pasting a sample of it:

test_data <- structure(
  list(
    name = c("Afghanistan", "Albania", "Algeria"),
    total = c(217.993073, 293.838988, 4862.566823),
    coal = c(57.676473,
             66.78178, 117.54147),
    oil = c(131.577257, 185.838389, 1653.701878),
    gas = c(20.266629, 16.707287, 2154.349422),
    cement = c(2.516676,
               24.375176, 243.216837),
    flaring = c(5.956043, 0.136357, 693.757211),
    other = c(0, 0, 0),
    coal_percent = c(26.46, 22.73, 2.42),
    oil_percent = c(60.36, 63.24, 34.01),
    gas_percent = c(9.3,
                    5.69, 44.3),
    cement_percent = c(1.15, 8.3, 5),
    flaring_percent = c(2.73,
                        0.05, 14.27),
    other_percent = c(0, 0, 0)
  ),
  class = c("grouped_df",
            "tbl_df", "tbl", "data.frame"),
  row.names = c(NA,-3L),
  groups = structure(
    list(
      name = c("Afghanistan", "Albania", "Algeria"),
      .rows = structure(
        list(1L, 2L, 3L),
        ptype = integer(0),
        class = c("vctrs_list_of",
                  "vctrs_vctr", "list")
      )
    ),
    class = c("tbl_df", "tbl", "data.frame"),
    row.names = c(NA,-3L),
    .drop = TRUE
  )
)

I am trying to show each emission's percent for each country on worldmap with highchart for R.

I tried:

highchart() %>%
  hc_add_series_map(worldgeojson, df=all_emissions_percent, value="coal_percent",
                    name="Coal Emissions", joinBy = "name") %>%
  hc_add_series_map(worldgeojson, df=all_emissions_percent, value="oil_percent",
                    name="Oil Emissions", joinBy = "name") %>%
  hc_add_series_map(worldgeojson, df=all_emissions_percent, value="gas_percent",
                    name="Gas Emissions", joinBy = "name") %>%
  hc_add_series_map(worldgeojson, df=all_emissions_percent, value="cement_percent",
                    name="Cement Emissions", joinBy = "name") %>%
  hc_add_series_map(worldgeojson, df=all_emissions_percent, value="flaring_percent",
                    name="Flaring Emissions", joinBy = "name") %>%
  hc_add_series_map(worldgeojson, df=all_emissions_percent, value="other_percent",
                    name="Other Emissions", joinBy = "name") %>%
  hc_colorAxis(stops=stops) %>%
  hc_title(text="Total  Emissions")

The world map is showing the last emission value only, i.e., other emissions. Is there any other way to plot all values for individual countries and show as the below examples:

Example

英文:

I have a data of co2 emissions as below and am pasting a sample of it:

test_data&lt;-structure(
  list(
    name = c(&quot;Afghanistan&quot;, &quot;Albania&quot;, &quot;Algeria&quot;),
    total = c(217.993073, 293.838988, 4862.566823),
    coal = c(57.676473,
             66.78178, 117.54147),
    oil = c(131.577257, 185.838389, 1653.701878),
    gas = c(20.266629, 16.707287, 2154.349422),
    cement = c(2.516676,
               24.375176, 243.216837),
    flaring = c(5.956043, 0.136357, 693.757211),
    other = c(0, 0, 0),
    coal_percent = c(26.46, 22.73, 2.42),
    oil_percent = c(60.36, 63.24, 34.01),
    gas_percent = c(9.3,
                    5.69, 44.3),
    cement_percent = c(1.15, 8.3, 5),
    flaring_percent = c(2.73,
                        0.05, 14.27),
    other_percent = c(0, 0, 0)
  ),
  class = c(&quot;grouped_df&quot;,
            &quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;),
  row.names = c(NA,-3L),
  groups = structure(
    list(
      name = c(&quot;Afghanistan&quot;, &quot;Albania&quot;, &quot;Algeria&quot;),
      .rows = structure(
        list(1L, 2L, 3L),
        ptype = integer(0),
        class = c(&quot;vctrs_list_of&quot;,
                  &quot;vctrs_vctr&quot;, &quot;list&quot;)
      )
    ),
    class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;),
    row.names = c(NA,-3L),
    .drop = TRUE
  )
)

I am trying to show each emission's percent for each country on worldmap with highchart for R.

I tried:

highchart() %&gt;% 
  hc_add_series_map(worldgeojson, df=all_emissions_percent, value=&quot;coal_percent&quot;,
                    name=&quot;Coal Emissions&quot;, joinBy = &quot;name&quot;) %&gt;% 
  hc_add_series_map(worldgeojson, df=all_emissions_percent, value=&quot;oil_percent&quot;,
                    name=&quot;Oil Emissions&quot;, joinBy = &quot;name&quot;) %&gt;% 
  hc_add_series_map(worldgeojson, df=all_emissions_percent, value=&quot;gas_percent&quot;,
                    name=&quot;Gas Emissions&quot;, joinBy = &quot;name&quot;) %&gt;% 
  hc_add_series_map(worldgeojson, df=all_emissions_percent, value=&quot;cement_percent&quot;,
                    name=&quot;Cement Emissions&quot;, joinBy = &quot;name&quot;) %&gt;% 
  hc_add_series_map(worldgeojson, df=all_emissions_percent, value=&quot;flaring_percent&quot;,
                    name=&quot;Flaring Emissions&quot;, joinBy = &quot;name&quot;) %&gt;% 
  hc_add_series_map(worldgeojson, df=all_emissions_percent, value=&quot;other_percent&quot;,
                    name=&quot;Other Emissions&quot;, joinBy = &quot;name&quot;) %&gt;% 
  hc_colorAxis(stops=stops) %&gt;% 
  hc_title(text=&quot;Total  Emissions&quot;)

The world map is showing the last emission value only i.e., other emissions. Is there any other way to plot all values for individual countries and show as the below examples:

Example

答案1

得分: 0

我自己弄清楚了。首先,我需要创建一个新列,其中所有百分比都用换行符'<br>'分隔:

test_data <- test_data %>%
  mutate(percentages = paste0("Coal: ", round(coal_percent, 2), "%", "<br>",
                              "Oil: ", round(oil_percent, 2), "%", "<br>",
                              "Gas: ", round(gas_percent, 2), "%", "<br>",
                              "Cement: ", round(cement_percent, 2), "%", "<br>",
                              "Flaring: ", round(flaring_percent, 2), "%", "<br>",
                              "Other: ", round(other_percent, 2), "%"))

然后使用highcharts在世界地图上绘制这个新列:

highchart() %>%
  hc_add_series_map(worldgeojson, df = test_data, value = "percentages", 
                    name="All Emissions Percentage",joinBy = "name") %>%
  hc_colorAxis(dataClasses = list(
    list(from = 0, to = 5, color = "#74c476"),
    list(from = 5, to = 10, color = "#1e2761"),
    list(from = 10, to = 20, color = "#fdae6b"),
    list(from = 20, to = 30, color = "#7a2048"),
    list(from = 30, to = 40, color = "#e6550d"),
    list(from = 40, to = 50, color = "#a63603"),
    list(from = 50, to = 100, color = "#FF0000")
  )) %>%
  hc_title(text = "All Emissions") %>%
  hc_tooltip(
    useHTML = TRUE,
    formatter = JS(
      "function() {
        var s = '<b><span style=\"font-size:14px;font-weight:bold;text-decoration:underline;\">' + this.point.name + '</span></b><br/>';
        var values = this.point.value.split('<br>');
        $.each(values, function(i, value) {
          var color;
          var percent = value.replace(/[^0-9.]/g, '');
          if (percent <= 5) {
            color = '#74c476';
          } else if (percent <= 10) {
            color = '#1e2761';
          } else if (percent <= 20) {
            color = '#fdae6b';
          } else if (percent <= 30) {
            color = '#7a2048';
          } else if (percent <= 40) {
            color = '#e6550d';
          } else if (percent <= 50) {
            color = '#a63603';
          } else {
            color = '#FF0000';
          }
          s += '<span style=\"color: ' + color + '; font-weight: bold;\">' + value + '</span><br>';
        });
        return s;
      }"
    )
  )

请注意,color_axis和hc_tooltip formatter是完全可选的。我已经自定义了它们以获得美观的外观。

英文:

I figured it out myself. First I need to create a new column with all percentages separated by line break '<br>

test_data &lt;- test_data %&gt;%
  mutate(percentages = paste0(&quot;Coal: &quot;, round(coal_percent, 2), &quot;%&quot;, &quot;&lt;br&gt;&quot;,
                              &quot;Oil: &quot;, round(oil_percent, 2), &quot;%&quot;, &quot;&lt;br&gt;&quot;,
                              &quot;Gas: &quot;, round(gas_percent, 2), &quot;%&quot;, &quot;&lt;br&gt;&quot;,
                              &quot;Cement: &quot;, round(cement_percent, 2), &quot;%&quot;, &quot;&lt;br&gt;&quot;,
                              &quot;Flaring: &quot;, round(flaring_percent, 2), &quot;%&quot;, &quot;&lt;br&gt;&quot;,
                              &quot;Other: &quot;, round(other_percent, 2), &quot;%&quot;))

Then plot this new column on world map using highcharts

highchart() %&gt;%
  hc_add_series_map(worldgeojson, df = test_data, value = &quot;percentages&quot;, 
                    name=&quot;All Emissions Percentage&quot;,joinBy = &quot;name&quot;) %&gt;%
  hc_colorAxis(dataClasses = list(
    list(from = 0, to = 5, color = &quot;#74c476&quot;),
    list(from = 5, to = 10, color = &quot;#1e2761&quot;),
    list(from = 10, to = 20, color = &quot;#fdae6b&quot;),
    list(from = 20, to = 30, color = &quot;#7a2048&quot;),
    list(from = 30, to = 40, color = &quot;#e6550d&quot;),
    list(from = 40, to = 50, color = &quot;#a63603&quot;),
    list(from = 50, to = 100, color = &quot;#FF0000&quot;)
  )) %&gt;%
  hc_title(text = &quot;All Emissions&quot;) %&gt;%
  hc_tooltip(
    useHTML = TRUE,
    formatter = JS(
      &quot;function() {
        var s = &#39;&lt;b&gt;&lt;span style=\&quot;font-size:14px;font-weight:bold;text-decoration:underline;\&quot;&gt;&#39; + this.point.name + &#39;&lt;/span&gt;&lt;/b&gt;&lt;br/&gt;&#39;;
        var values = this.point.value.split(&#39;&lt;br&gt;&#39;);
        $.each(values, function(i, value) {
          var color;
          var percent = value.replace(/[^0-9.]/g, &#39;&#39;);
          if (percent &lt;= 5) {
            color = &#39;#74c476&#39;;
          } else if (percent &lt;= 10) {
            color = &#39;#1e2761&#39;;
          } else if (percent &lt;= 20) {
            color = &#39;#fdae6b&#39;;
          } else if (percent &lt;= 30) {
            color = &#39;#7a2048&#39;;
          } else if (percent &lt;= 40) {
            color = &#39;#e6550d&#39;;
          } else if (percent &lt;= 50) {
            color = &#39;#a63603&#39;;
          } else {
            color = &#39;#FF0000&#39;;
          }
          s += &#39;&lt;span style=\&quot;color: &#39; + color + &#39;; font-weight: bold;\&quot;&gt;&#39; + value + &#39;&lt;/span&gt;&lt;br/&gt;&#39;;
        });
        return s;
      }&quot;
    )
  )

Note that the color_axis and hc_tooltip formatter are completely optional. I have customized it for aesthetic appearance:

如何在R中使用Highchart绘制每个国家的多个数值在世界地图上。

huangapple
  • 本文由 发表于 2023年2月24日 07:07:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551213.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定