如何在华夫饼图符号上显示国旗?(使用,例如,`ggwaffle`)

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

How to display country flags on waffle chart symbols? (using, e.g,. `ggwaffle`)

问题

我想创建一个华夫饼图,并在方块的位置使用国旗。这是我当前尝试使用ggwaffle的代码:

library(ggplot2)
library(emojifont)
library(ggwaffle)

iris2 <- iris
iris2$Species <- c("🇫🇷", "🇩🇪", "🇪🇸")

waffle_data <- waffle_iron(iris2, aes_d(group = Species))
waffle_data$label = fontawesome('fa-flag')

ggplot(waffle_data, aes(x, y, colour = group)) +
  geom_text(aes(label=label), family='fontawesome-webfont', size=7) +
  coord_equal() +
  scale_colour_waffle() +
  theme_waffle()

如何在华夫饼图符号上显示国旗?(使用,例如,`ggwaffle`)

创建于2023-06-18,使用 reprex v2.0.2

使用ggwaffle是否可能?我也尝试过使用ggflags,但我不知道如何使这两者兼容,因为ggflags会替换散点图中的点。

我也知道Windows因政治原因不提供国旗的Unicode表情符号,因此基本的Unicode无法工作(就像我以前尝试使用waffle包时一样)。所以需要一个包含国旗或其图像的字体。感谢任何帮助。

英文:

I would like to create a waffle chart and use country flags in place of squares. Here is my current attempt using ggwaffle:

library(ggplot2)
library(emojifont)
library(ggwaffle)

iris2 &lt;- iris
iris2$Species &lt;- c(&quot;&#127482;&#127480;&quot;, &quot;&#127464;&#127462;&quot;, &quot;&#127468;&#127463;&quot;)

waffle_data &lt;- waffle_iron(iris2, aes_d(group = Species))
waffle_data$label = fontawesome(&#39;fa-flag&#39;)

ggplot(waffle_data, aes(x, y, colour = group)) +
  geom_text(aes(label=label), family=&#39;fontawesome-webfont&#39;, size=7) +
  coord_equal() +
  scale_colour_waffle() +
  theme_waffle()

如何在华夫饼图符号上显示国旗?(使用,例如,`ggwaffle`)<!-- -->

<sup>Created on 2023-06-18 with reprex v2.0.2</sup>

Is it possible at all with ggwaffle? I was also trying to play with ggflags but I don't see how to make these two compatible because ggflags replace points in a scatter plot.

I am also aware that Windows does not ship with country flag unicode emojis for political reasons, so base unicode wouldn't work (as in one of my previous attempts using the waffle package). So it would need to be a font that includes country flags or images thereof. Thanks for any help.

答案1

得分: 1

使用ggflags,您可以设置x和y坐标以复制一个网格。

library(ggflags)
library(ggplot2)

df1 <- data.frame(x=rep(1:15, each = 10),
                  y=rep(1:10, 15),
                  country = c(rep("us", 75), rep("ca", 50), rep("gb", 25)))

ggplot(df1, aes(x, y, country=country)) + 
  geom_flag() +
  scale_country() +
  theme_void() +
  theme(legend.position = "bottom")

这里有一个可以自动化这个过程的函数,虽然有点笨拙,但可能作为一个起点很有用...

library(ggflags)
library(ggplot2)

# @param in_map_var string 要映射到标志的元素向量
# @param len_x integer x轴的长度,x轴上的标志数量
# @param na_flag string 用于填充空网格空间的标志的ID,这是一个技巧。

waffle_map <- function(in_map_var, len_x = NA, na_flag = "ac"){

  # 计算网格维度  
  var_count <- length(in_map_var)
  
  if(is.na(len_x)){
    x_count <- ceiling(sqrt(var_count))
  } else {
    x_count <- len_x
  }

  y_count <- ceiling(var_count / x_count)
  grid_count <- x_count * y_count
  
  df <- 
    data.frame(x = rep(1:y_count, each = x_count),
               y = rep(1:x_count, y_count),
               country = c(in_map_var, rep(na_flag, grid_count - var_count))
               )

  country_4legend <- unique(df$country)[unique(df$country) != na_flag]
  
  p <- 
    ggplot(df, aes(x, y, country = country)) + 
    geom_flag(size = 8) +
    scale_country(breaks = country_4legend) +
    theme_void() +
    theme(legend.position = "bottom")

  if(grid_count > var_count){
    p <- 
      p +
      geom_point(data = df[var_count:grid_count, ], aes(x, y), colour = "white", size = 10)
  }
                 
  return(p)
  
}

var_2map <- c(rep("us", 75), rep("ca", 50), rep("gb", 25))

waffle_map(var_2map, len_x = 15)

waffle_map(var_2map)

供参考。

英文:

Using ggflags you could set the x and y coordinates to replicate a grid.

library(ggflags)
library(ggplot2)

df1 &lt;- data.frame(x=rep(1:15, each = 10),
                  y=rep(1:10, 15),
                  country = c(rep(&quot;us&quot;, 75), rep(&quot;ca&quot;, 50), rep(&quot;gb&quot;, 25)))

ggplot(df1, aes(x, y, country=country)) + 
  geom_flag() +
  scale_country() +
  theme_void() +
  theme(legend.position = &quot;bottom&quot;)

如何在华夫饼图符号上显示国旗?(使用,例如,`ggwaffle`)<!-- -->

<sup>Created on 2023-06-18 with reprex v2.0.2</sup>

Here's a go at a function which can automate this, it's more than a bit hacky but may be useful as a starter...

library(ggflags)
library(ggplot2)



``` r
#&#39; @param in_map_var string Vector of elements to be mapped to flags
#&#39; @param len_x integer Length of x axis, number of flags on x axis
#&#39; @param na_flag string ID of a flag which is nominally used for filling in the empty grid spaces, This is a hack.

waffle_map &lt;- function(in_map_var, len_x = NA, na_flag = &quot;ac&quot;){

  # work out grid dimensions  
  var_count &lt;- length(in_map_var)
  
  if(is.na(len_x)){
    x_count &lt;- ceiling(sqrt(var_count))
  } else {
    x_count &lt;- len_x
  }

  
  y_count &lt;- ceiling(var_count / x_count)
  grid_count &lt;- x_count * y_count
  
  df &lt;- 
    data.frame(x = rep(1:y_count, each = x_count),
               y = rep(1:x_count, y_count),
               country = c(in_map_var, rep(na_flag, grid_count - var_count))
               )

  country_4legend &lt;- unique(df$country)[unique(df$country) != na_flag]
  
  
  
  p &lt;- 
    ggplot(df, aes(x, y, country = country)) + 
    geom_flag(size = 8) +
    scale_country(breaks = country_4legend) +
    theme_void() +
    theme(legend.position = &quot;bottom&quot;)

    if(grid_count &gt; var_count){
    p &lt;- 
      p +
      geom_point(data = df[var_count:grid_count, ], aes(x, y), colour = &quot;white&quot;, size = 10)
  }
                 
  return(p)
  
}

var_2map &lt;- c(rep(&quot;us&quot;, 75), rep(&quot;ca&quot;, 50), rep(&quot;gb&quot;, 25))

waffle_map(var_2map, len_x = 15)

如何在华夫饼图符号上显示国旗?(使用,例如,`ggwaffle`)<!-- -->


waffle_map(var_2map)

如何在华夫饼图符号上显示国旗?(使用,例如,`ggwaffle`)<!-- -->

<sup>Created on 2023-06-18 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年6月18日 22:45:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76501103.html
匿名

发表评论

匿名网友

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

确定