将次轴作为折线图添加到 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()
我可以用下面的代码得到条形图。但是,我正在尝试将次轴作为折线图添加到半决赛列。我们可以添加这个吗?
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()