简体   繁体   English

将辅助轴作为折线图添加到 R 中的 DY 图中

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

I was able to get the bar plots with the below code.我能够使用以下代码获得条形图。 However I am trying to add secondary axis to Semi-Final column as a line chart.但是,我正在尝试将辅助轴作为折线图添加到半决赛列。 Can we add this ?我们可以添加这个吗?

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()

You may try(You need to download barseries.js )你可以试试(需要下载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