簡體   English   中英

將輔助軸作為折線圖添加到 R 中的 DY 圖中

[英]Add secondary axis as a line chart to DY graphs in R

我能夠使用以下代碼獲得條形圖。 但是,我正在嘗試將輔助軸作為折線圖添加到半決賽列。 我們可以添加這個嗎?

mobility_aus_sum <- structure(list(Year = c(2020, 2020, 2020, 2020, 2020, 2020, 2020, 
                                            2020, 2020, 2020, 2020, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 
                                            2021, 2021, 2021, 2021, 2021, 2022, 2022), mon_day = c("April", 
                                                                                                   "August", "December", "February", "July", "June", "March", "May", 
                                                                                                   "November", "October", "September", "April", "August", "December", 
                                                                                                   "February", "January", "July", "June", "March", "May", "November", 
                                                                                                   "October", "September", "February", "January"), Final = c(-1483, 
                                                                                                                                                             -912, -405, -232, -698, -739, -633, -1125, -540, -738, -802, 
                                                                                                                                                             -482, -1012, -260, -607, -677, -827, -549, -509, -440, -326, 
                                                                                                                                                             -659, -871, -480, -639), `Semi-Final` = c(-1333, -762, -255, 
                                                                                                                                                                                                         -82, -548, -589, -483, -975, -390, -588, -652, -332, -862, -110, 
                                                                                                                                                                                                         -457, -527, -677, -399, -359, -290, -176, -509, -721, -330, -489
                                                                                                                                                             )), row.names = c(NA, -25L), groups = structure(list(Year = c(2020, 
                                                                                                                                                                                                                           2021, 2022), .rows = structure(list(1:11, 12:23, 24:25), ptype = integer(0), class = c("vctrs_list_of", 
                                                                                                                                                                                                                                                                                                                  "vctrs_vctr", "list"))), row.names = c(NA, 3L), class = c("tbl_df", 
                                                                                                                                                                                                                                                                                                                                                                            "tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
                                                                                                                                                                                                                                                                                                                                                                                                                           "tbl_df", "tbl", "data.frame"))
dates <- with(mobility_aus_sum, as.Date(paste(Year, mon_day, 1), "%Y %B %d"))
df2 <- mobility_aus_sum[order(dates),]
df3 <- ts(df2$Final, start = c(df2$Year[1], match(df2$mon_day[1], month.name)), 
          frequency = 12)

dygraph(df3) %>%
  dyRangeSelector() %>%
  dyBarChart()

你可以試試(需要下載barseries.js

library(dplyr)
library(dygraphs)
library(tibble)
dyBarSeries <- function(dygraph, name, ...) {
  file <- "D:/barseries.js" #you need to link to the downloaded file
  plotter_ <- paste0(readLines(file, skipNul = T), collapse = "\n")
  
  dots <- list(...)
  do.call('dySeries', c(list(dygraph = dygraph, name = name, plotter = 
                               plotter_), dots))
  
}

mobility_aus_sum %>%
  mutate(mon_day = match(mon_day, month.name)) %>%
  rowwise %>%
  mutate(dates = paste0(c(Year, mon_day, 1), collapse = "-")) %>%
  mutate(dates = as.Date(dates, "%Y-%m-%d"))  %>%
  ungroup %>%
  select(-mon_day, -Year) %>% 
  column_to_rownames(var = 'dates') %>%
  dygraph(.) %>%
  dyAxis("y", label = "Final", valueRange = c(-1500, -200), independentTicks = TRUE) %>%
  dyAxis("y2", label = "Semi-Final ", valueRange = c(-1400, 0), independentTicks = TRUE) %>%
  dyBarSeries("Final") %>%
  dySeries("Semi-Final", axis=('y2')) %>%
  dyRangeSelector() 

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM