gpt4 book ai didi

r - 在 R/ggplot 中没有得到 geom_text

转载 作者:行者123 更新时间:2023-12-03 16:44:08 25 4
gpt4 key购买 nike

我有一个 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 的版本: map without plotly
而带有 plotly 的 map 作为 API 出现在 plotly 站点上: map with plotly API (是的, plotly 版本有更多的城市,但那是因为我剥离了 csv 文件以进行堆栈溢出,因此很容易重现)

但基本上 plotly 版本缺少 geom_text (城市名称)在非 plotly 版本中。

最佳答案

好的,我在 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=2size=10 .
  • 不幸的争论 hjustvjust不支持(此处忽略)。
  • geom_textgeom_point用于相同的数据,ggplotly应该设置 mode="text+markers" ,目前在 R“plotly”包(版本 0.5.25)中不是这种情况。
  • 关于r - 在 R/ggplot 中没有得到 geom_text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29068867/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com