我有一个 ggplot 可以自己正常工作。但是,当我尝试将其导入 plotly api 系统时,它geom_text
似乎不起作用 - 其他一切都有效。谁能帮我?
这是我的 R 版本 - R 版本 3.1.2 (2014-10-31) 和 plotly 版本 - 0.5.23
我使用的数据在 file.csv 中,如下所示:
Province,Community,General Shelters,General Beds,Mens Shelters,Mens Beds,Womens Shelters,Womens Beds,Youth Shelters,Youth Beds,Family Shelters,Family Beds,Total Shelters,Total Beds
New Brunswick,Saint John,0,0,1,35,1,10,0,0,0,0,2,45
Quebec,Montréal,7,114,9,916,12,259,17,197,1,7,45,"1,493"
Quebec,Québec City,3,49,2,102,1,12,2,15,0,0,8,178
Ontario,Toronto,4,250,13,"1,483",10,572,10,416,4,496,41,"3,217"
British Columbia,Vancouver,13,545,7,291,9,238,7,90,2,30,38,"1,194"
British Columbia,Victoria,1,84,1,21,1,25,1,10,1,5,5,145
这是我的完整代码:
library(ggplot2)
library(zoo)
library(DAAG)
library(mapdata) #for canada map from worldhires database
library(ggmap)
library("plotly") # for plotly
homeless <- function()
{
allcit <- NULL
#read csv
allcittmp <- read.csv("file.csv", sep=",", header=TRUE, colClasses="character")
#cast data to proper format from character for both data frames
allcittmp[,1] <- as.character(allcittmp[,1])
allcittmp[,2] <- as.character(allcittmp[,2])
allcittmp[,13] <- as.integer(allcittmp[,13])
allcittmp[,14] <- as.integer(gsub(",","",allcittmp[,14]))
#get only relevant columns to a new data frame
allcit <- allcittmp[,c(1,2,13,14)]
#delete temp data frames for hygiene
allcittmp <- NULL
#give better colnames
colnames(allcit) <- c("prov","community","totshelters","totbeds")
#concatenate col2,1 to get city, province
allcit$hcity <- paste(allcit$community,allcit$prov, sep=", ")
#clean up NA's
allcit <- na.omit(allcit)
plmap <- mapcit3(allcit$hcity, allcit$totshelters, allcit$community)
#the following two lines commented out makes plotly graph
#everything is fine except that the city names don't show up
#py <- plotly()
#py$ggplotly(plmap)
}
mapcit3 <- function(citiesM, indM, cityname)
{
#concatenate Canada to city names, to be safe and not pick up similar US cities:
citiesM <- paste(citiesM,", Canada", sep="")
freqM <- data.frame(citiesM, indM, cityname) #make dataframe
lonlat <- geocode(citiesM) #courtesy of google, logitude, lattitude (gives two var's lon, lat among others)
citiesC <- cbind(freqM,lonlat) #make new df with long/lat
mappts2 <- ggplot(citiesC, aes(lon, lat)) +
borders(regions="canada", name="borders") +
coord_equal() +
geom_point(aes(text=cityname, size=indM), colour="red", alpha=1/2, name="cities", label=citiesC$cityname) +
geom_text(size=2, aes(label=cityname),hjust=0, vjust=0)
return(mappts2)
}
附加为 map1_without_plotly.png 的是没有 plotly 的版本:
并且带有 plotly 的地图作为 API 出现在 plotly 站点上:(
是的,plotly 版本有更多城市,但那是因为我剥离了 csv 文件以防止堆栈溢出,所以它很容易重现)
但基本上情节版本缺少geom_text
非情节版本中的(城市名称)。