[英]Problems creating points on a map ggplot2/r
I have a data set of UK earthquakes that I want to plot by location on a map.我有一个英国地震数据集,我想按 map 上的位置对它进行 plot。 (Hopefully I then want to change the size to be representative of the magnitude). (希望我想改变大小以代表大小)。 I have made a map of the uk using ggmap, but I am struggling to then add the points to a map.我已经使用 ggmap 制作了英国的 map,但我正在努力将这些点添加到 map。
I however keep getting 2 errors, and cannot plot my points on the map.然而,我不断收到 2 个错误,并且不能 plot 我在 map 上的观点。 The errors are either - Error: Aesthetics must be either length 1 or the same as the data (990): x, y or - Error in FUN(X[[i]], ...): object 'group' not found depending on how I try to plot the points.错误是 -错误:美学必须是长度 1 或与数据 (990) 相同:x,y或 - FUN 中的错误(X[[i]],...):object 'group' not found取决于我如何尝试 plot 点。
this is what I have so far:这就是我到目前为止所拥有的:
table <- data.frame(long2, lat2, mag1)
table
long2 lat2 mag1
1 -2.62 52.84 1.9
2 1.94 57.03 4.2
3 -0.24 51.16 0.6
4 -2.34 53.34 0.8
5 -3.16 55.73 2.0
6 -0.24 51.16 1.0
7 -4.11 53.03 1.5
8 -0.24 51.16 0.2
9 -0.24 51.16 1.1
10 -5.70 57.08 1.6
11 -2.40 53.00 1.4
12 -1.19 53.35 1.2
13 -1.02 53.84 1.7
14 -4.24 52.62 0.8
15 -3.23 54.24 0.3
16 -2.06 52.62 1.0
17 1.63 54.96 1.7
18 -5.24 56.05 0.7
19 -5.86 55.84 1.3
20 -3.22 54.23 0.3
21 -0.24 51.16 -1.4
22 -0.24 51.16 -0.7
23 -4.01 55.92 0.3
24 -5.18 50.08 2.3
25 -1.95 54.44 1.0
library(ggplot2)
library(maps)
w <- map_data("world", region = "uk")
uk <- ggplot(data = w, aes(x = long, y = lat, group=group)) + geom_polygon(fill = "seagreen2", colour="white") + coord_map()
uk + geom_point(data=table, aes(x=long2, y=lat2, colour="red", size=2), position="jitter", alpha=I(0.5))
Is it the way I have built my map, or how I am plotting my points?是我构建 map 的方式,还是我如何绘制我的观点? And how do I fix it?我该如何解决?
I've made three changes to your code, and one or more of them solved the problems you were having.我对您的代码进行了三处更改,其中一项或多项解决了您遇到的问题。 I'm not sure exactly which—feel free to experiment!我不确定到底是哪一个——随意尝试吧!
pdat
(point data) instead of table
.我将您的数据命名为pdat
(点数据)而不是table
。 table
is the name of a built-in R function, and it's best to avoid using it as a variable name. table
是一个内置的 R function 的名称,最好避免使用它作为变量名。data=
expressions inside the geom function that needs that data (instead of placing the data=
and aes()
inside the initial ggplot()
call.) When I use two or more data.frames in a single plot, I do this defensively and find that it avoids many problems.我已将两个data=
表达式放在需要该数据的 geom function 中(而不是将data=
和aes()
放在初始ggplot()
调用中。)当我在单个 plot 中使用两个或多个 data.frames 时,我防御性地这样做,并发现它避免了许多问题。colour="red"
and size=2
outside of the aes()
function.我已将colour="red"
和size=2
移到aes()
function 之外。 aes()
is used to create an association between a column in your data.frame and a visual attribute of the plot. aes()
用于在 data.frame 中的列与 plot 的可视属性之间创建关联。 Anything that's not a name of a column doesn't belong inside aes()
.任何不是列名的东西都不属于aes()
。# Load data.
pdat <- read.table(header=TRUE,
text="long2 lat2 mag1
1 -2.62 52.84 1.9
2 1.94 57.03 4.2
3 -0.24 51.16 0.6
4 -2.34 53.34 0.8
5 -3.16 55.73 2.0
6 -0.24 51.16 1.0
7 -4.11 53.03 1.5
8 -0.24 51.16 0.2
9 -0.24 51.16 1.1
10 -5.70 57.08 1.6
11 -2.40 53.00 1.4
12 -1.19 53.35 1.2
13 -1.02 53.84 1.7
14 -4.24 52.62 0.8
15 -3.23 54.24 0.3
16 -2.06 52.62 1.0
17 1.63 54.96 1.7
18 -5.24 56.05 0.7
19 -5.86 55.84 1.3
20 -3.22 54.23 0.3
21 -0.24 51.16 -1.4
22 -0.24 51.16 -0.7
23 -4.01 55.92 0.3
24 -5.18 50.08 2.3
25 -1.95 54.44 1.0")
library(ggplot2)
library(maps)
w <- map_data("world", region = "uk")
uk <- ggplot() +
geom_polygon(data = w,
aes(x = long, y = lat, group = group),
fill = "seagreen2", colour = "white") +
coord_map() +
geom_point(data = pdat,
aes(x = long2, y = lat2),
colour = "red", size = 2,
position = "jitter", alpha = 0.5)
ggsave("map.png", plot=uk, height=4, width=6, dpi=150)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.