I have a trouble about the tooltip function in ggvis for my choropleth plot.I have attached my code below. The code is running good with no tooltip function. But if I add tooltip, only the land of new jersey left(see pic). The other states are missing. Any one can help to have a look and how can I fix it to get the other parts back with tooltip function added? Thanks a lot!
对于我的等值线图,我在ggvis中有一个关于工具提示函数的麻烦。我在下面附上了我的代码。代码运行良好,没有工具提示功能。但是如果我加上工具提示,就只剩下新泽西州的土地了(见图)。其他州也不见踪影。任何人都可以帮助查看,我如何修复它,使其他部分回到工具提示功能添加?谢谢!
###my data for example
mapdata<-data.frame(
state=c("alabama","alaska","arizona","arkansas","california","colorado","connecticut","delaware","florida","georgia","hawaii","idaho","illinois","indiana","iowa","kansas","kentucky","louisiana","maine","maryland","massachusetts","michigan", "minnesota","mississippi","missouri","montana","nebraska","nevada","new hampshire","new jersey","new mexico","new york","north carolina","north dakota","ohio","oklahoma", "oregon","pennsylvania","rhode island","south carolina","south dakota","tennessee","texas","utah","vermont","virginia","washington","west virginia","wisconsin","wyoming"),
revenue=runif(50,min=100,max=9000))
library(rgdal)
library(ggplot2)
library(ggvis)
url <- "http://www2.census.gov/geo/tiger/GENZ2014/shp/cb_2014_us_state_20m.zip"
tf <- tempfile()
td <- tempdir()
download.file(url,tf, mode="wb")
unzip(tf, exdir=td)
usa <- readOGR(dsn=td, layer="cb_2014_us_state_20m")
shp <- usa[(!usa$STUSPS %in% c("AK","HI")),]
df<- fortify(shp)
df<- merge(df,cbind(id=rownames(shp@data),shp@data),by="id")
df$state <- tolower(df$NAME)
df<- merge(df,mapdata,by="state")
df<- df[order(df$order),]
####function for tooltip
gg<-df
gg$id<-1:nrow(gg)
values<-function(x){
if(is.null(x)) return(NULL)
row<-gg[gg$id==x$id,]
paste0("State: ",row$state,"<br />",
"Revenue: ", row$revenue, "<br />")
}
####ggvis choropleth
gg %>%
group_by(group) %>%
ggvis(~long, ~lat, key:=~id) %>%
hide_axis("x") %>%
hide_axis("y")%>%
add_tooltip(values,"hover")%>%
layer_paths(fill= ~revenue)
1 个解决方案
#1
4
I was looking around a bit and realized that since paths are made from information from multiple rows of a dataset, it doesn't make sense to try to use a unique row id for the tooltip like we usually do with key
.
我仔细查看了一下,发现由于路径是由来自数据集的多行信息组成的,所以尝试像使用key那样为工具提示使用唯一的行id是没有意义的。
Then I saw this very nice ggvis tutorial, and the way forward became clearer to me. You need to base the tooltip on your grouping variable, and then somehow winnow the info from the many rows in that group down to a single value. Among other options, you could do this by pulling out just the first row from the group or by using just the unique
value when making your in your tooltip function.
然后我看到了这个非常棒的ggvis教程,在我看来,前进的道路变得更加清晰。您需要将工具提示以分组变量为基础,然后以某种方式将信息从该组中的许多行压缩为单个值。在其他选项中,您可以从组中取出第一行,或者在创建工具提示函数时使用惟一值。
Here's examples of what I mean, using the variable group
that you use in group_by
to pull out the pertinent info.
下面是我的意思,使用group_by中使用的变量组提取相关信息。
First, using head
to get just the first row:
首先,用head表示第一行:
values = function(x){
if(is.null(x)) return(NULL)
row = head(gg[gg$group == unique(x$group), ], 1)
paste0("State: ", row$state,"<br />",
"Revenue: ", row$revenue, "<br />")
}
Alternatively, just pull the unique
values of the variables you want to display (there is one per state/group). This works OK in your case because you are only displaying two values:
或者,只需提取要显示的变量的惟一值(每个状态/组有一个)。这在你的例子中是可行的,因为你只显示两个值:
values2 = function(x){
if(is.null(x)) return(NULL)
row = gg[gg$group == unique(x$group), ]
paste0("State: ", unique(row$state),"<br />",
"Revenue: ", unique(row$revenue), "<br />")
}
Now take the key
argument out of ggvis
and use one of the new tooltip functions. Now the interactive graph appears to work fine.
现在从ggvis中取出关键参数,使用一个新的工具提示函数。现在,交互式图形看起来运行良好。
gg %>%
group_by(group) %>%
ggvis(~long, ~lat) %>%
hide_axis("x") %>%
hide_axis("y")%>%
add_tooltip(values, "hover")%>%
layer_paths(fill= ~revenue)
#1
4
I was looking around a bit and realized that since paths are made from information from multiple rows of a dataset, it doesn't make sense to try to use a unique row id for the tooltip like we usually do with key
.
我仔细查看了一下,发现由于路径是由来自数据集的多行信息组成的,所以尝试像使用key那样为工具提示使用唯一的行id是没有意义的。
Then I saw this very nice ggvis tutorial, and the way forward became clearer to me. You need to base the tooltip on your grouping variable, and then somehow winnow the info from the many rows in that group down to a single value. Among other options, you could do this by pulling out just the first row from the group or by using just the unique
value when making your in your tooltip function.
然后我看到了这个非常棒的ggvis教程,在我看来,前进的道路变得更加清晰。您需要将工具提示以分组变量为基础,然后以某种方式将信息从该组中的许多行压缩为单个值。在其他选项中,您可以从组中取出第一行,或者在创建工具提示函数时使用惟一值。
Here's examples of what I mean, using the variable group
that you use in group_by
to pull out the pertinent info.
下面是我的意思,使用group_by中使用的变量组提取相关信息。
First, using head
to get just the first row:
首先,用head表示第一行:
values = function(x){
if(is.null(x)) return(NULL)
row = head(gg[gg$group == unique(x$group), ], 1)
paste0("State: ", row$state,"<br />",
"Revenue: ", row$revenue, "<br />")
}
Alternatively, just pull the unique
values of the variables you want to display (there is one per state/group). This works OK in your case because you are only displaying two values:
或者,只需提取要显示的变量的惟一值(每个状态/组有一个)。这在你的例子中是可行的,因为你只显示两个值:
values2 = function(x){
if(is.null(x)) return(NULL)
row = gg[gg$group == unique(x$group), ]
paste0("State: ", unique(row$state),"<br />",
"Revenue: ", unique(row$revenue), "<br />")
}
Now take the key
argument out of ggvis
and use one of the new tooltip functions. Now the interactive graph appears to work fine.
现在从ggvis中取出关键参数,使用一个新的工具提示函数。现在,交互式图形看起来运行良好。
gg %>%
group_by(group) %>%
ggvis(~long, ~lat) %>%
hide_axis("x") %>%
hide_axis("y")%>%
add_tooltip(values, "hover")%>%
layer_paths(fill= ~revenue)