0

I'm doing the following:

## Get the world map: ##
worldMap <- getMap()

## Define vector with all European countries: ##
v.europe <- c("Norway", "Sweden", "Finland", "Denmark", "United Kingdom","Ireland", "Greece",
              "Belgium", "Netherlands", "France", "Spain", "Portugal", "Luxembourg", "Croatia",
              "Germany", "Switzerland", "Austria", "Slovenia", "Italy", "Bulgaria", "Romania",
              "Czech Rep.", "Slovakia", "Hungary", "Poland", "Bosnia Hercegovina", "Serbia",
              "Turkey", "Ukraine", "Moldova", "Belarus", "Estonia", "Latvia", "Lithuania",
              "Montenegro", "Albania", "Macedonia")
              
## Select only the index of countries of Europe: ##
indEU <- which(worldMap$NAME%in%v.europe)


## Extract longitude and latitude border's coordinates of countries: ##
df.europeCoords <- lapply(indEU, function(i){
    df <- data.frame(worldMap@polygons[[i]]@Polygons[[1]]@coords)
    df$region = as.character(worldMap$NAME[i])
    colnames(df) <- list("long", "lat", "region")
    return(df)
})
df.europeCoords <- do.call("rbind", df.europeCoords)
names(df.europeCoords) <- c("longitude", "latitude", "country")

## Mean values of some of the countries of Europe: ##
meanGermany <- 33.33
meanAustria <- 35.71
meanNetherlands <- 35.9
meanBelgium <- 34.66
meanFrance <- 34.89
meanItaly <- 43.97
meanHungary <- 43.96
meanCroatia <- 42.54
meanBulgaria <- 54.61
meanGreece <- 25.72
meanNorway <- 27.64
meanSweden <- 36.41
meanFinland <- 32.13
meanDenmark <- 36.83
meanSlovakia <- 35.94
meanCzechia <- 44.15
meanRomania <- 36.52
meanSwitzerland <- 44.12
meanSerbia <- 45.53
meanSlovenia <- 45.1

## Create vector with mean values: ##
v.meanValues <- c('Germany' = meanGermany, 'Austria' = meanAustria, 'Netherlands' = meanNetherlands,
                  'Belgium' = meanBelgium, 'France' = meanFrance, 'Italy' = meanItaly, 'Greece' = meanGreece,
                  'Hungary' = meanHungary, 'Croatia' = meanCroatia, 'Bulgaria' = meanBulgaria,
                  'Norway' = meanNorway, 'Sweden' = meanSweden, 'Finland' = meanFinland, 'Denmark' = meanDenmark,
                  'Slovakia' = meanSlovakia, 'Czech Rep.' = meanCzechia, 'Romania' = meanRomania,  
                  'Serbia' = meanSerbia, 'Slovenia' = meanSlovenia, 'Switzerland' = meanSwitzerland)

## Merge mean values with European countries: ##
df.europeCoords$meanValues <- v.meanValues[match(df.europeCoords$country, names(v.meanValues))]


## Read Excel-file with longitude and latitude of each country: ##
df.longLat <- read_excel("C:/Users/z_kordasch/Documents/fire/server/input_Data/EUROPE_LongitudeLatitude.xlsx", na = "NA")

## Merge mean values to this new data frame: ##
df.longLat$meanValues <- v.meanValues[match(df.longLat$country, names(v.meanValues))]



## Deletes/Removes borders of PLOT: ##
ax <- list(
  title = "",
  zeroline = FALSE,
  showline = FALSE,
  showticklabels = FALSE,
  showgrid = FALSE
)

When I'm plotting the European map I get the error massage Warning: Ignoring unknown aesthetics: text, where I use text = paste("<b>", country, '</b>') in geom_polygon(). See the next code snippet:

