R ggmap ggplot2 error "Error: Discrete value supplied to continuous scale"
R ggmap ggplot2 error "Error: Discrete value supplied to continuous scale"
首先,我很抱歉。
我知道有很多关于此错误的线索,但是我尝试了很多解决方案,但都没有设法解决我的问题。
我已经尝试将我的数据转换成多种形式,但我仍然遇到同样的错误,或者 ggplot2 不支持该数据格式。
这是我的代码:
library(ggmap)
library(ggplot2)
library(data.table)
setwd("~/Projects/reformat")
map <- get_map(location = c(-4,54.5), zoom = 6)
data <- read.csv('lonlatprice.csv')
colnames(data) <- c('Longitude','Latitude','Price')
ggmap(map, extent = "device") + geom_point(aes(x = data$Longitude, y = data$Latitude), colour = "red",
alpha = 0.1, size = 2)
数据格式是这样的:
> head(data)
Longitude Latitude Price
1 53.778274 -2.48129 147500
2 52.833819 -0.936527 182000
3 50.792457 0.046043 193000
4 51.476984 -0.612126 580000
5 51.460139 -0.01867 905000
6 52.235942 1.519404 641500
在此先感谢您的帮助,我在无数天都没有成功后才作为最后的手段询问。
你的代码有几个问题:
- 您检索到错误的地图。您混淆了
lon
和 lat
值在 get_map(location = c(-4,54.5), zoom = 6)
中的顺序
- 您正在以错误的方式调用
geom_point
部分中的数据。
以下代码修复了这些问题:
map <- get_map(location = c(51.5,0.2), zoom = 6)
ggmap(map) +
geom_point(data= data,
aes(x = Longitude, y = Latitude),
colour = "red",
alpha = 0.5,
size = 4)
并给你这张地图:
首先,我很抱歉。
我知道有很多关于此错误的线索,但是我尝试了很多解决方案,但都没有设法解决我的问题。
我已经尝试将我的数据转换成多种形式,但我仍然遇到同样的错误,或者 ggplot2 不支持该数据格式。
这是我的代码:
library(ggmap)
library(ggplot2)
library(data.table)
setwd("~/Projects/reformat")
map <- get_map(location = c(-4,54.5), zoom = 6)
data <- read.csv('lonlatprice.csv')
colnames(data) <- c('Longitude','Latitude','Price')
ggmap(map, extent = "device") + geom_point(aes(x = data$Longitude, y = data$Latitude), colour = "red",
alpha = 0.1, size = 2)
数据格式是这样的:
> head(data)
Longitude Latitude Price
1 53.778274 -2.48129 147500
2 52.833819 -0.936527 182000
3 50.792457 0.046043 193000
4 51.476984 -0.612126 580000
5 51.460139 -0.01867 905000
6 52.235942 1.519404 641500
在此先感谢您的帮助,我在无数天都没有成功后才作为最后的手段询问。
你的代码有几个问题:
- 您检索到错误的地图。您混淆了
lon
和lat
值在get_map(location = c(-4,54.5), zoom = 6)
中的顺序
- 您正在以错误的方式调用
geom_point
部分中的数据。
以下代码修复了这些问题:
map <- get_map(location = c(51.5,0.2), zoom = 6)
ggmap(map) +
geom_point(data= data,
aes(x = Longitude, y = Latitude),
colour = "red",
alpha = 0.5,
size = 4)
并给你这张地图: