我正在R Shiny中绘制一个气泡图,其中显示了德国不同城市位置(在数据中称为“ Stadt”)的销量。因此,我基本上使用了德国默认贴图(.rds),并用气泡的绘图层对其进行了补充。 我要使用ggplot而不是ggplot,因为它具有更多默认功能,这些功能对于在地图上排列显示的信息非常有用。 但是,当我应用plotly函数时,该图显示出变形很大的国家形状。侧面显示的气泡和地图图例可以正常工作。
我知道可以通过ggplot括号内的相应命令来固定高度和尺寸,但是在这种情况下,这不会导致所需的结果。 地图应通过保持正确的宽高比来适应窗口大小。 除了通过反复试验手动确定尺寸和宽度之外,还有其他解决方案吗?
基本代码如下:
library(shiny)
library(tidyverse)
library(ggplot2)
library(plotly)
##pool package necessary for dbpool function in order to access database via function in 'database_init.R'
library(pool)
### load and preprocess the German geo map polygons. Download here http://biogeo.ucdavis.edu/data/gadm2.8/rds/DEU_adm1.rds
map_data_sp2 <- readRDS("gadm36_DEU_2_sp.rds")
map_data_1 <- fortify(map_data_sp2, region = "NAME_1")
# link to database and declaration as 'pool'
pool <- dbPool(
drv = RMySQL::MySQL(),
dbname = "rlyshiny_",
host = "85.214.74.4",
username = "xxxxxxxx",
password = "xxxxxxxxxxxxxxxx")
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
sidebarPanel()
),
dashboardBody(
plotlyOutput("heatmap")
)
)
server <- function(input, output, session) {
output$heatmap <- renderPlotly({
locations <- dbReadTable(pool,"Locations")
##tbl extrahiert salesdata aus pool
sales_data <- pool%>%
tbl("Salesdata")%>%
as.data.frame()%>%
merge(locations, by="Stadt")%>%
select(Material,Nettogewicht,Stadt,Longitude,Latitude)%>%
group_by(Material,Stadt,Longitude,Latitude)%>%
summarise(Nettogewicht=sum(Nettogewicht))
ggplot() + geom_polygon(data=map_data_1,aes(x=long,y=lat,group=group),fill="grey",color="white")+
geom_point(data=sales_data,aes(x=Longitude,y=Latitude, size=Nettogewicht,colour=Stadt),alpha=0.7)+
scale_size(range = c(2,20))+
theme_void() +
coord_map()
}
)
}
shinyApp(ui, server)