【问题标题】:Edit labels in tooltip for plotly maps using ggplot2 in r在 r 中使用 ggplot2 为绘图地图编辑工具提示中的标签
【发布时间】:2016-08-03 02:28:33
【问题描述】:

我知道这个问题已经被问过很多次了,但我认为 plotly 的一些基本语法在被问到这些问题后已经发生了变化。使用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”标记为“Example 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)

非常感谢任何帮助!

更新:我通过从 github 而不是 CRAN 安装更新了 plotly。使用这个更新的版本 (4.0.0) 我已经把它分开了。

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

现在发生的事情让我很困惑。这增加了一个与前一个分开的额外工具提示。这个新的工具提示正是我想要的,但是它们都出现了——不是一次出现,而是如果我移动鼠标。请看以下两张截图:

请注意,这些工具提示来自同一个单位 (Tirol)。这可能是包中的错误吗?当显示其他图形(例如时间序列而不是地图)时,不会发生这种情况。另请注意,我分配了标签“示例 I”(或 II 或 III),这不会显示在我添加的新工具提示中。

更新 #2:我发现旧的工具提示(显示了 long 和 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

这会产生以下情节:

请注意,“Province”现在大写(以前不是)。诀窍是将text = paste("Province:", NAME_1) 添加到美学中。但是,当我尝试使用 text2=paste("Example III:", example1) 添加其他标签更改时,会发生以下情况:

请注意,它无法以与呈现 text1 相同的方式呈现 text2。因此,我只是尝试添加一个没有 text2 的副本,如下所示:text=paste("Example III:", example1) - 这会产生以下奇怪的结果:

我开始认为在 plotly 的 ggplot 转换中切换“图例”选项这样简单的事情是不可能的。 更新#4:所以我决定用另一种方式来解决这个问题。相反,我决定自己更改变量名。我会从一开始就这样做,除了我不确定 ggplot2 是否/如何接受带空格的变量 - 我发现 `variable` 可以工作。所以我继续并重新标记了变量。它有效-有点。问题是文本出现时带有引号。现在我需要一种方法来摆脱这些!!!有什么想法吗?谢谢!这是我在文本中引用的意思的图像:

【问题讨论】:

  • 我想知道您是否遇到过尝试使用注释标记点然后为工具提示设置不同变量的问题。例如,通过名称标记这些区域,然后让工具提示显示统计信息。我一直在尝试这样做,但 ggplotly 始终将标签作为工具提示文本,即使我明确传递了文本和标签。有什么可以帮助的想法吗?

标签: r ggplot2 interactive plotly choropleth


【解决方案1】:

我也是 plotly 新手,但在使用 ggplotly() 时,我的 ggplot2 气泡图也遇到了类似的问题。我终于找到了一个适合我的解决方案,并认为它也可能对您有所帮助,尽管我还没有尝试将它用于等值线图。

您的第一个问题是自定义工具提示,以便显示数据集中的一些变量(包括未映射到美学的变量)。
在您的 UPDATE#3 中,您将:text = paste("Province:", NAME_1) 引入您的 aes。如果要添加第二行自定义变量或文本,只需继续将其添加到括号中:text = paste("Province:", NAME_1, "Example III:", example1) 要在两者之间添加换行符,请在您想要换行符的位置添加 &lt;br&gt;,例如:@ 987654326@

您的第二个问题是自定义工具提示,使其不显示其他(默认)变量(映射到美学,例如坐标)。
我发现ggplotly() 函数的这个非常简单的添加对我有用:ggplotly(gg, tooltip = c("text")) 在我的情况下,这删除了工具提示中显示的所有默认变量,只显示了上面text 自定义指定的变量.您可以通过执行ggplotly(gg, tooltip = c("text","x")) 重新添加其他变量。工具提示中显示的变量顺序将与tooltip 参数中指定的顺序相同。我发现这里记录了这一点:https://github.com/ropensci/plotly/blob/master/R/ggplotly.R

这个解决方案(原则上)对我使用 R 3.1.1 和 plotly 3.4.13 有效

【讨论】:

  • 你知道是否有可能摆脱悬停在像这个问题的边界这样的其他 aes 映射时出现的工具提示?例如,在我的 ggplot 中,我在数据点旁边有标签,并按照您在示例中推荐的方式构建了我的文本映射:ggplot(randomData, aes(x, y, label=ip_country, text=paste(' Country:', ip_country, '
    ', 'x:', x)) + geom_point() + geom_text(aes(x=x-1)) 然后当我使用ggplotly转换时,文本标签也有悬停仅显示文本标签的工具提示,而实际数据点将显示正确的悬停工具提示。
  • 我有同样的问题,我无法摆脱工具提示中的默认轴信息,即使通过执行 c("text") 我可以看到我的新文本,但我仍然看到默认一个。你知道为什么会这样吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-20
  • 1970-01-01
相关资源
最近更新 更多