## Plot the map: ##
p <- ggplot() + 
     geom_polygon(data = df.europeCoords, 
                  aes(x = longitude, y = latitude, 
                      group = country, fill = meanValues, 
                      text = paste("<b>", country, '</b>')),
                  color = "black", size = 0.1) +
     coord_map(xlim = c(-13, 35),  ylim = c(32, 71)) +
     theme_classic() +
     scale_fill_gradient(name = "Price Mean Values", low = "#81C07A", 
                         high = "#007d3c", na.value = "#CCCCCC") +
     theme(axis.text.x = element_blank(), 
           axis.text.y = element_blank(), 
           axis.ticks.x = element_blank(),
           axis.ticks.y = element_blank(), 
           axis.title = element_blank(), 
           legend.position = "none",
           plot.margin = unit(0 * c(-1.5, -1.5, -1.5, -1.5), "lines")) +
     geom_text(data = df.longLat, aes(long, lat, label = meanValues), size = 2)
                  
## Generate ggplot() to plot_ly() object: ##
EuropePlot <- plotly::ggplotly(p, tooltip = "text") %>%
              layout(xaxis = ax, yaxis = ax)

The plot looks like this:

enter image description here

The problem is that the warning in R doesn't bother as it still plots the desired result. When called in the Shiny App, nothing is displayed, precisely because of this warning. And it's supposed to work the same way in RShiny, so I have to somehow resolve this warning. How can I solve this issue/problem?

Phil
  • 7,287
  • 3
  • 36
  • 66
MikiK
  • 398
  • 6
  • 19
  • To add a text label, you would use `geom_text` or `geom_label` and the aesthetic is named `label` rather than `text`. You might also need to load the [`ggtext` package](https://github.com/wilkelab/ggtext) to be able to use html tags in the text labels. Or maybe you would be better off using [`geom_sf` and `geom_sf_text`](https://ggplot2-book.org/maps.html), rather than `geom_polygon`, given that you're creating maps. – eipi10 Oct 08 '20 at 06:10
  • If I use ```text = paste(...)``` in ```geom_text()``` of the last line of the plot construction, then I'm getting the same warning message. And I don't really know how to fix this – MikiK Oct 08 '20 at 06:16
  • It would be `label=paste(...)`. In `geom_text` and `geom_label` you specify the mapping with `label=`, rather than `text=`. See the [`geom_text` help](https://ggplot2.tidyverse.org/reference/geom_text.html#aesthetics) for additional details. – eipi10 Oct 08 '20 at 06:18
  • I want to have the ```text = paste(...)``` only for the data frame ```df.europeCoords```. In ```geom_text()``` there is another data frame as data input, and here label = ```meanValues``` is already correct. I would like the country name to be displayed when I move the mouse over a country. – MikiK Oct 08 '20 at 06:31

1 Answers1

0

I've solved the issue as the following:

  ## Plot the map: ##
  p <- ggplot(data = df.europeCoords, aes(x = longitude, y = latitude, group = country, 
                                          fill = meanValues, text = paste("<b>", country, '</b>\n')),  
              color = "black", size = 0.1) + 
       geom_polygon() +
       coord_map(xlim = c(-13, 35),  ylim = c(32, 71)) +
       theme_classic() +
       scale_fill_gradient(name = "Price Mean Values", low = "#81C07A", high = "#007d3c", 
                           na.value = "#CCCCCC") +
       theme(axis.text.x = element_blank(), axis.text.y = element_blank(), 
             axis.ticks.x = element_blank(), axis.ticks.y = element_blank(), 
             axis.title = element_blank(),  legend.position = "none",
             plot.margin = unit(0 * c(-1.5, -1.5, -1.5, -1.5), "lines")) +
       geom_text(data = df.longLat, aes(x = long, y = lat, label = meanValues), size = 2)
  
  ## Generate ggplot() to plot_ly() object: ##
  EuropePlot <- plotly::ggplotly(p, tooltip = "text") %>%
                layout(xaxis = ax, yaxis = ax)
 
MikiK
  • 398
  • 6
  • 19