【问题标题】:Error at scale_x_date in ggmapggmap 中 scale_x_date 的错误
【发布时间】: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_pointx 轴。您是否要按十年为geom_points 着色?
  • 是的,我正在尝试按十年绘制点。谢谢!

标签: r ggplot2 ggmap


【解决方案1】:

这应该是一个开始。我必须添加 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

【讨论】:

  • 是否可以显示像 1980、1990、2000、2010 这样的十年?非常感谢!
  • 可以在这个脚本中使用“scale_x_date”吗?
【解决方案2】:

如前所述,您不能使用 scale_x_date,因为这会影响 x 轴,即经度,并且您正在尝试修改分组变量。

您可以通过正则表达式替换数字来显示几十年。可能有更好的东西,但您可以在前面的代码中定义eq$decade,如下所示,它应该显示适当的图例:

eq$decade <- as.factor(sub('([1-9][0-9][0-9])[0-9]','\\10',as.character(eq$year)))

【讨论】:

    猜你喜欢
    • 2014-09-30
    • 2013-11-18
    • 1970-01-01
    • 2012-06-15
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多