错误:美学的长度必须为1或与数据相同(4) [英] Error: Aesthetics must be either length 1 or the same as the data (4)

查看:389
本文介绍了错误:美学的长度必须为1或与数据相同(4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ggmap.目的是在地图上绘制坐标点,并用其名称标记这些点.我有名称,经度和纬度的数据框.

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.

数据如下:

df <- structure(list(Station.Area = c("Balbriggan", "Blanchardstown", 
"Dolphins Barn", "Donnybrook", "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")

我写的代码如下:

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()

但是我得到

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

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

我曾尝试在geom_text中加入各种美学元素,例如尺寸,颜色,x&是,但仍然会给出相同的错误.

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

我是否为自己的目标正确地做到了?请帮忙.

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

现在不用geom_text来获取它,我只想标记点

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

推荐答案

您的代码中有些事情不太正确.

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

对于geom_point,您只需要aes中的xy.其他论点 应该在外面,给

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

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

geom_textlabel也应位于aes内部.然而, 因为没有更高级别的dataxy,所以geom_text 将找不到标签变量或放置标签的位置.因此,您还需要在通话中包含这些信息

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

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

但是,您可以使用base_layer自变量省略某些重复 的ggmap:

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

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或与数据相同(4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