gpt4 book ai didi

r - ggplot aes eval(解析(文本))

转载 作者:行者123 更新时间:2023-12-01 20:02:26 27 4
gpt4 key购买 nike

我的数据 DT 具有以下结构:

structure(list(Ticker = c("MSDLWI Index", "MSDLWI Index", "MSDLWI Index", "MSDLWI Index","MSDLWI Index", "MSDLWI Index", "NDLEACWF Index", "NDLEACWF Index", "NDLEACWF Index","NDLEACWF Index", "NDLEACWF Index", "NDLEACWF Index"), Date = structure(c(-1L, 89L, 180L, 272L, 364L, 454L, 15705L, 15793L, 15884L, 15978L, 16070L, 16136L), class = c("IDate", "Date")), Value = c(NA, -0.02925, -0.180118465104301, 0.124488001005151, 0.0497217814923236,0.0966385660152425, 0.0323951658690891, 0.0842289682913797, 0.00992717655157427, 0.0631103139013451, 0.0782204344979787, 0.00855027196875335)), .Names =c("Ticker", "Date", "Value"), row.names = c(NA, -12L),class=c("data.table","data.frame"))

head(DT,3)

         Ticker       Date      Value
1: MSDLWI Index 1969-12-31 NA
2: MSDLWI Index 1970-03-31 -0.0292500
3: MSDLWI Index 1970-06-30 -0.1801185

但是,列名可能是可变的,因此我尝试在 ggplot 调用中对它们进行参数化:

var.col="Ticker"
ggplot(DT, aes(x=Date, y=Value, colour=eval(parse(text=var.col)))) + geom_line()

http://i.stack.imgur.com/H3tbN.png

在图例中,如何显示Ticker而不是eval(parse(text=var.col))

最佳答案

时髦的 eval(parse()) 业务没有理由:

ggplot(DT, aes_string(x="Date", y="Value", colour=var.col)) + 
geom_line()

关于r - ggplot aes eval(解析(文本)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22376504/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com