0
votes

I am trying to plot some data (mainly character data, unfortunately) and I wanted to use geom_flag to create little flag images to put under the labels on the x axis (x = Country) to make it more clear since I cannot write the full country name (it would be a mess with long names such as Argentina)

But what I am getting is the image below... Only a few flags appear and they are not even properly showed as the image seems not be "not tall enough" to contain them. How can I modify my code to show all the flags and also make sure the image is fully showed in the graph? I know it's complicated, thank you so much for your help! Here's my code:

data$iso2 <- countrycode(data$Country, "country.name", "iso2")

heatmap_trial3 <- y_Rec1 %>%
  select(iso2, Place, `Contentious?`,`Comments number`, Country) %>%
  ggplot(aes(x=iso2,
             y=Place,
             group=iso2)) +
  geom_point(aes(shape=`Contentious?`,
                 size=`Comments number`,
                 color=Country)) +
  theme(plot.background = element_rect(fill = NA),
        panel.background = element_rect(fill = "white")) +
  guides(size=FALSE, color=FALSE) +
  geom_flag(y =-0.75, aes(image = iso2)) +
  xlab(NULL) + expand_limits(y=-0.75)

dput() of my data:

structure(list(iso2 = c("CA", "DE", "DE", "CA", "CA", "AU", "RU", "BE", "BE", "BE", "BE", "TR", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "DE", "FR", "FR", "FR", "CN", "CN", "CN", "CN", "CN", "CN", "CN", "CN", "CN", "FR", "FR", "FR", "JP", "JP", "JP", "JP", "JP", "SA", "SA", "BR", "BR", "BR", "BR", "BR", "BR", "BR", "BR", "BR", "BR", "BR", "RU", "RU", "BE", "IT", "IT", "IT", "IT", "IT", "IT", "IT", "IT", "AR", "IT", "IT", "CH", "AR", "IT", "US", "US", "AR", "AR", "AR", "AR", "AR", "JP", "JP", "US", "US", "US"), Place = structure(c(4L, 3L, 3L, 3L, 4L, 4L, 3L, 5L, 5L, 5L, 5L, 4L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 3L, 3L, 4L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 4L, 4L, 5L, 4L, 5L, 5L, 5L, 5L, 3L, 4L, 2L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 2L, 3L, 5L, 3L, 3L, 2L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 5L, 3L, 5L, 3L, 3L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 3L, 4L, 5L), .Label = c("Context Intro", "Context Rec 1", "Protectionism", "WTO Reform", "Multilateralism", "Context Rec 2", "Crisis Response", "Customs", "Gloval Value Chains", "Context Rec 3", "Micro-Small-Med-Entrepr.", "Trade Finance", "Export Credit Agencies", "Context Rec 4", "Green Trade", "Foreing Investments"), class = "factor"), Contentious? = c("Not contentious", "Contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Contentious", "Not contentious", "Contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Contentious", "Contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Contentious", "Not contentious", "Not contentious", "Not contentious", "Contentious", "Contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Contentious", "Contentious", "Contentious", "Contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Contentious", "Contentious", "Contentious", "Not contentious", "Not contentious", "Contentious", "Not contentious", "Not contentious", "Not contentious", "Contentious", "Contentious", "Contentious", "Not contentious", "Contentious", "Contentious", "Contentious", "Contentious", "Not contentious", "Not contentious", "Not contentious", "Contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Contentious", "Not contentious", "Contentious", "Not contentious", "Contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Contentious", "Not contentious", "Contentious", "Contentious", "Not contentious", "Not contentious", "Contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Not contentious", "Contentious", "Not contentious", "Contentious", "Not contentious", "Contentious", "Not contentious" ), Comments number = c(2L, 9L, 9L, 1L, 2L, 1L, 2L, 4L, 4L, 4L, 4L, 1L, 5L, 5L, 5L, 5L, 5L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 2L, 2L, 3L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 1L, 1L, 6L, 6L, 6L, 6L, 1L, 1L, 2L, 3L, 3L, 3L, 6L, 6L, 6L, 6L, 6L, 6L, 2L, 2L, 1L, 1L, 4L, 3L, 3L, 3L, 4L, 4L, 2L, 2L, 7L, 2L, 2L, 1L, 7L, 4L, 2L, 2L, 7L, 7L, 7L, 7L, 7L, 6L, 6L, 2L, 1L, 2L ), Country = c("Canada", "Germany", "Germany", "Canada", "Canada", "Australia", "Russia", "Belgium", "Belgium", "Belgium", "Belgium", "Turkey", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "France", "France", "France", "China", "China", "China", "China", "China", "China", "China", "China", "China", "France", "France", "France", "Japan", "Japan", "Japan", "Japan", "Japan", "Saudi Arabia", "Saudi Arabia", "Brazil", "Brazil", "Brazil", "Brazil", "Brazil", "Brazil", "Brazil", "Brazil", "Brazil", "Brazil", "Brazil", "Russia", "Russia", "Belgium", "Italy", "Italy", "Italy", "Italy", "Italy", "Italy", "Italy", "Italy", "Argentina", "Italy", "Italy", "Switzerland", "Argentina", "Italy", "United States of America", "United States of America", "Argentina", "Argentina", "Argentina", "Argentina", "Argentina", "Japan", "Japan", "United States of America", "United States of America", "United States of America")), row.names = c(NA, -114L), class = c("tbl_df", "tbl", "data.frame"))

1
Thank you for posting the data. For those trying to read it in, the variables Contentious? and Comments number variables need to be surrounded with backticks. SO messes up that display a little.Joel Buursma

1 Answers

0
votes

Try adding + expand_limits(y=-1) to your ggplot statement.

See this for another example from the author of that function.

After reading your data into a data object, this code:

library(dplyr)
library(ggplot2)
library(ggimage)

data %>%
  select(iso2, Place, `Contentious?`,`Comments number`, Country) %>%
  ggplot(aes(x=iso2,
             y=Place,
             group=iso2)) +
  geom_point(aes(shape=`Contentious?`,
                 size=`Comments number`,
                 color=Country)) +
  theme(plot.background = element_rect(fill = NA),
        panel.background = element_rect(fill = "white")) +
  guides(size=FALSE, color=FALSE) +
  geom_flag(y =-0.75, aes(image = iso2)) +
  xlab(NULL) + expand_limits(y=-1)

... produces for me this graph with flags that aren't cut off: Graph result

You can experiment with different y values for geom_flag and expand_limits. Just make sure your y value for expand_limits expands the graph far enough to make the flags visible.