2016-04-16 12 views
2

シェイプファイルを使用して作成された 'Total Population'と 'Population Density'の2つのマッププロットがあります。さて、私は合計人口から人口密度に変更することができるように、光沢のあるアプリを構築しようとしているので、プロットはそれに応じて変更する必要があります。Shinyアプリケーションを作成しようとするとエラーが発生する

警告:でエラーが発生しました:ggplot2クラス行列のデータと

を対処する方法を知りませんが、ここで私がしようとしてきたコードだ私は、コードを実行したとき、私は、次のエラーコードを得ました使用:

library(shiny) 
library(ggplot2) #Loading necessary libraries 


ui <- fluidPage(
    selectInput("mr", 
       label="Type of Plot", 
       choices=c("Total Population", "Density"), 
       selected="Total Population"), 
    plotOutput("curv") #Giving an input name and listing out types to choose in the Shiny app 
) 

server <- function(input, output){ 

    output$curv <- renderPlot({ 
    ggplot() + 
     geom_polygon(data = final.plot==input$mr, 
        aes(x = long, y = lat, group = group, fill = Population), 
        color = "black", size = 0.20) + 
     coord_map()+ 
     scale_fill_distiller(name="Population", palette = "YlGn")+ 
     labs(title="Population in Australia") 
    }) # Output with the data file and input string to change when input changes. 
} 

shinyApp(ui = ui, server = server) 

ご協力いただきありがとうございます。

UPDATE:

私のデータセットは、次のようになります。これは、 "final.plot" と呼ばれるデータフレームに格納されて

 id  long  lat order hole piece 
1 Ashmore and Cartier Islands 123.1169 -12.25333  1 FALSE  1 
2 Ashmore and Cartier Islands 123.1206 -12.25611  2 FALSE  1 
3 Ashmore and Cartier Islands 123.1222 -12.25861  3 FALSE  1 
4 Ashmore and Cartier Islands 123.1239 -12.25528  4 FALSE  1 
5 Ashmore and Cartier Islands 123.1258 -12.25333  5 FALSE  1 
6 Ashmore and Cartier Islands 123.1275 -12.25619  6 FALSE  1 
          group Population Density 
1 Ashmore and Cartier Islands.1   NA  NA 
2 Ashmore and Cartier Islands.1   NA  NA 
3 Ashmore and Cartier Islands.1   NA  NA 
4 Ashmore and Cartier Islands.1   NA  NA 
5 Ashmore and Cartier Islands.1   NA  NA 
6 Ashmore and Cartier Islands.1   NA  NA 

。他の州の人口と密度の値があります。私は、人口の静的なビジュアライゼーションを作成することができましたし、それは次のようになります。

enter image description here

あり密度のための同様のものだと私はので、これら二つの間で切り替えることができシャイニーアプリを作成しようとしていますそれに応じてプロットも変わります今、私は次のコードを試してみた:

library(shiny) 
library(ggplot2) #Loading necessary libraries 


ui <- fluidPage(
    selectInput("pop", 
       label="Type of Plot", 
       choices=c("Population", "Density"), 
       selected="Total Population"), 
    plotOutput("curv") #Giving an input name and listing out types to choose in the Shiny app 
) 

server <- function(input, output){ 

    output$curv <- renderPlot({ 
    ggplot() + 
     geom_polygon(data = final.plot, 
        aes(x = long, y = lat, group = group, fill = input$pop), 
        color = "black", size = 0.25) + 
     coord_map()+ 
     scale_fill_distiller(name="Density", palette = "Spectral")+ 
     labs(title="Population in Australia") 
    }) 
} 
shinyApp(ui = ui, server = server) 

をしかし、私は、「連続尺度に供給された離散値を」というエラーを取得しています。

UPDATE 2:ここで私が使用しているデータセットへのリンクです :私はあなたのコードを簡単に見ていたとの提案がいくつかありました Dataset

+0

まず、反応性のあるデータセットを作成して(入力$ mrに応じて)、ggplotに渡すことができます。 –

+2

この 'data = final.plot == input $ mr 'はうまく動作しない可能性があります。これはエラーメッセージを説明しています。私は他の問題(例えば、 'final.plot'とは何か、' mr'は何をするのか)を疑っていますが、あなたの例が[再現可能]でないと助けがありません(http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610)。 –

+0

こんにちは、私は投稿を編集しました。あなたのことを見てコメントすることができたのは素晴らしいことでしょう。ありがとう! –

答えて

1

1)あなたのデータを提供します(関数dputを使用することができます設定) - これは、あなたの質問に答える人は、単に例えばR.に貼り付けることができ、あなたのdata.frameのテキスト表現を書き込みます

dput(final.plot) 

これは、final.plot <-の接頭辞を付けてデータフレームに割り当てることができるテキスト出力を生成します。私はあなたのデータフレームを作り直して、dput()を使ってそれをテキストとして出力しました。今、他のユーザーが迅速&をカットすることができ、あなたのデータを貼り付けます。このデータセットは故障している

注 -

final.plot <- structure(list(id = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "Ashmore and Cartier Islands", class = "factor"), 
          long = c(123.1169, 123.1206, 123.1222, 123.1239, 123.1258, 123.1275), 
          lat = c(-12.25333, -12.25611, -12.25861, -12.25528, -12.25333, -12.25619), 
          order = 1:6, hole = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE), 
          piece = c(1L, 1L, 1L, 1L, 1L, 1L), 
          group = structure(c(1L, 1L, 1L, 1L, 1L, 1L), 
          .Label = "Ashmore and Cartier Islands.1", class = "factor"), 
          Population = c(NA, NA, NA, NA, NA, NA), 
          Density = c(NA, NA, NA, NA, NA, NA)), 
          .Names = c("id", "long", "lat", "order", "hole", "piece", "group", "Population", "Density"), 
          class = "data.frame", 
          row.names = c(NA, -6L)) 

