【发布时间】:2014-05-13 05:21:02
【问题描述】:
我在带有 scale_x_date 的 ggmap 中遇到了一些问题。 感谢大家。 赵,
这是 R 代码:
library(ggmap)
library(lubridate)
Sys.setlocale("LC_TIME", "C")
eq <- read.table("https://dl.dropbox.com/u/8686172/eq.csv", sep="\t",
header=T, stringsAsFactors=F)
eq$latitude <- unlist(strsplit(eq$latitude, " "))[seq(from=1,to=nrow(eq), by=2)]
eq$longitude <- unlist(strsplit(eq$longitude, " "))[seq(from=1,to=nrow(eq), by=2)]
eq$longitude <- as.double(eq$longitude)
eq$latitude <- as.double(eq$latitude)
eq$year <- as.factor(substr(eq$date,1,4))
eq$date <- ymd_hm(eq$date)
ggmap(get_googlemap(center='korea',zoom=6,maptype='terrain'),extent='device') +
geom_point(aes(x=longitude,y=latitude,size=power,colour=date),data=eq,alpha=.7) +
scale_x_date("10 years") +
geom_density2d(aes(x=longitude, y=latitude), data=eq)
结果:
> ggmap(get_googlemap(center='korea',zoom=6,maptype='terrain'),extent='device') +
+ geom_point(aes(x=longitude,y=latitude,size=power,colour=date),data=eq,alpha=.7) +
+ scale_x_date("10 years") +
+ geom_density2d(aes(x=longitude, y=latitude), data=eq)
Scale for 'x' is already present. Adding another scale for 'x',
which will replace the existing scale.
Error: Invalid input: date_trans works with objects of class Date only
错误似乎与 scale_x_date 有关。
> sessionInfo()
R version 3.1.0 (2014-04-10)
Platform: i386-w64-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=Korean_Korea.949 LC_CTYPE=Korean_Korea.949 LC_MONETARY=Korean_Korea.949
[4] LC_NUMERIC=C LC_TIME=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] mapproj_1.2-2 maps_2.3-6 lubridate_1.3.3 ggmap_2.3 ggplot2_0.9.3.1
loaded via a namespace (and not attached):
[1] colorspace_1.2-4 digest_0.6.4 grid_3.1.0 gtable_0.1.2
[5] MASS_7.3-31 memoise_0.2.1 munsell_0.4.2 plyr_1.8.1
[9] png_0.1-7 proto_0.3-10 Rcpp_0.11.1 reshape2_1.4
[13] RgoogleMaps_1.2.0.6 rjson_0.2.13 RJSONIO_1.2-0.2 scales_0.2.4
[17] stringr_0.6.2 tools_3.1.0
【问题讨论】:
-
您的问题是,当
x实际为longitude时,您正试图按“日期”缩放geom_point的x轴。您是否要按十年为geom_points着色? -
是的,我正在尝试按十年绘制点。谢谢!