Home > Software design >  R visualization: How to display flags on points created by geom_quasirandom with the use of geom_fla
R visualization: How to display flags on points created by geom_quasirandom with the use of geom_fla

Time:06-09

I am pretty new to R programming and I need help with plotting flags using ggflags library.

If you run the code below, it will create a single graph of density plots, box plots, and swarm plots. There are two purple dots in each 5-year period, which stand for China and India (the two most populous countries).

What I need is to replace these purple dots with flags of China and India, and with the country code attached to each flag.

I used the ggflags library but I could not figure out how to use it (geom_flag) with the points created by geom_quasirandom.

The two code lines mentioned are:

geom_quasirandom(aes(color = color_custom, alpha = alpha_custom, size = size_custom), width = 0.20)

This creates points of the swarm plot, including purple dots.

geom_flag(aes(country = CodeISO2C), size = 3)

This uses the country codes to make flags. However, when you uncomment this line, an error will occur:

Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘grobify’ for signature ‘"NULL"’

If it is not possible to use flags instead of purple dots, then please tell me how to set 2 colors for China and India and add a legend with a single function call: ComboGraph(country_data['isMostPopulous'],'none')

Thank you very much for your help.

The data source file can be downloaded from: enter image description here

enter image description here

CodePudding user response:

ggbeeswarm comes with a few new "positions": these are specific objects that define the offset of the plotted points. You can pass this position to geom_flag

I have not used your example because it seems quite... involved. I hope the underlying concept becomes clear from the below example.

library(ggbeeswarm)
#> Loading required package: ggplot2
# devtools::install_github("jimjam-slam/ggflags")
library(ggflags)

ggplot(iris)  
  geom_flag(aes(x = Species, Sepal.Length, country = "fr"), 
            position = position_quasirandom())

Created on 2022-06-08 by the reprex package (v2.0.1)

  • Related