下記参照エラー「連続尺度に供給された離散値は、」二つの問題によって引き起こされます。

i)集団と密度の両方の列にNAを渡しています。以下のデータフレームでは、これらの列にいくつかの(非現実的な)数値が追加され、プロットコードを単独で実行するとエラーが取り除かれます。

は、対話的に実行するときは、fill = input$popを埋めるために適切なデータを渡していないため、エラーが発生した玩具データセット

final.plot <- structure(list(id = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "Ashmore and Cartier Islands", class = "factor"), 
          long = c(123.1169, 123.1206, 123.1222, 123.1239, 123.1258, 123.1275), 
          lat = c(-12.25333, -12.25611, -12.25861, -12.25528, -12.25333, -12.25619), 
          order = 1:6, hole = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE), 
          piece = c(1L, 1L, 1L, 1L, 1L, 1L), 
          group = structure(c(1L, 1L, 1L, 1L, 1L, 1L), 
               .Label = "Ashmore and Cartier Islands.1", class = "factor"), 
          Population = c(1, 2, 3, 4, 5, 6), 
          Density = c(7, 3, 9, 1, 3, 6)), 
         .Names = c("id", "long", "lat", "order", "hole", "piece", "group", "Population", "Density"), 
         class = "data.frame", 
         row.names = c(NA, -6L)) 

II)を修正しました。選択した内容に応じて、final.plot $ Populationまたはfinal.plot $ Densityの値を渡す必要があります。代わりに、ドロップダウンボックスの出力 - 「Population」または「Density」を渡しています。これはrenderPlot以内にswitch文を使用して修正することができます。

# User input assigns appropriate data to selectedData which can be passed to other functions: 
    selectedData <- switch(input$pop, 
        "Population" = final.plot$Population, 
        "Density" = final.plot$Density) 

2)あなたは上記のあなたのアップデートに表示静的マップを作成したコードを提供することができればそれが役立つだろう。 Shinyコードをデバッグする際には、最初に非対話的に機能してからShinyに組み込むのが最も簡単です。私はRでマップ上にデータをプロットすることに慣れていないですが、私はあなたのアプリケーションがする必要があると考えている

library(ggplot2) #Loading necessary libraries 
library(mapproj) 
library(maps) 
ggplot() + 
    geom_polygon(data = final.plot, 
       [![aes(x = long, y = lat, group = group, fill = Population), 
       color = "black", size = 0.25) + 
    coord_map()+ 
    scale_fill_distiller(name="Density", palette = "Spectral")+ 
    labs(title="Population in Australia")` 

enter image description here

3):私は、下記のおプロットコードを抽出しようとしたが、それは期待される結果を生成されていませんライブラリ(mapproj)とライブラリ(maps)をロードして、必要な結果を得ることができます。ここでは上記のすべてを一緒に入れている:

library(shiny) 
library(ggplot2) #Loading necessary libraries 
#I added the two lines below: 
library(mapproj) 
library(map) 

ui <- fluidPage(

    selectInput("pop", 
       label="Type of Plot", 
       choices=list("Population", "Density"), 
       selected="Population"), #NOTE: Total Population changed to Population so that it selects correct default value 

    plotOutput("curv") #Giving an input name and listing out types to choose in the Shiny app 
) 

server <- function(input, output){ 

#You will probably want to simply import your dataframe final.plot using read.table etc:  
final.plot <- structure(list(id = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "Ashmore and Cartier Islands", class = "factor"), 
            long = c(123.1169, 123.1206, 123.1222, 123.1239, 123.1258, 123.1275), 
            lat = c(-12.25333, -12.25611, -12.25861, -12.25528, -12.25333, -12.25619), 
            order = 1:6, hole = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE), 
            piece = c(1L, 1L, 1L, 1L, 1L, 1L), 
            group = structure(c(1L, 1L, 1L, 1L, 1L, 1L), 
                .Label = "Ashmore and Cartier Islands.1", class = "factor"), 
            Population = c(1, 2, 3, 4, 5, 6), 
            Density = c(7, 3, 9, 1, 3, 6)), 
           .Names = c("id", "long", "lat", "order", "hole", "piece", "group", "Population", "Density"), 
           class = "data.frame", 
           row.names = c(NA, -6L)) 

    output$curv <- renderPlot({ 
    #Assign value of selectedData based upon user input: 
    selectedData <- switch(input$pop, 
        "Population" = final.plot$Population, 
        "Density" = final.plot$Density) 
    ggplot() + 
     geom_polygon(data = final.plot, 
        aes(x = long, y = lat, group = group, fill = selectedData), 
        color = "black", size = 0.25) + 
     coord_map()+ 
     scale_fill_distiller(name="Density", palette = "Spectral")+ 
     labs(title="Population in Australia") 
    }) 
} 
shinyApp(ui = ui, server = server) 

今あなたがする必要があるすべてはあなたの光沢のあるアプリでrenderPlot内の障害のあるコードのために、あなたの更新に示す静的マップを作成し、あなたのコードに置き換えています。

+0

うわー! Graemeに関する問題の詳細な説明をありがとう!それは今働いている。私はfill = selectedDataを変更しなければならず、それがトリックでした!これを私に見せる時間をとってくれてありがとう。 –

+0

答えとしてマークしました。また、選択肢に応じて伝説の名前を変更できるかどうか疑問に思っていましたか? –

+1

Arvindさん、 'scale_fill_distiller(name = "Density"、palette = "Spectral")の行を 'scale_fill_distiller(name = input $ pop、palette =" Spectral ")に変更してください+これは自動的にFigureの凡例を変更しますユーザーの入力に基づいています。 – Graeme

関連する問題