1

我是串扰的新手,并试图通过在bar+line plot 上使用来使rmarkdown文件更具交互性,但它没有在图上给出线条,并且当​​我更改 country 时也会变得奇怪

library(tidyverse)
library(plotly)
library(crosstalk)
library(glue)
library(scales)
library(tidytext)

加载数据:

file_url <- "https://raw.githubusercontent.com/johnsnow09/covid19-df_stack-code/main/test_crosswalk.csv"

test_df <- read.csv(url(file_url))
Country_selected = c("Brazil") 
selected_case_type = c("Confirmed_daily")

trend_sd <- test_df %>% 
                filter(Daily_Cases_type %in% selected_case_type
                       # Country.Region %in% Country_selected, 
                       ) %>%   
              select(Country.Region, date, Cases_count)%>%
              arrange(date) %>% 
              group_by(Country.Region) %>% 
              mutate(new_avg = cumsum(Cases_count)/ seq_len(length(Cases_count))) %>% 
              ungroup() %>% 
              SharedData$new()


bscols(widths = c(9, 3),
       list(
  filter_select(id = "country", label = "Country:", sharedData =  trend_sd, group =  ~ Country.Region),
  
  
  ggplotly(ggplot(data = trend_sd) +
            geom_col(aes(x = date, y = Cases_count), fill = "turquoise", alpha = .3) +
            geom_point(aes(x = date, y = new_avg), col = "tomato") +
            geom_line(aes(x = date, y = new_avg), col = "tomato", size = .9, alpha = .3) +
            scale_y_continuous(labels = comma) +
            # expand_limits(y = 100000) +
            
            labs(title = glue("{Country_selected}'s {selected_case_type} Cases {date_from} onwards"),
                 caption = "Data source: covid19.analytics") 
  ))
)

这并没有给出正确的线图,即使我将国家/地区更改为其他国家/地区,条形也会失真。

在此处输入图像描述


下面的代码和绘图结果没有串扰和情节

Country_selected = c("India")  # can be selective
selected_case_type = c("Confirmed_daily")

  test_df %>% 
                filter(Daily_Cases_type %in% selected_case_type,
                       Country.Region %in% Country_selected,
                       ) %>%   
              select(Country.Region, date, Cases_count)%>%
              arrange(date) %>% 
              group_by(Country.Region) %>% 
              mutate(new_avg = cumsum(Cases_count)/ seq_len(length(Cases_count))) %>% 
              ungroup() %>%   
  
  ggplot() +
  geom_col(aes(x = date, y = Cases_count), fill = "turquoise", alpha = .3) +
  geom_point(aes(x = date, y = new_avg), col = "tomato") +
  geom_line(aes(x = date, y = new_avg), col = "tomato", size = .9, alpha = .3) +
  scale_y_continuous(labels = comma) +
  
  labs(title = glue("{Country_selected}'s {selected_case_type} Cases {date_from} onwards"),
       subtitle = "With Average Daily Cases Trend line",
       caption = "Data source: covid19.analytics")

在此处输入图像描述

4

0 回答 0