【发布时间】: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 轴就被完全删除了!谢谢