如何通过 R 中的 gt table 按升序(一月、二月、三月)显示我的月份?
How do I display my months in ascending order (Jan, Feb, Mar) through a gt table in R?
下面是我的两段代码。在第一个块中,我试图让我的数据准备好在 gt table 中显示。我的目标是在第 1 列中按升序(一月、二月、三月等)显示带有月份缩写的 table。目前,每次我 运行 要创建的第二段代码时它都会出现a table,它按字母顺序对月份数据进行排序。我查看了 lubridate 包,弄乱了 'month.abb' 和 'month.name 函数,但仍然按字母顺序输出月份。请参阅下面使用的包和下面的块。
library(tidyverse)
library(quantmod)
library(tidyquant)
library(xts)
library(rvest)
library(stringr)
library(forcats)
library(lubridate)
library(tidyr)
library(plotly)
library(purrr)
library(PerformanceAnalytics)
library(gt)
library(paletteer)
library(readr)
library(janitor)
library(scales)
library(lubridate)
获取并清理数据以输入 gt(区块 1)
start <- as.Date("2020-01-01")
end <- as.Date("2020-11-30")
StockMonthlyReturns <- c("XLC", "XLC", "XLP", "XLE","XLF", "XLV", "XLI","XLB", "XLRE", "XLK", "XLU") %>%
tq_get(get = "stock.prices",
from = start,
to = end) %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "StockMonthlyReturns")
stockdata <- StockMonthlyReturns %>% group_by(symbol, Month = floor_date(date, "month")) %>% summarise(Amount = sum(StockMonthlyReturns))
monthnum <- month(stockdata$Month)
stockdata <- cbind(stockdata, monthnum)
stockdata <- stockdata %>% rename(Month_Num = ...4)
stockdata$Month_Num <- month.abb[stockdata$Month_Num]
stockdata <- stockdata %>%
select(-Month)
stockdata <- stockdata %>% rename(Month = "Month_Num")
#stockdata$Month <- months(as.Date(stockdata$Month))
stockdata <- adorn_rounding(stockdata, digits= 4) # Rounding Returns
stockdata$Amount <- percent(stockdata$Amount, accuracy = 0.01) # Changing Decimals to Percent
使用 dplyr 使用 gt(块 2)绘制数据图
stockdata %>%
tidyr::spread(key="symbol", value = Amount) %>%
gt(rowname_col = "Month") %>%
tab_header(title = "Monthly Return of SPDR ETF's")
你可以试试:
library(tidyverse)
library(gt)
stockdata %>%
mutate(Month = factor(Month, month.abb)) %>%
tidyr::spread(key=symbol, value = Amount) %>%
#tidyr::pivot_wider(names_from = symbol, values_from= Amount) %>%
gt(rowname_col = "Month") %>%
tab_header(title = "Monthly Return of SPDR ETF's")
另请注意,spread
已停用,取而代之的是 pivot_wider
。
下面是我的两段代码。在第一个块中,我试图让我的数据准备好在 gt table 中显示。我的目标是在第 1 列中按升序(一月、二月、三月等)显示带有月份缩写的 table。目前,每次我 运行 要创建的第二段代码时它都会出现a table,它按字母顺序对月份数据进行排序。我查看了 lubridate 包,弄乱了 'month.abb' 和 'month.name 函数,但仍然按字母顺序输出月份。请参阅下面使用的包和下面的块。
library(tidyverse)
library(quantmod)
library(tidyquant)
library(xts)
library(rvest)
library(stringr)
library(forcats)
library(lubridate)
library(tidyr)
library(plotly)
library(purrr)
library(PerformanceAnalytics)
library(gt)
library(paletteer)
library(readr)
library(janitor)
library(scales)
library(lubridate)
获取并清理数据以输入 gt(区块 1)
start <- as.Date("2020-01-01")
end <- as.Date("2020-11-30")
StockMonthlyReturns <- c("XLC", "XLC", "XLP", "XLE","XLF", "XLV", "XLI","XLB", "XLRE", "XLK", "XLU") %>%
tq_get(get = "stock.prices",
from = start,
to = end) %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "StockMonthlyReturns")
stockdata <- StockMonthlyReturns %>% group_by(symbol, Month = floor_date(date, "month")) %>% summarise(Amount = sum(StockMonthlyReturns))
monthnum <- month(stockdata$Month)
stockdata <- cbind(stockdata, monthnum)
stockdata <- stockdata %>% rename(Month_Num = ...4)
stockdata$Month_Num <- month.abb[stockdata$Month_Num]
stockdata <- stockdata %>%
select(-Month)
stockdata <- stockdata %>% rename(Month = "Month_Num")
#stockdata$Month <- months(as.Date(stockdata$Month))
stockdata <- adorn_rounding(stockdata, digits= 4) # Rounding Returns
stockdata$Amount <- percent(stockdata$Amount, accuracy = 0.01) # Changing Decimals to Percent
使用 dplyr 使用 gt(块 2)绘制数据图
stockdata %>%
tidyr::spread(key="symbol", value = Amount) %>%
gt(rowname_col = "Month") %>%
tab_header(title = "Monthly Return of SPDR ETF's")
你可以试试:
library(tidyverse)
library(gt)
stockdata %>%
mutate(Month = factor(Month, month.abb)) %>%
tidyr::spread(key=symbol, value = Amount) %>%
#tidyr::pivot_wider(names_from = symbol, values_from= Amount) %>%
gt(rowname_col = "Month") %>%
tab_header(title = "Monthly Return of SPDR ETF's")
另请注意,spread
已停用,取而代之的是 pivot_wider
。