R闪亮。使用ggmap时弹出错误

时间:2015-03-09 05:32:05

标签: r ggplot2 shiny ggmap

我有一个纬度和经度的数据集

place   latitude    longitude
w      28.844692    77.103305
x      28.750925    77.1963099
y      28.702134    77.2202666
z      28.716547    77.1704

我已经尝试过以下代码并且工作正常:

a <- coordinates[coordinates$place=="y",c("place","latitude","longitude")]
get_constituency <- get_map(c(a$longitude, a$latitude))
ggmap(get_constituency) + geom_point(aes(x = a$longitude, y = a$latitude, size=10), alpha = .5, col="red") + scale_size(range=c(3,5))

我正在使用闪亮来显示地图,目前我正在使用打印功能来显示地图

p <- ggmap(get_constituency) + 
  geom_point(aes(x = a$longitude, 
                 y = a$latitude, size=10), 
             alpha = .5, col="red") + 
  scale_size(range=c(3,5))
print(p, newpage = FALSE)

它返回:

  

eval(expr,envir,enclos)中的错误:object&#39; a&#39;找不到

附加的UI.R和Server.R代码

  

UI.R

library(shiny)

# Define UI
shinyUI(fluidPage(

  # Application title
  titlePanel("Analysis!"),

  sidebarLayout(position="left",

    # Sidebar with a slider input
    sidebarPanel(width="3",

    uiOutput("Constituency_dropdown", label="aaa")),



    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("voteshare_2015", height="1000px", width="1024px")
    )
  )
))
  

Server.R

library(shiny)
library(plyr)
library(ggplot2)
library(gridExtra)
library(gridBase)
library(reshape)
library(ggmap)
library(PerformanceAnalytics)

z=data.frame(x=1:10, y=11:20)
coordinates <- data.frame(place=c("w", "x", "y", "z"), 
                        latitude=c(28.844692, 28.750925, 28.702134, 28.716547),
                        longitude=c(77.103305,  77.1963099, 77.2202666, 77.1704),
                      stringsAsFactors=FALSE)


shinyServer(function(input, output) {

  # Constituency
  output$Constituency_dropdown  <- reactiveUI(function() {
    selectInput(inputId = "Constituency", label = "Constituency", c("w","x","y","z"))
  })

output$voteshare_2015 <- renderPlot({

plot.new()
gl <- grid.layout(2,2)
vp.1 <- viewport(layout.pos.col = 1, layout.pos.row = 1)
vp.2 <- viewport(layout.pos.col = 2, layout.pos.row = 1)
vp.3 <- viewport(layout.pos.col = c(1,2), layout.pos.row = 2)
pushViewport(viewport(layout=gl))

a <- coordinates[coordinates$place==as.character(input$Constituency),c("place","latitude","longitude")]


# First plot
pushViewport(vp.1)
par(new = TRUE, fig = gridFIG(), mar=c(0,0,0,0))
pie(z$x,z$y)
popViewport()

# Second plot
pushViewport(vp.2)
par(new = TRUE, fig = gridFIG(), mar=c(0,0,0,0))
pie(z$x,z$y)
popViewport()

# Your map
pushViewport(vp.3)
par(new = TRUE, fig = gridFIG(), mar=c(0,0,0,0))
#a <- coordinates[coordinates$place==as.character(input$Constituency),c("place","latitude","longitude")]
#op=plot(x=a$latitude, y=a$longitude)
 get_constituency <- get_map(c(a$longitude, a$latitude))

 op <-ggmap(get_constituency) + 
   geom_point(aes(x = a$longitude, 
                  y = a$latitude, size=10), 
             alpha = .5, col="red") + 
    scale_size(range=c(3,5))
print(op, newpage = FALSE)
popViewport()

  })
  } )  

请从R studio中清除所有全局环境变量并运行代码。上面的代码返回错误。无法识别问题。请帮忙

1 个答案:

答案 0 :(得分:1)

这很好用:

require("ggmap") # apparently needs rjson

coordinates <- data.frame(place=c("w", "x", "y", "z"), 
               latitude=c(28.844692, 28.750925, 28.702134, 28.716547),
               longitude=c(77.103305,  77.1963099, 77.2202666, 77.1704),
               stringsAsFactors=FALSE)

a <- coordinates[coordinates$place=="y",c("place","latitude","longitude")]
get_constituency <- get_map(c(a$longitude, a$latitude))
ggmap(get_constituency) + 
  geom_point(aes(x = a$longitude, 
                 y = a$latitude, size=10), 
             alpha = .5, col="red") + 
  scale_size(range=c(3,5))
p=ggmap(get_constituency) + 
  geom_point(aes(x = a$longitude,
                 y = a$latitude, size=10), 
             alpha = .5, col="red") + 
  scale_size(range=c(3,5))

print(p, newpage = FALSE)

new_delhi_ggmap

修改

OP正在寻找类似的东西:

require(ggplot2)
require(grid)
require(gridBase)
require(ggmap) 

z=data.frame(x=1:10, y=11:20)
coordinates <- data.frame(place=c("w", "x", "y", "z"), 
                        latitude=c(28.844692, 28.750925, 28.702134, 28.716547),
                        longitude=c(77.103305,  77.1963099, 77.2202666, 77.1704),
                      stringsAsFactors=FALSE)
a <- coordinates[coordinates$place=="y",c("place","latitude","longitude")]
get_constituency <- get_map(c(a$longitude, a$latitude))

# setup everything
plot.new()
gl <- grid.layout(2,2)
vp.1 <- viewport(layout.pos.col = 1, layout.pos.row = 1)
vp.2 <- viewport(layout.pos.col = 2, layout.pos.row = 1)
vp.3 <- viewport(layout.pos.col = 1, layout.pos.row = 2)
vp.4 <- viewport(layout.pos.col = 2, layout.pos.row = 2)
pushViewport(viewport(layout=gl))

# First plot
pushViewport(vp.1)
par(new = TRUE, fig = gridFIG(), mar=c(0,0,0,0))
pie(z$x,z$y)
popViewport()

# Second plot
pushViewport(vp.2)
par(new = TRUE, fig = gridFIG(), mar=c(0,0,0,0))
pie(z$x,z$y)
popViewport()

# Your map
pushViewport(vp.3)
op <-ggmap(get_constituency) + 
  geom_point(aes(x = a$longitude, 
                 y = a$latitude, size=10), 
             alpha = .5, col="red") + 
    scale_size(range=c(3,5))
print(op, newpage = FALSE)

np=ggmap(get_constituency) + 
  geom_point(aes(x = a$longitude,
                 y = a$latitude, size=10), 
             alpha = .5, col="red") + 
    scale_size(range=c(3,5))
print(np, newpage = FALSE)
popViewport()

# Your map
pushViewport(vp.4)
op <-ggmap(get_constituency) + 
  geom_point(aes(x = a$longitude, 
                 y = a$latitude, size=10), 
             alpha = .5, col="red") + 
    scale_size(range=c(3,5))
print(op, newpage = FALSE)

np=ggmap(get_constituency) + 
  geom_point(aes(x = a$longitude,
                 y = a$latitude, size=10), 
             alpha = .5, col="red") + 
    scale_size(range=c(3,5))
print(np, newpage = FALSE)
popViewport()