首页 > 解决方案 > 当我在其中添加世界地图代码时,Flex Dashboard 无法正常工作

问题描述

我在 RStudio 中使用 R Markdown 为“COVID-19 WORLDWIDE TRACKER”创建了一个弹性仪表板。我已经创建了一个线图、一个面积图和 2 个柱形图。我想在其中添加一个世界热图。数据在这里: mydatax。代码如下:

---
title: "COVID-19 WORLDWIDE TRACKER"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: scroll
    runtime: shiny
---

```{r setup, include=FALSE}
library(flexdashboard)
library(ggplot2)
library(plotly)
library(readxl)
library(rworldmap)
library(dplyr)
library(maps)
library(plyr)
library(gridExtra)

x <- read_excel("owid-covid-data.xlsx")
```

Page 1
===
row {data-height = 350}
---

### World Hotspots
```{r}
library(maps)
library(ggplot2)

mydata <- readxl::read_excel("mapdata1.xlsx")

mydata$Country[mydata$Country == "United States"] <- "USA"
mydata$Country[mydata$Country == "United Kingdom"] <- "UK"

world_map <- map_data("world")
world_map <- subset(world_map, region != "Antarctica")

ggplot(mydata) +
  geom_map(
    dat = world_map, map = world_map, aes(map_id = region),
    fill = "white", color = "#7f7f7f", size = 0.25
  ) +
  geom_map(map = world_map, aes(map_id = Country, fill = Cases), size = 0.25) +
  scale_fill_gradient(low = "#fff7bc", high = "#cc4c02", name = "Total Cases") +
  expand_limits(x = world_map$long, y = world_map$lat)
```


row {data-height=300} 
-----------------------------------------------------------------------
### Top 10 Countries
```{r}
p <- ggplot(x, aes(date, total_cases, color = location)) + geom_line()
ggplotly(p)
```


row {data-height=300} 
-----------------------------------------------------------------------
### Daily Comparison of New Cases and Deaths

```{r}
a <- ggplot(x, aes(date, new_cases, fill = location)) + geom_area()
ggplotly(a)
```


row {data-height=400} 
-----------------------------------------------------------------------

### Daily Comparison of Total Cases and Deaths

```{r}
b <- ggplot(x, aes(date, total_cases, fill = total_deaths)) + geom_col(position = "dodge")
ggplotly(b)
```

### Top Countries

```{r}
c <- ggplot(x, aes(total_cases_per_million, location)) + geom_col(position = "dodge")
ggplotly(c)
```

没有世界地图代码,仪表板工作得很好。但是当我向其中添加世界地图代码时,仪表板显示一个白屏,根本没有图表。我无法理解为什么会这样。请帮忙!

标签: rggplot2flexdashboardggplotly

解决方案


推荐阅读