r - 用 ggplotly 转换 ggplot2:包错误
问题描述
我一直在 ggplot2 中构建一个小的散点图,以发布在一个闪亮的应用程序中,一切正常,但我想尝试使其具有交互性,例如查看每个点的值并能够根据需要调整地图的大小. 我看到有一个更简单的解决方案,而不是在 plotly 中重建另一个地图,它正在使用 ggplotly。问题是,在我运行 ggplotly 之后,我无法在 RStudio 或 Shiny 中查看地图,但也没有出现错误。
提前感谢您的帮助,如果答案很明显/简单,我深表歉意,但我无法弄清楚。
更新 1:就像 A.Suliman 所说,似乎生成了 ggplotly 地图,但只能在单独的窗口中查看,而不是在 RStudio 查看器中查看。但它仍然没有出现在应用程序中。
更新 2:MLavoie 指出我提供的代码适用于 R 版本 3.5.0,情节 4.7.1.9000 和 ggplot2 2.2.1.9000(因为他可以在 RStudio 和 Shiny 仪表板中查看情节)。我目前使用 R 3.5.1., plotly 4.8.0 & ggplot2 3.0.0。为了解决这个问题,我尝试将软件包降级到 4.7.1。和 2.2.1。使用 CRAN 存储库,它工作了很短的时间,但它开始产生错误,例如 ggplot2 需要为 ggplotly 使用开发版本(安装它当然更新我到 3.0.0.900 版本)并且闪亮的 pkg 需要更新。我也将软件包更新为开发版本。我尝试使用 R 版本 3.5.0,但它干扰了很多包并再次产生错误。我回去和第四次安装/删除不同版本的软件包,但似乎没有一个工作。
我的代码
对于 ui.R
library(shinydashboard)
library(shiny)
library(plotly)
library(ggplot2)
dashboardPage(
dashboardHeader(),
dashboardSidebar(
sidebarMenu(
sidebarSearchForm(textId = "searchbar", buttonId = "searchbtn", label = "Search..."),
menuItem("Test", tabName="state", icon = icon("dashboard")))
),
dashboardBody(
tabItems(
tabItem(tabName="state",
fluidRow(
box(title = "Products across the country", width = 12, status = "success", plotlyOutput("map01"))
)
)
)
)
)
对于服务器.R
library(shinydashboard)
library(shiny)
library(plotly)
library(ggplot2)
shinyServer(function(input, output) {
world_map <- map_data("world")%>%filter(region=="Portugal")
info = data.frame(read.csv("cities.csv", header = TRUE))
dput(info)
structure(list(name = structure(c(4L, 10L, 6L, 15L, 14L, 9L,
3L, 7L, 8L, 11L, 18L, 17L, 1L, 16L, 12L, 13L, 2L, 19L, 5L, 20L,
21L), .Label = c("Continente ID0912 - Espinho", "Continente ID8234 -
Guimaraes", "Continente ID9912 - Braga", "Continente ID9932 - Albufeira",
"Jumbo ID2441 - Lisbon", "Minipreco ID1120 - Almada", "Minipreco ID1150 -
Cacilhas",
"Minipreco ID1212 - Canelas", "Minipreco ID1231 - Beja", "Minipreco ID1332
- Alfena", "Minipreco ID5345 - Caparica", "Minipreco ID7772 - Fatima",
"Minipreco ID8723 - Gondomar",
"Minipreco ID8891 - Aveiro", "Pingodoce ID1002 - Amadora", "Pingodoce
ID4228 - Faro", "Pingodoce ID4778 - Cova da Piedade", "Pingodoce ID5426 -
Coimbra", "Pingodoce ID7734 - Lagos", "Pingodoce ID7734 - Nazare",
"Pingodoce ID9832 - Viana do Castelo"), class = "factor"), products =
c(86L, 53L, 77L, 89L, 61L, 65L, 60L, 43L, 72L, 34L, 41L, 88L, 44L, 23L,
67L, 87L, 45L, 56L, 19L,
87L, 53L), lat = c(37.09, 41.23, 38.68, 38.75, 40.65, 38.02,
41.55, 38.68, 41.08, 38.67, 40.22, 38.67, 41.01, 37.03, 39.62,
41.15, 41.44, 37.1, 38.72, 39.6, 41.71), long = c(-8.26, -8.52,
-9.16, -9.24, -8.66, -7.86, -8.43, -9.14, -8.59, -9.19, -8.43,
-9.15, -8.64, -7.94, -8.64, -8.52, -8.3, -8.68, -9.14, -9.06,
-8.83)), class = "data.frame", row.names = c(NA, -21L))
output$map01<- renderPlotly({
p = info %>% ggplot() + geom_polygon(data=world_map, aes(x=long, y=lat, group=group), fill="grey50", alpha=0.3) + geom_point(aes(x=long, y=lat, colour=products, size=products, group=name), alpha=0.6) + scale_size_continuous(range=c(1,10)) + theme_void() + coord_map() + guides(size=FALSE)
ggplotly(p, tooltip = c("group","colour"))
})
})
如果在单独的窗口中打开,此代码会创建地图,但不会在 Shiny 中显示。
解决方案
推荐阅读
- html - 在表中抓取特定数据
- php - 如何在单个字段 laravel 中存储多个值
- flutter - 颤振嵌套json解析与三个模型类
- python - 我在 beautifulsoup 中找不到表
- c++ - 头文件可以在主类中运行。这怎么可能?
- jquery - 我有两个操作链接和一个模型弹出窗口。有什么方法可以知道点击了哪个动作链接
- c# - 在控制台应用程序中使用 System.ComponentModel.DataAnnotations
- c++ - Gstreamer 与 UDP 网络捕获->修改->流管道错误
- c++ - 如果第一个元素包含任何零,那么我的程序就会以不希望的方式运行
- ios - 在 swift 中遇到关于暗模式下自定义颜色的问题