使用ggplot2编辑工具提示中的标签以用于绘制地图

问题描述:

我知道这个问题已被问了很多次,但是我认为一些问题的底层语法已经改变了,因为这些问题已经被提出。使用ggplotly()创建一个等值线图给出了我的美学的长,拉特,组和我的一个变量的默认工具提示。我明白,工具提示只会映射美学中的内容。我想要做的就是自定义工具提示,以便它显示我的数据集中的一些变量(包括那些未映射到美学的变量),而不是其他的(如坐标)。下面是一个可重复使用的例子,以及我迄今为止所尝试的内容。我根据其他问题给出的建议无济于事。使用ggplot2编辑工具提示中的标签以用于绘制地图

#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 
[email protected]$example1<-sample(seq(from = 1, to = 100, by = 1), size = 11, replace = TRUE) 
[email protected]$example2<-sample(seq(from = 1, to = 100, by = 1), size = 11, replace = TRUE) 
[email protected]$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)我不能重命名变量除了它们的列名以外的。数据集(例如我便想“示例3‘实施例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) 

任何帮助非常感谢!

UPDATE:I updated 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 

现在发生的事简直让我感到困惑。这增加了一个独立于前一个的附加工具提示。这个新的工具提示正是我想要的,但是他们都出现 - 不是一次,而是如果我移动我的鼠标。看到两个屏幕截图下面:

This is the old tooltip from before that still lingers This is the new one and the only one I want to keep

通知那些工具提示来自相同的单元(蒂罗尔)。这可能是包中的错误吗?当显示其他图形(如时间序列)而不是地图时,不会发生这种情况。还要注意,我分配了标签“示例I”(或II或III),并且这不会显示在我添加的新工具提示上。

更新#2:我发现只有当鼠标悬停在边界上时,才会出现旧的工具提示(带有长和拉),因此我摆脱了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 
    [email protected]$example1<-sample(seq(from = 1, to = 100, by = 1), size = 11, replace = TRUE) 
    [email protected]$example2<-sample(seq(from = 1, to = 100, by = 1), size = 11, replace = TRUE) 
    [email protected]$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 

产生以下情节:

enter image description here

注意,“省”,是现在资本(这是不是之前)。诀窍是将text = paste("Province:", NAME_1)添加到美学。然而,当我尝试使用text2=paste("Example III:", example1)添加额外的标签更改,会出现以下情况:

enter image description here

请注意,它无法呈现的text2它呈现文本1相同的方式。所以不是我只是尝试在下面添加一个重复的无文本2,如:text=paste("Example III:", example1)哪位产生以下结果奇:

enter image description here

我开始想象的那样切换在“传奇”的选项,简单的事情情节的ggplot转换是不可能的。 更新#4:所以我决定采取另一种方式。相反,我决定自己更改变量名称。我会从一开始就这样做,除非我不确定是否/如何ggplot2接受带空格的变量 - 我想通过`variable`可以工作。所以我继续为变量重新标记。它的工作原理 - 金达。问题在于文字出现时带有围绕它们的引号。现在我需要一种方法来摆脱这些!任何想法的人?谢谢!这里是什么,我的意思是在文本报价图片:

enter image description here

+0

我在想,如果你曾经跑进试图与标记点的问题注释,然后有不同的工具提示变量。例如,按名称标记这些区域,然后使用工具提示显示统计信息。我一直在努力做到这一点,但ggplotly总是将标签作为工具提示文本,即使我明确地通过文本和标签。任何想法可以帮助吗? – jtanman

我是新来plotly太多,但使用ggplotly()时所遇到类似的问题,我GGPLOT2气泡图。我终于找到了一个适合我的解决方案,并认为它也可以帮助你,虽然我还没有尝试过使用choropleth地图。

您的第一个问题是自定义工具提示,以便显示数据集中的一些变量(包括那些未映射到美学的变量)。
在您的更新#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"))添加其他变量。工具提示中显示的变量顺序将与tooltip参数中指定的顺序相同。我发现这个记录在这里:https://github.com/ropensci/plotly/blob/master/R/ggplotly.R

该解决方案,使用R 3.1.1和3.4.13 plotly工作(原则上)我

+0

你是否知道是否有可能摆脱显示在鼠标悬停在其他像这个问题的边界的aes映射上的工具提示?例如,在我的ggplot中,我在数据点旁边有标签,并按照您在示例中推荐的方式构建了我的文本映射: ggplot(randomData,aes(x,y,label = ip_country,text = paste ( '国家:',ip_country, '
', 'X:',X))+ geom_point()+ geom_text(AES(X = X-1)) 然后,当我转换使用ggplotly,文本标签也有悬停工具提示,只显示文本标签,而实际的数据点将显示正确的悬停工具提示。 – jtanman