Warning gets to Error in RShiny: Ignoring unknown aesthetics: text

759 views Asked by At

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?

1

There are 1 answers

0
Miko On BEST ANSWER

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)