我希望根據供應量是否超過或低于估計值來改變我的情節標題。供應量的估計值在 DF$Supply 中。如果它是正的,它超過了估計值,如果它是負的,它低于估計值。我如何設定條件以檢查 DF$Supply 并選擇正確的標題。
如果供應超出估計,我希望標題是這樣的:
labs(title=paste("In 2021-2030 the supply is over the estimate by", DF$Supply)
如果供應低于估計,我希望標題是這樣的:
labs(title=paste("In 2021-2030 the supply is under the estimate by", DF$Supply*-1)
如果供應量等于估計值,則標題為(這意味著 DF$Supply = 0):
labs(title="In 2021-2030 the supply matches the estimate")
uj5u.com熱心網友回復:
我不確定這是否可行,當你提供資料集時它總是更好......
labs(title = case_when(
DF$Supply >= 1 ~ paste("In 2021-2030 the supply is over the estimate by", DF$Supply),
DF$Supply < 0 ~ paste("In 2021-2030 the supply is under the estimate by", DF$Supply*-1),
DF$Supply == 0 ~ "In 2021-2030 the supply matches the estimate"
))
uj5u.com熱心網友回復:
您可以在繪圖之前設定繪圖的標題,如下所示:
plot_title <- "In 2021-2030 the supply "
plot_title <- dplyr::case_when(
DF$Supply>0~paste0(plot_title, "is over the estimate by ", DF$Supply),
DF$Supply<0~paste0(plot_title, "is under the estimate by ", DF$Supply*-1),
TRUE~paste0(plot_title, "matches the estimate")
)
然后,在您的 ggplot 呼叫中,您可以簡單地執行以下操作:
labs(title=plot_title)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/474387.html
下一篇:通過填寫R重新排序條形圖
