警告在RShiny中出错:忽略未知美学:文本

警告在RShiny中出错:忽略未知美学:文本,r,ggplot2,maps,aesthetics,R,Ggplot2,Maps,Aesthetics,我正在做以下工作: ## Get the world map: ## worldMap <- getMap() ## Define vector with all European countries: ## v.europe <- c("Norway", "Sweden", "Finland", "Denmark", "United Kingdom","Ireland&q

我正在做以下工作:

## 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
)
  ## 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)
 
情节如下所示:

问题是R中的警告并不麻烦,因为它仍然绘制期望的结果。在Shining应用程序中调用时,不会显示任何内容,正是因为此警告。在RShiny也应该是这样的,所以我必须设法解决这个警告。
如何解决此问题?

我已按以下方式解决了此问题:

## 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
)
  ## 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)
 
##绘制地图:##

p要添加文本标签,可以使用
geom_text
geom_label
,美学名称为
label
,而不是
text
。您可能还需要加载才能在文本标签中使用html标记。或者,考虑到您正在创建地图,您最好使用而不是
geom_polygon
。如果我在绘图构造的最后一行的
geom_text()
中使用
text=paste(…)
,那么我会收到相同的警告消息。我真的不知道如何解决这个问题,这将是
label=paste(…)
。在
geom\u text
geom\u label
中,使用
label=
指定映射,而不是
text=
。有关更多详细信息,请参阅。我希望仅针对数据框
df.europeCoords
使用
text=paste(…)
。在
geom_text()
中有另一个数据框作为数据输入,这里的label=
meanValues
已经正确。我希望在将鼠标移到国家上时显示国家名称。