簡體   English   中英

使用r中的ggplot2在工具提示中編輯用於繪制地圖的標簽

[英]Edit labels in tooltip for plotly maps using ggplot2 in r

我知道這個問題已被多次詢問,但我認為,自從提出這些問題以來,情節的一些基本語法已經改變了。 使用ggplotly()創建一個等值區域貼圖,從我的美學中提供了long,lat,group和我的一個變量的默認工具提示。 我知道工具提示只映射了美學中的什么。 我想要做的就是自定義工具提示,以便在我的數據集中顯示一些變量(包括那些未映射到美學的變量)而不顯示其他變量(例如坐標)。 下面是一個可重復的例子,到目前為止我嘗試過的。 我跟着回答其他問題的建議無濟於事。

#Load dependencies
library(rgeos)
library(stringr)
library(rgdal)
library(maptools)
library(ggplot2)
library(plotly)

#Function to read shapefile from website
dlshape=function(shploc, shpfile) {
  temp=tempfile()
  download.file(shploc, temp)
  unzip(temp)
  shp.data <- sapply(".", function(f) {
    fp <- file.path(temp, f)
    return(readOGR(".",shpfile))
  })
}

austria <- dlshape(shploc="http://biogeo.ucdavis.edu/data/gadm2.8/shp/AUT_adm_shp.zip", 
                   "AUT_adm1")[[1]]
#Create random data to add as variables
austria@data$example1<-sample(seq(from = 1, to = 100, by = 1), size = 11, replace = TRUE)
austria@data$example2<-sample(seq(from = 1, to = 100, by = 1), size = 11, replace = TRUE)
austria@data$example3<-sample(seq(from = 1, to = 100, by = 1), size = 11, replace = TRUE)

#Fortify shapefile to use w/ ggplot
austria.ft <- fortify(austria, region="ID_1")
data<-merge(austria.ft, austria, region="id", by.x = "id", by.y = "ID_1")

#Save as ggplot object    
gg<-ggplot(data, aes(x = long, y = lat, fill = example1, group = group)) + 
  geom_polygon() + geom_path(color="black",linetype=1) + 
  coord_equal() +
  scale_fill_gradient(low = "lightgrey", high = "darkred", name='Index') +xlab("")+ylab("") + 
  theme(axis.text = element_blank(), 
        axis.title = element_blank(), 
        axis.ticks = element_blank()) + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
        panel.background = element_blank(), axis.line = element_line(colour = "black"))

#Plot using ggplotly
ggplotly(gg)

從這里開始,我嘗試了兩種不同的方法。 最成功的方法之一就是讓我在那里。 我可以將新變量添加到工具提示中但我不能做兩件事:1)我無法擺脫默認情況下已經顯示的其他變量(來自美學)和2)我不能將變量重命名為除了列名以外的變量數據集(例如,我想將“example3”標記為“示例III”)。這是方法:

#Save as a new ggplot object except this time add ``label = example3`` to the aesthetics
 gg2<-ggplot(data, aes(x = long, y = lat, fill = example1, group = group, label = example3)) + 
  geom_polygon() + geom_path(color="black",linetype=1) + 
  coord_equal() +
  scale_fill_gradient(low = "lightgrey", high = "darkred", name='Index') +xlab("")+ylab("") + 
  theme(axis.text = element_blank(), 
        axis.title = element_blank(), 
        axis.ticks = element_blank()) + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
        panel.background = element_blank(), axis.line = element_line(colour = "black"))

#Save as plotly object then plot
gg2 <- plotly_build(gg2)
gg2

我也嘗試添加以下內容但它沒有做任何事情:

gg2$data[[1]]$text <- paste("Example I:", data$example1, "<br>",
                           "Example II:", data$example2, "<br>",
                           "Example III:", data$example3)

任何幫助深表感謝!

更新:我更新plotly從github上,而不是安裝CRAN。 使用這個更新版本(4.0.0)我已經把它分開了。

gg2$x$data[[2]]$text <- paste("Example I:", data$example1, "<br>",
                             "Example II:", data$example2, "<br>",
                             "Example III:", data$example3)
gg2

現在發生的事情簡直讓我感到困惑。 這會增加一個與前一個工具提示分開的額外工具提示。 這個新的工具提示正是我想要的,但是它們都出現了 - 不是一次但是如果我移動我的鼠標。 請參閱以下兩個屏幕截圖:

這是之前仍然存在的舊工具提示 這是新的,也是我唯一要保留的

請注意,這些工具提示來自同一個單元(蒂羅爾)。 這可能是包中的錯誤嗎? 顯示其他圖形(例如時間序列而不是地圖)時不會發生這種情況。 另請注意,我指定了標簽“示例I”(或II或III),但這並未在我添加的新工具提示中顯示。

