简体   繁体   中英

Points to flags in ggplot2

I would like to change the dots in the next plot to the flag for the respective country. For this I know that geom_flag() could works, but... not for me but I got stuck with this error message:

Error in grobify(picture, x = x, y = y, width = width, height = height,  : 
  object '.flaglist' not found

This is my code:

ggplot(df, aes(lenght, Ponto.Caspian)) + 
 geom_point()+   ggflags::geom_flag(aes(x = longitude, y = latitude+0.5, country = country))

maybe also geom_image() could work but I don't know how to link the flag with the country column...or How to use an image as a point in ggplot? , but I don't understand the process

This my desire plot, but changing dots to flags:

ggplot(df, aes(lenght, Ponto.Caspian)) + 
  geom_point(aes(colour = factor(country)))+ 
  scale_fill_brewer(palette = "Set3")+ scale_x_continuous(breaks = seq(10, 40, by = 5))+
  scale_y_continuous(breaks = seq(0, 16, by = 1))+
  theme_classic2() +  theme_cleveland()+ geom_smooth(method = "lm", alpha=0.2)+
  stat_cor(label.x = 2, label.y = 1)

在此处输入图像描述

This is my data: https://drive.google.com/drive/folders/1qR2mUdrpStOYBmxajc_F4nxS_qf-4bzf?usp=sharing

thanks in advance

It seems to work for me if I convert your countries to two-character ISO codes (which is what the example at https://github.com/jimjam-slam/ggflags uses).

I also had to load the ggflags library, rather than using ggflags::geom_flag

For example, using the countrycode package:

df$iso_code <- tolower(countrycode(df$country, origin = 'country.name', destination = 'iso2c'))

ggplot(df, aes(lenght, Ponto.Caspian)) + 
  geom_point() +
  geom_flag(aes(x = longitude, y = latitude+0.5, country = iso_code))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM