【问题标题】:How to merge a map made with ggplot and a bubble plot in r如何合并用ggplot制作的地图和r中的气泡图
【发布时间】:2021-06-07 15:43:48
【问题描述】:

我可以制作一张欧洲地图和将叠加在地图顶部的气泡图,但我不知道如何成功地将它们合并在一起。有人会帮我这样做吗? :)

这是我用来创建两个独立组件的代码:

    library(cowplot)
    library(googleway)
    library(ggplot2)
    library(ggrepel)
    library(ggspatial)
    library(sf)
    library(rnaturalearth)
    library(rnaturalearthdata)    
    library("ggplot2")
    theme_set(theme_bw())
    library("sf")
    library("rnaturalearth")
    library("rnaturalearthdata")
    
    world <- ne_countries(scale = "medium", returnclass = "sf")
    class(world)
     
    ## Produces the map of Europe ##
    ggplot(data = world) +
    geom_sf() +
    coord_sf(xlim = c(-15, 55), ylim = c(34, 71), expand = FALSE)
   
             
    cities <- read.csv("cities1_R.csv")
    df <- cities
             
    df$Country <- as.factor(df$Country)
             
    head(df[ , c("City", "Country", "Latitude", "Longitude", "No..of.Samples")], 28)
             
    ## Produces Bubbles plot ##               
    ggplot(df, aes(x = Longitude, y = Latitude)) + 
    geom_polygon(df, aes(x= Longitude, y= Latitude, group=group), fill="grey", alpha=0.3)
    geom_point(aes(color = Country, size = No..of.Samples), alpha = 0.9) +
    scale_color_manual(values = c("#FF3300", "#0066CC", "#FFFF00", "#CC0066", "#9933CC", 
    "#00FFFF", "#FF9900", "#000000", "#00CC00", 
    "#999999", "#FF6666", "#FF3399", "#669933", 
    "#000099", "#6666FF", "#003300", "#66FF33", "#660066", "#FF99CC", 
    "#333333", "#CC33FF", "#CC6699", "#663300", "#330033", "#666600", "#CCFF00", "#CC9900", 
    "#990033")) +
    scale_size(breaks = c(0, 25, 50, 75, 100, 125, 150, 175), range = c(0.5, 12))  # Adjust 
    the range of points size

气泡的大小取决于编号。城市数据中的样本。这是表格: cities1_R.csv file showing the data in a table format

City Country Latitude Longitude No..of.Samples
1 AMSTERDAM NETHERLANDS 52.37403 4.88969 22
2 BARI ITALY 41.11148 3.53390 196
3 BUDAPEST HUNGARY 47.49801 19.03991
4 LONDON UNITED KINGDOM 51.50853 -0.12574
5 BERLIN GERMANY 52.52437 13.41053
6 COPENHAGEN DENMARK 55.67594 12.56553
7 LISBOA PORTUGAL 38.71667 -9.13333
8 WARSZAW POLAND 52.22977 21.01178
9 DUBLIN IRELAND 53.33306 -6.24889
10 LJUBLJANA SLOVENIA 46.05108 14.50513
11 BRUXELLES BELGIUM 50.85045 4.34878
12 PARIS FRANCE 48.85341 2.34880
13 WIEN AUSTRIA 48.20849 16.37208
14 ASTURIAS SPAIN 43.36140 -5.85930
15 ATHENS GREECE 37.98380 23.72750
16 STOCKHOLM SWEDEN 59.32930 18.06860
17 SOFIA BULGARIA 42.69770 23.32190
18 ZAGREB CROATIA 45.81500 15.98190
19 NICOSIA CYPRUS 35.18560 33.38230
20 PRAGUE CZECH REPUBLIC 50.07550 14.43780
21 TALLINN ESTONIA 59.43700 24.75360
22 HELSINKI FINLAND 60.16990 24.93840
23 RIGA LATVIA 56.94960 24.10520
24 VILNIUS LITHUANIA 54.68720 25.27970
25 LUXEMBOURG CITY LUXEMBOURG 49.81530 6.12960
26 VALLETTA MALTA 35.89890 14.51460
27 BUCHAREST ROMANIA 44.42680 26.10250
28 BRATISLAVA SLOVAKIA 48.14860 17.10770

这是我制作的欧洲地图: Map of Europe

这是我制作的气泡图: Bubble ggplot

如果有人知道如何合并这两个 ggplot,那就太棒了。我尝试使用 geom_polygon 函数,但我无法让它工作。提前谢谢!

【问题讨论】:

  • 你能把它变成一个可重现的例子吗?如果您添加从 CSV 加载的数据帧的硬编码版本,其中只有几行,那么任何人都可以更轻松地运行您的代码,
  • 你是这个意思吗?我已经编辑了帖子^。
  • 不完全是,但我在回答中添加了一个简单的示例。

标签: r ggplot2


【解决方案1】:

我添加了一个只有 3 个城市的粗略版本,但这应该适用于您的整个数据。

library(cowplot)
library(googleway)
library(ggplot2)
library(ggrepel)
library(ggspatial)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)    
library("ggplot2")
theme_set(theme_bw())
library("sf")
library("rnaturalearth")
library("rnaturalearthdata")

x = "1 AMSTERDAM NETHERLANDS 52.37403 4.88969 22

7 LISBOA PORTUGAL 38.71667 -9.13333 12

3 BUDAPEST HUNGARY 47.49801 19.03991 0"


df <- read.table(text=x, 
                 header = F,
                 col.names=c('idx', 'City', 'Country', 'Latitude',  'Longitude', 'No..of.Samples'))

world <- ne_countries(scale = "medium", returnclass = "sf")
class(world)

## Produces the map of Europe ##
ggplot(data = world) +
  geom_sf() +
  geom_point(df, 
             mapping = aes(x= Longitude, y= Latitude,  size = No..of.Samples, color = Country), 
             fill="grey", alpha=0.3) + 
  coord_sf(xlim = c(-15, 55), ylim = c(34, 71), expand = FALSE)

它输出:

【讨论】:

  • 嗨,非常感谢您。我不断收到此错误 - read.table 中的错误(text = x,header = F,col.names = c(“idx”,“City”,:比列名更多的列
  • 你知道为什么会这样吗?提前谢谢!
  • 我只使用df &lt;- read.table(...),因为我没有citys1_R.csv。如果你用 df &lt;- read.csv("cities1_R.csv") 替换它应该可以工作。
猜你喜欢
  • 2019-08-08
  • 1970-01-01
  • 2023-03-12
  • 2021-11-13
  • 1970-01-01
  • 2021-04-28
  • 2015-11-09
  • 1970-01-01
  • 2018-09-15
相关资源
最近更新 更多