我知道这个问题已经被问过很多次了,但我认为自从提出这些问题以来,plotly 的一些底层语法已经发生了变化。使用ggplotly()
创建分区统计图会给出长、纬度、组的默认工具提示,以及我的美学变量之一。我知道工具提示仅映射美学内容。我想要做的就是自定义工具提示,以便它显示我的数据集中的一些变量(包括那些未映射到美学的变量)而不是其他变量(例如坐标)。下面是一个可重现的示例以及我到目前为止所尝试过的。我遵循了在回答其他问题时给出的建议,但无济于事。
#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)
任何帮助深表感谢!
更新:我更新了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:我发现旧的工具提示(显示长和纬度)仅在悬停在边框上时才会出现,所以我摆脱了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)
- 产生以下奇怪的结果:
我开始认为像在绘图的 ggplot 转换中切换“图例”选项这样简单的事情是不可能的。
更新#4:所以我决定以另一种方式解决这个问题。相反,我决定自行更改变量名称。我会从一开始就这样做,除非我不确定 ggplot2 是否/如何接受带空格的变量 -我想通了`variable`
那可以工作。所以我继续重新标记变量。它有效-有点。问题是文本出现时带有引号。现在我需要一种方法来摆脱这些!有人有什么想法吗?谢谢!这是我在文本中引用的意思的图像: