【问题标题】:Europe map with mean values ​in the middle of each country欧洲地图的平均值在每个国家的中间
【发布时间】:2021-01-22 03:48:01
【问题描述】:

我用ggplot() 创建了一张欧洲地图。此外,我有一些国家/地区的平均价格,其中有平均值的国家/地区的颜色不同(取决于平均水平),所有其他国家/地区默认为灰色。

我的代码如下所示:

library(ggplot2)
library(grid)
library(rworldmap)

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

## Define vector with all european states: ##
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 statesin europe: ##
indEU <- which(worldMap$NAME%in%v.europe)


## Extract longitude and latitude border's coordinates of states: ##
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)

## Mean values of some of the states in 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$region, names(v.meanValues))]


ax <- list(
  title = "",
  zeroline = FALSE,
  showline = FALSE,
  showticklabels = FALSE,
  showgrid = FALSE
)   
## Plot the map: ##
p <- ggplot() + 
     geom_polygon(data = df.europeCoords, aes(x = long, y = lat, group = region, fill = meanValues,
                                              text = paste("<b>", region, '</b>\n',
                                                           "Price Mean: ", meanValues, "\u20ac")),
                  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"),
     )
EuropePlot <- plotly::ggplotly(p, tooltip = "text") %>%
              layout(xaxis = ax, yaxis = ax)

剧情如下:

我想将可用的平均值(作为数字)放在相应国家的中间。我该怎么做?

【问题讨论】:

  • 关于坐标轴,+ theme_void() 可能会满足您的需求,而对于颜色,ColorBrewer r-graph-gallery.com/38-rcolorbrewers-palettes.html 可能会很有趣。
  • @D.J 我已经尝试过theme_void(),但没有成功!
  • 您可以使用 layout() 删除坐标区。见here
  • @stefan 谢谢!这样,y 和 y 轴就被完全删除了!谢谢

标签: r ggplot2 maps plotly


【解决方案1】:

我找到了两种适合我的解决方案:

解决方案 1:

我已经创建了一个包含每个国家的地区和经纬度的 Excel 文件,并尝试如下解决:

## 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$region, names(v.meanValues))]
as.data.frame(df.longLat)

## Add it to plot: ##
p <- p + geom_text(data = df.longLat, aes(long, lat, label = meanValues), size = 2)
EuropePlot <- plotly::ggplotly(p, tooltip = "text") %>%
              layout(xaxis = ax, yaxis = ax)

解决方案 2:

我用下面的代码计算了每个国家的经纬度的平均值:

df.europeCoords <- aggregate(cbind(long, lat) ~ region, data = df.europeCoords, 
                             FUN = function(x)mean(range(x)))

df.europeCoords$meanValues <- v.meanValues[match(df.europeCoords$region, names(v.meanValues))]

p <- p + geom_text(data = df.europeCoords, aes(long, lat, label = meanValues), size = 2)
EuropePlot <- plotly::ggplotly(p, tooltip = "text") %>%
              layout(xaxis = ax, yaxis = ax)

【讨论】:

    猜你喜欢
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 2015-04-06
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多