簡體   English   中英

在ggplot中保持通用圖例比例並閃亮

[英]Keeping a common legend scale in ggplot with shiny

我有一個閃亮的應用程序,它需要一個跨越8年的數據集,並按月進行拆分。 然后使用日期滑塊,僅使用該月的數據創建ggplot。

使用的ggplot是:

  ggplot() +
      geom_map(
        map = wrld, data = wrld, aes(long, lat, map_id=region),
        color = "grey", fill ="white", size=0.1
      ) +
      geom_point(
        data = dat(), aes(longitude, latitude, size = freq), 
        shape=21, fill = "red", color = "white", stroke=0.01
      ) +
      scale_size(name = "# IPs", label=scales::comma, range = c(1,10)) +
      ggalt::coord_proj("+proj=wintri") +
      ggthemes::theme_map() +
      theme(legend.justification = "center") +
      theme(legend.position = "bottom") +
      labs(title=paste("Post frequency ", shiny())) +
      annotate("text", x = 0, y = -60, label = "Bots")

反應性元素dat()shiny()是通過以下代碼創建的:

  shiny <- reactive({
    format(as.Date(input$slider), "%Y-%m")
  })
  dat <- reactive({
    count <- count(data.frame(splitted[[shiny()]]))
    counted <- ddply(count,"city_name",summarize,longitude=mean(longitude),latitude=mean(latitude),freq=sum(freq))
    counted <- counted[order(counted$freq),]
    counted[nrow(counted),1] <- "Bots"
    counted[nrow(counted),2:3] <- c(0,-55)
    counted
  })

顯示了splitted的示例:

> head(splitted['2012-07'])
$`2012-07`
                                         city_name longitude latitude   dateline       ipaddress                date
348812                                    Oak Lawn  -87.7516  41.7143 1341093604   67.175.106.62 2012-07-01 00:00:04
348813                                    Richmond  -84.2955  37.7546 1341093617    76.177.28.76 2012-07-01 00:00:17
348814                                    Lynbrook  -73.6741  40.6571 1341093624   72.68.156.111 2012-07-01 00:00:24
348815                                    Oak Lawn  -87.7516  41.7143 1341093680   67.175.106.62 2012-07-01 00:01:20
348816                                   Las Vegas -115.0669  36.1730 1341093690   24.253.64.114 2012-07-01 00:01:30
348817                                    Richmond  -84.2955  37.7546 1341093698    76.177.28.76 2012-07-01 00:01:38
348818                                   Cleveland  -83.7500  34.5839 1341093716  67.140.236.247 2012-07-01 00:01:56
348819                                   Las Vegas -115.0669  36.1730 1341093782   24.253.64.114 2012-07-01 00:03:02
348820                                    Lynbrook  -73.6741  40.6571 1341093826   72.68.156.111 2012-07-01 00:03:46
348821                                    Oak Lawn  -87.7516  41.7143 1341093886   67.175.106.62 2012-07-01 00:04:46

閃亮的應用正常運行,並且地圖正確更新。 問題是,圖例比例會更新每個地圖。 結果是,無論實際頻率水平如何,地圖都“看起來”相同。 看一下這兩個月: 在此處輸入圖片說明 在此處輸入圖片說明

點的大小是相同的,即使第一張圖片中的頻率最高為4,000,而第二張圖片中的頻率最高為80。這樣做的原因在每天查看每天的總帖子時可以理解時間: 在此處輸入圖片說明

我不是很了解我需要在ggplot上進行哪些更改,以便在所有時間段上強制使用相同的“大小比例”。

可以在https://pastebin.com/WjedbxT3上查看整個app.R文件

遺憾的是,我無法共享887,000點的數據集。

我設法弄清楚了。 我以為我已經嘗試過了。

scale_size(name = "# IPs", label=scales::comma, range = c(2,25), limits = c(1,5000))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM