错误:美学必须是长度1或与数据相同(4)

时间:2022-04-06 16:13:02

I am working with ggmap. the goal is to plot coordinate points on the map and label the points with their names. I have data frame with name, longitude and latitude.

我正在和ggmap合作。目标是在地图上标出坐标点,并把它们的名字标上标记。我有数据帧的名称,经度和纬度。

The data looks like:

数据看起来像:

df <- structure(list(Station.Area = c("Balbriggan", "Blanchardstown", 
"Dolphins Barn", "Donny*", "Dun Laoghaire", "Finglas"), Latitude = c(53.608319, 
53.386813, 53.333532, 53.319259, 53.294396, 53.390325), Longitude = c(-6.18208, 
-6.377197, -6.29146, -6.232017, -6.133867, -6.298401)), .Names =c("Station.Area","Latitude", "Longitude"), row.names = c(NA, 6L), class = "data.frame")

The code I wrote is as below:

我写的代码如下:

library(ggmap)
library(ggplot2)

dub_map <- get_map(location = "Dublin", zoom = "auto", scale="auto", crop = TRUE, maptype = "hybrid")

ggmap(dub_map) +`
    geom_point(data = df, aes(x = Longitude, y = Latitude, 
              fill = "green", alpha =` `0.8, size = 5, shape = 21)) +`
guides(fill=FALSE, alpha=FALSE, size=FALSE)+
geom_text(label=df$Station.Area)+
scale_shape_identity()

But i am getting

但我得到

Error: Aesthetics must be either length 1 or the same as the data (4): label

错误:美学必须是长度1或与数据相同(4):标签。

I have tried to put various aesthetics in geom_text like size,color,x & Y but it still gives out same error.

我试着把各种各样的美学放在像大小、颜色、x和Y这样的风水中,但它还是给出了同样的错误。

Am i doing it correctly for my goal? Please help.

我的目标正确吗?请帮助。

Getting this without geom_text now I just want to label the points

现在我想要把这些点标记出来。

错误:美学必须是长度1或与数据相同(4)

1 个解决方案

#1


2  

There are a couple of things not quite right in your code.

在您的代码中有一些事情并不完全正确。

For the geom_point you only want the x and y in your aes. The other arguments should be outside, giving

对于风水来说,你只需要你的aes中的x和y。其他的争论应该是在外面,给予。

geom_point(data = df, aes(x = Longitude, y = Latitude), 
                  fill = "green", alpha =0.8, size = 5, shape = 21)

Also the label for the geom_text should be inside aes. However, as there is no data, x or y at a higher level, then geom_text will not find the label variable or the positions of where to place the labels. So you also need to include these in the call

此外,在aes内也应该有“地_text”的标签。但是,由于在较高的级别上没有数据、x或y,因此,地址簿不会找到标签变量或在何处放置标签的位置。因此,您还需要在调用中包含这些内容。

geom_text(data=df, aes(x = Longitude, y = Latitude, label=Station.Area))

However, you can omit some of this repetition by using the base_layer argument of ggmap:

但是,您可以通过使用ggmap的base_layer参数来省略某些重复:

ggmap(dub_map, 
      base_layer = ggplot(data=df, aes(x = Longitude, 
                                       y = Latitude, 
                                       label=Station.Area))) +
      geom_point(fill = "green", alpha =0.8, size = 5, shape = 21) +
      geom_text() 

#1


2  

There are a couple of things not quite right in your code.

在您的代码中有一些事情并不完全正确。

For the geom_point you only want the x and y in your aes. The other arguments should be outside, giving

对于风水来说,你只需要你的aes中的x和y。其他的争论应该是在外面,给予。

geom_point(data = df, aes(x = Longitude, y = Latitude), 
                  fill = "green", alpha =0.8, size = 5, shape = 21)

Also the label for the geom_text should be inside aes. However, as there is no data, x or y at a higher level, then geom_text will not find the label variable or the positions of where to place the labels. So you also need to include these in the call

此外,在aes内也应该有“地_text”的标签。但是,由于在较高的级别上没有数据、x或y,因此,地址簿不会找到标签变量或在何处放置标签的位置。因此,您还需要在调用中包含这些内容。

geom_text(data=df, aes(x = Longitude, y = Latitude, label=Station.Area))

However, you can omit some of this repetition by using the base_layer argument of ggmap:

但是,您可以通过使用ggmap的base_layer参数来省略某些重复:

ggmap(dub_map, 
      base_layer = ggplot(data=df, aes(x = Longitude, 
                                       y = Latitude, 
                                       label=Station.Area))) +
      geom_point(fill = "green", alpha =0.8, size = 5, shape = 21) +
      geom_text()