使用ggmap的Interactive R Markdown文档

时间:2015-01-27 15:08:26

标签: r ggplot2 markdown shiny ggmap

我正在使用Manuel Amunategui在http://amunategui.github.io/ggmap-example/处的ggmap教程。这是对ggmap包的一个很好的介绍,谢天谢地我理解他的教程。

但是,我也试图通过R markdown使这种材料互动。当我运行下面的文档时,由于某种原因,地图的渲染质量非常低。在我的标准.R脚本中,生成的图像更好。关于什么可能导致质量的巨大差异的任何想法?

此外,在R Markdown中,是否可以自定义图像尺寸以及放置?我特别感兴趣的是让地图更大和/或并排显示另一张地图。

第一段代码只是为了让你的手掌握数据。

#install.packages("RCurl"); install.packages("xlsx"); install.packages("zipcode"); install.packages("ggmap")
library(RCurl)
library(xlsx)

# NOTE if you can't download the file automatically, download it manually at:
#'http://www.psc.isr.umich.edu/dis/census/Features/tract2zip/'
urlfile <-'http://www.psc.isr.umich.edu/dis/census/Features/tract2zip/MedianZIP-3.xlsx'
destfile <- "census20062010.xlsx"
download.file(urlfile, destfile, mode="wb")
census <- read.xlsx2(destfile, sheetName = "Median")
#census <- read.xlsx2(file = "census20062010.xlsx", sheetName = "Median")
head(census)

# clean up data
# census <- census[c('Zip','Median..', 'Pop')]
names(census) <- c('Zip','Median', 'Pop')
census$Median <- as.character(census$Median)
census$Median <- as.numeric(gsub(',','',census$Median))
census$Pop <- as.numeric(gsub(',','',census$Pop))
head(census)

# get geographical coordinates from zipcode
library(zipcode)
data(zipcode)
census$Zip <- clean.zipcodes(census$Zip)
census <- merge(census, zipcode, by.x='Zip', by.y='zip')
census$location <- paste0(census$city, ", ", census$state)
names(census) <- sapply(names(census), tolower)

# saved census to census.rdata at this point...

下面的下一段代码是markdown文件中的内容。

```{r, message=FALSE, echo=FALSE}
library(ggmap)
library(ggplot2)
load("census.rdata")

inputPanel(
  textInput("loc", label = "Location", value = "Orlando, FL"),

  sliderInput("zoom", label = "Zoom Level",
              min = 1, max = 12, value = 10, step = 1)

)


renderPlot({
  census2 <- census[census$location == input$loc,]
  map <- get_map(location = input$loc, 
           zoom = input$zoom, 
           maptype = 'roadmap',
           source = 'google',
           color = 'color',
           filename = "ggmapTemp")

  print(ggmap(map) + 
    geom_point(
    aes(x=longitude, y=latitude, 
        show_guide = TRUE, size=Median), 
    data=census2, colour = I('red'), na.rm = T)
    )

})
```

感谢您的帮助!

0 个答案:

没有答案