我正在嘗試創建一個回圈函式,該函式將通過資料集中兩個欄位的值組合生成相同的視覺效果。但是,這兩個欄位的長度不同。
報告型別有兩個唯一值,而標準號有 10。第一個組合最多只能達到 7,繪圖停止。我不知道如何讓它轉到報告型別中的下一個值以繼續沿著條件串列向下。
這是我的函式的樣子:
plot <- function(df, x, y){
# create list of reports and criterias in data to loop over
rpt_list<-unique(Property$REPORT_TYPE)
crit_list<-unique(Property$CRITERIA_NO)
for (i in length(rpt_list)) {
for(j in seq_along(crit_list)){
x_var <- enquo(x)
y_var <- enquo(y)
blah<-ggplot(data=subset(Property, REPORT_TYPE==rpt_list[[i]] & CRITERIA_NO==crit_list[[j]]), aes(x=!!x_var, y=!!y_var))
geom_bar(stat="identity")
facet_wrap(~DESCRIPTION)
ggtitle(expression('Properties by Qtr'))
print(blah)
}
}
}
這是我得到的錯誤。
Error: Faceting variables must have at least one value
Run `rlang::last_error()` to see where the error occurred.
繪圖部分有效,但在第 4-8 行之間似乎存在問題,我試圖創建一個串列進行迭代。
更新:資料結構
structure(list(QTR_END_DATE = structure(c(15795, 15795, 15795,
15795, 15795, 15795, 15795, 15795, 15795, 15795), class = "Date"),
REPORT_TYPE = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L), .Label = c("PT", "RE", "DU", "OY", "ST", "SZ"), class = "factor"),
CRITERIA_NO = c(1, 2, 3, 4, 5, 6, 7, 1, 2, 3), DESCRIPTION = structure(c(57L,
66L, 68L, 75L, 82L, 77L, 72L, 74L, 71L, 60L), .Label = c("$10 M to $15 M ",
"$15 M to $25 M ", "$25 M to $35 M ",
"$35 M to $50 M ", "$5 M to $10 M ",
"$50 M to $100 M ", "1 - 3 years ",
"1976", "1977", "1978", "1979", "1980", "1981", "1982", "1983",
"1984", "1985", "1986", "1987", "1988", "1989", "1990", "1991",
"1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999",
"2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007",
"2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015",
"2016", "2017", "2018", "2019", "2020", "2021", "3 - 5 years ",
"5 - 7 years ", "7 - 10 years ",
"Apartment ", "Current ",
"Delinquent ", "East North Central ",
"East South Central ", "Extended ",
"Foreclosed ", "Greater than $100 M ",
"Greater than 10 years ", "Hotel/Motel ",
"In-Process of Foreclosure/ Foreclosed ", "Industrial ",
"Less than 1 year ", "Loans less than $5 M ",
"Mid Atlantic ", "Mixed Use ",
"Mountain ", "New England ",
"Office Building ", "Other ",
"Other Commercial ", "Pacific ",
"Paid ", "Prior to 1976 ",
"Restructured ", "Retail ",
"Sold ", "South Atlantic ",
"West North Central ", "West South Central "
), class = "factor"), NUMBER_PROPERTY = c(808, 28, 972, 883,
1012, 235, 18, 155, 734, 356)), row.names = c(NA, 10L), class = "data.frame")
并打電話
plot(Property, QTR_END_DATE, NUMBER_PROPERTY)
uj5u.com熱心網友回復:
問題是,你迭代的每個組合REPORT_TYPE和CRITERIA_NO即使一些組合,比如PE-4,在資料不存在。這會導致您傳遞一個空資料框ggplot(),最終導致您看到的錯誤。
這是解決此問題的示例方法。
library(tidyverse)
quarter_plot = function(data, x, y) {
df %>%
split(list(.$REPORT_TYPE, .$CRITERIA_NO)) %>%
discard(~ nrow(.x) == 0) %>%
map(function(sub_data) {
ggplot(sub_data, aes_string(x = x, y = y))
geom_bar(stat = "identity")
facet_wrap(~ DESCRIPTION)
ggtitle("Properties by Qtr")
})
}
# Example usage
quarter_plot(Property, "QTR_END_DATE", "NUMBER_PROPERTY")
在我的實作,我用的每一種組合分割資料REPORT_TYPE和CRITERIA_NO,然后取出在沒有資料的情況下。之后我曾經purrr::map()為每個子資料框生成一個圖。關于此實作的一些注意事項:
- 我沒有將符號傳遞到函式中,而是將其更改為字串,以
aes_string()使實作更清晰。隨意恢復這個。 - 你不小心
Property在你的函式中使用df了引數 - 我修復了這個。 - 我不建議命名您的函式,
plot()因為它會覆寫現有函式。 - 我建議您不要列印圖,而是實際回傳它們(這是在我的版本中使用 完成的
map())。這使您可以選擇將繪圖保存到變數中,并且您仍然可以通過在控制臺中運行該函式輕松地將繪圖列印到螢屏上。
如果您想對原始功能進行最小的更改,您可以使用以下方法:
plot <- function(df, x, y){
# create list of reports and criterias in data to loop over
rpt_list<-unique(df$REPORT_TYPE)
crit_list<-unique(df$CRITERIA_NO)
for (i in seq_along(rpt_list)) {
for(j in seq_along(crit_list)){
data=subset(
df,
REPORT_TYPE==rpt_list[[i]] & CRITERIA_NO==crit_list[[j]]
)
if (nrow(data) == 0) {
next
}
x_var <- enquo(x)
y_var <- enquo(y)
blah<-ggplot(data, aes(x=!!x_var, y=!!y_var))
geom_bar(stat="identity")
facet_wrap(~DESCRIPTION)
ggtitle(expression('Properties by Qtr'))
print(blah)
}
}
}
uj5u.com熱心網友回復:
我認為當您為REPORT_TYPE=REandCRITERIA_NO=4情況對資料集進行子集化時會出現錯誤,因為您獲得了 0 行資料集。
避免此錯誤的一個選項可以是首先制作子集,然后檢查您是否有 0 行資料,并在可能的情況下制作繪圖:
plot <- function(df, x, y){
# create list of reports and criterias in data to loop over
rpt_list<-unique(Property$REPORT_TYPE)
crit_list<-unique(Property$CRITERIA_NO)
for (i in length(rpt_list)) {
for(j in seq_along(crit_list)){
x_var <- enquo(x)
y_var <- enquo(y)
tmp = subset(Property, REPORT_TYPE==rpt_list[[i]] & CRITERIA_NO==crit_list[[j]])
if (nrow(tmp) != 0) {
blah<-ggplot(data=tmp, aes(x=!!x_var, y=!!y_var))
geom_bar(stat="identity")
facet_wrap(~DESCRIPTION)
ggtitle(expression('Properties by Qtr'))
print(blah)
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/390426.html