更新#2:我發現舊的工具提示(顯示長和lat)僅在懸停在邊框上時出現,因此我擺脫了geom_path(color="black",linetype=1)命令(以刪除邊框) )現在我成功解決了這個問題。 但是,我仍然無法修改工具提示中顯示的標簽。

更新#3:我想出了如何編輯標簽,但只有一個變量。 哪個是堅果! 這是我從開始到結束的工作流程:

 #Load dependencies
    library(rgeos)
    library(stringr)
    library(rgdal)
    library(maptools)
    library(ggplot2)
    library(plotly)

    #Function to read shapefile from website
    dlshape=function(shploc, shpfile) {
      temp=tempfile()
      download.file(shploc, temp)
      unzip(temp)
      shp.data <- sapply(".", function(f) {
        fp <- file.path(temp, f)
        return(readOGR(".",shpfile))
      })
    }

    austria <- dlshape(shploc="http://biogeo.ucdavis.edu/data/gadm2.8/shp/AUT_adm_shp.zip", 
                       "AUT_adm1")[[1]]
    #Create random data to add as variables
    austria@data$example1<-sample(seq(from = 1, to = 100, by = 1), size = 11, replace = TRUE)
    austria@data$example2<-sample(seq(from = 1, to = 100, by = 1), size = 11, replace = TRUE)
    austria@data$example3<-sample(seq(from = 1, to = 100, by = 1), size = 11, replace = TRUE)

    #Fortify shapefile to use w/ ggplot
    austria.ft <- fortify(austria, region="ID_1")
    data<-merge(austria.ft, austria, region="id", by.x = "id", by.y = "ID_1")

    #Save as ggplot object    
    gg<-ggplot(data, aes(x = long, y = lat, fill = example1, group = group, text = paste("Province:", NAME_1))) + 
      geom_polygon(color="black", size=0.2) + 
      coord_equal() +
      scale_fill_gradient(low = "lightgrey", high = "darkred", name='Index') +xlab("")+ylab("") + 
      theme(axis.text = element_blank(), 
            axis.title = element_blank(), 
            axis.ticks = element_blank()) + 
      theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
            panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
      theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
            panel.background = element_blank(), axis.line = element_line(colour = "black"))


gg <- plotly_build(gg)
gg

這產生了以下情節:

在此輸入圖像描述

請注意,“省”現在已大寫(之前不是)。 訣竅是將text = paste("Province:", NAME_1)到美學中。 但是,當我嘗試使用text2=paste("Example III:", example1)添加其他標簽更改時,會發生以下情況:

在此輸入圖像描述

請注意,它無法以與呈現text1相同的方式呈現text2。 所以我只是嘗試添加沒有text2的副本,如下所示: text=paste("Example III:", example1) - 它產生以下奇怪的結果:

在此輸入圖像描述

我開始認為在plotly的ggplot轉換中切換“圖例”選項這么簡單是不可能的。 更新#4:所以我決定采用另一種方式。 相反,我決定自己更改變量名稱。 我會從一開始就做到這一點,除了我不確定ggplot2是否/如何接受帶空格的變量-i想出可以工作的`variable` 所以我繼續並重新標記變量。 它有效--KINDA。 問題是文本出現時帶有引號。 現在我需要一種擺脫這些的方法! 任何人的想法? 謝謝! 這是我在文中引用的意思的圖像:

在此輸入圖像描述

我是新手,但在使用ggplotly()時,我的ggplot2氣泡圖遇到了類似的問題。 我終於找到了一個對我有用的解決方案,並認為它也可能對你有所幫助,雖然我還沒有嘗試過用於等值線圖。

您的第一個問題是自定義工具提示,以便顯示數據集中的一些變量(包括那些未映射到美學的變量)。
在您的更新#3中,您將: text = paste("Province:", NAME_1)引入您的aes。 如果要添加第二行自定義變量或文本,只需將其添加到括號中: text = paste("Province:", NAME_1, "Example III:", example1)在兩者之間添加換行符<br>在您想要休息的地方,例如: text = paste("Province:", NAME_1, "<br>", "Example III:", example1)

您的第二個問題是自定義工具提示,因此它不顯示其他(默認)變量(映射到美學,例如坐標)。
我發現ggplotly()函數很容易添加到我這里的技巧: ggplotly(gg, tooltip = c("text"))在我的例子中,這刪除了工具提示中顯示的所有默認變量並且只顯示那些用上面的text指定的自定義。 您可以通過執行ggplotly(gg, tooltip = c("text","x"))添加其他變量ggplotly(gg, tooltip = c("text","x"))工具提示中顯示的變量的順序將與tooltip參數中指定的順序相同。 我在這里記錄了這個: https//github.com/ropensci/plotly/blob/master/R/ggplotly.R

這個解決方案(原則上)使用R 3.1.1和圖3.4.13

暫無
暫無

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

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