我在ggmap中遇到了scale_x_date的一些问题。 感谢大家。 町
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
答案 0 :(得分:0)
这应该是一个开始。我必须添加fileEncoding="latin1"
才能在我的系统上读取文件。
eq <- read.table("https://dl.dropbox.com/u/8686172/eq.csv", sep="\t",
header=TRUE, stringsAsFactors=FALSE, fileEncoding="latin1")
# this is a more compact way to extract lat/lon as a number
eq$latitude <- as.numeric(gsub("[ NE]*", "", eq$latitude))
eq$longitude <- as.numeric(gsub("[ NE]*", "", eq$longitude))
eq$year <- as.factor(substr(eq$date,1,4))
eq$date <- ymd_hm(eq$date)
# this does the breaks by decade
eq$decade <- cut(eq$date, breaks="10 years")
# we use those breaks in the "colour=..."
gg <- ggmap(get_googlemap(center = 'korea', zoom=6, maptype='terrain'),extent='device')
gg <- gg + geom_point(aes(x=longitude, y=latitude, size=power, colour=decade),
data = eq, alpha = .7)
gg <- gg + geom_density2d(aes(x=longitude, y=latitude, z=date), data=eq)
gg
答案 1 :(得分:0)
如前所述,您不能使用scale_x_date
,因为这会影响x轴,这是经度并且您正在尝试修改分组变量。
你可以用正则表达式代替数字来表示几十年。可能有更好的东西,但您可以在前面的代码中定义eq$decade
,如下所示,它应该显示相应的图例:
eq$decade <- as.factor(sub('([1-9][0-9][0-9])[0-9]','\\10',as.character(eq$year)))