我目前正在尝试制作两张地图,一张有多个分类值,一张有连续的数值,如:
我有一个数据集,它提供 NPA 和每个 NPA 的两个信息:项目(类别)和频率(范围从 1 到 10):
NPA item Frequency
1000 huitante 0
1002 huitante 10
1006 quatre-vingt 3
2000 huitante 9
对于我工作的国家(瑞士),我也有一个特定的 shapefile。在上一篇文章中,我发现了一些有趣的代码,我在这里复制/粘贴:
# open the shapefile
require(rgdal)
require(rgeos)
require(ggplot2)
ch <- readOGR(work.dir, layer = "PLZO_PLZ")
# convert to data frame for plotting with ggplot - takes a while
ch.df <- fortify(ch)
# generate fake data and add to data frame
ch.df$count <- round(runif(nrow(ch.df), 0, 100), 0)
# plot with ggplot
ggplot(ch.df, aes(x = long, y = lat, group = group, fill = count)) +
geom_polygon(colour = "black", size = 0.3, aes(group = group)) +
theme()
作者在评论中提供了一些信息来绘制特定的数据集(而不是假数据):
# plot just a subset of NPAs using ggplot
my.sub <- ch.df[ch.df$id %in% c(4,6), ]
ggplot(my.sub, aes(x = long, y = lat, group = group, fill = count)) +
geom_polygon(colour = "black", size = 0.3, aes(group = group)) +
theme()
并在帖子的评论中说:
将 ggplot(my.sub, aes(x = long, y = lat, group = group, fill = count)) 替换为 ggplot(my.sub, aes(x = long, y = lat, group = group, fill = frequency ))
所以我想我需要提取频率作为变量
frequency <- table(data$frequency)
并按照报价中的指示更改代码。
不幸的是,我的问题是它不起作用,我收到以下评论:
不知道如何为表格类型的对象自动选择比例。默认为连续错误:美学长度必须为 1,或与 dataProblems:frequency 长度相同
我的问题是:
- 如何更改代码以包含我自己的数据,并绘制数值(频率)
- 如何更改代码以包含我自己的数据,并绘制分类值(项目)
我不需要在同一张地图上表示频率和项目,只知道如何创建单独的地图。
我的数据集和我需要使用的 shapefile 都在这个文件中。
https://www.dropbox.com/sh/5x6r6s2obfztblm/AAAesIOrxn76HU57AIF0y1Oua?dl=0
任何帮助将不胜感激!