我想將label_format運算式傳遞給 ggplot 但出現錯誤
#toy df
df <- data.frame(
x = 1:10,
y = seq(0.1,1,by=0.1),
label_format = "scales::percent_format(accuracy=0.1)"
)
ggplot(df,aes(x=x,y=y))
geom_point()
scale_y_continuous(label=!! rlang::parse_expr( label_format))
Error in parse_exprs(x) : object 'label_format' not found
我想評估字串以最終得到這個帶有格式化 y 軸的圖:
ggplot(df,aes(x=x,y=y))
geom_point()
scale_y_continuous(label=scales::percent_format(accuracy = 0.1))

uj5u.com熱心網友回復:
標尺無權訪問全域資料來評估存盤在資料列中的運算式。您可以使用未決議的運算式,如下所示:
library(ggplot2)
df <- data.frame(
x = 1:10,
y = seq(0.1,1,by=0.1)
)
label_format <- "scales::percent_format(accuracy=0.1)"
ggplot(df,aes(x=x,y=y))
geom_point()
scale_y_continuous(label= eval(parse(text = label_format)))

由reprex 包于 2022-01-13 創建(v2.0.1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/416805.html
標籤:
下一篇:ggplot2之前/之后的斜率圖
