在 R / ggplot 中没有得到 geom_text

plotly not getting geom_text in R / ggplot

我有一个独立运行的 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 的地图以 API: 的形式出现在 plotly 站点上(是的,plotly 版本有更多城市,但那是因为我剥离了 csv 文件以进行堆栈溢出,所以它很容易重现)

但基本上,情节版本缺少非情节版本中的 geom_text(城市名称)。

read.csv() 有默认值 header=TRUE, sep=",",所以你不需要指定这些。

如果您有 运行 allcittmp <- read.csv("file.csv", colClasses="character"),则无需执行

for (i in c(1, 2)) {
    allcittmp[, i] <- as.character(allcittmp[, i])
}

因为这正是 colClasses="character" 所关心的。

我不太喜欢 mapcit3() 函数,它似乎在做一些处理然后一些绘图(?!)。

好的,我在 ggplotly 转换中发现了几个缺点。现在,我可以建议以下解决方法:

mappts2 <- ggplot(citiesC, aes(x=lon, y=lat)) +
  geom_text(size=10, aes(label=cityname), hjust=0, vjust=0) +
  borders(regions="canada", name="borders") +
  coord_equal() +
  geom_point(aes(text=cityname, size=indM), colour="red", alpha=0.5,
             name="cities", label=citiesC$cityname)
# Take a look
mappts2
# Yes, text is too big in ggplot2

first_version <- py$ggplotly(mappts2, kwargs=list(filename="map_text",
                                                  fileopt="overwrite"))
# Has the labels, misses the markers

my_account <- "marianne2"  # Replace with yours
account_url <- paste0("https://plot.ly/~", my_account, "/")
plot_number <- as.integer(gsub(account_url, "", first_version$response$url))

text_marker <- py$get_figure(my_account, plot_number)

text_marker$data[[1]]$mode
# Says "text"
text_marker$data[[1]]$mode <- "text+markers"
final_version <- py$plotly(text_marker$data,
                           kwargs=list(layout=text_marker$layout,
                                       fileopt="overwrite",
                                       filename="text_markers_mode"))

# Visit final_version$url
  • 大小转换并不完美,因此我将 size=2 替换为 size=10

  • 不幸的是,参数 hjustvjust 不受支持(此处忽略)。

  • geom_textgeom_point对同一个数据使用时,ggplotly应该设置mode="text+markers",目前不是R "plotly" 包(版本 0.5.25)。