r - ggplot: aesthetics from variable string without aes_string -


i have variable v containing data.frame column name.

i want plot against index.

normally, plotting column against index easy:

df <- data.frame(a = c(.4, .5, .2)) ggplot(df, aes(seq_along(a), a)) + geom_point() 

but in case, can’t figure out incantations do:

plot_vs <- function(df, count = 2) {     vs <- paste0('v', seq_len(count)) # 'v1', 'v2', ...     (v in vs) {         # won’t work because “v” string         print(ggplot(df, aes(seq_along(v), v)) + geom_point())         # maybe this? doesn’t work (“object ‘v.s’ not found”)         v.s <- as.symbol(v)         print(ggplot(df, aes(seq_along(v.s), v.s)) + geom_point())         # doesn’t work because use seq_along, , seq_along('v1') 1:         print(ggplot(df, aes_string(seq_along(v), v)) + geom_point())     } } plot_vs(data.frame(v1 = 4:6, v2 = 7:9, v3 = 10:12)) 

your question title explicitly states want without aes_string. here's how aes_string: use paste.

plot_vs <- function(df, count = 2) {   vs <- paste0('v', seq_len(count)) # 'v1', 'v2', ...   (v in vs) {     print(ggplot(df, aes_string(paste("seq_along(", v, ")"), v)) + geom_point())   } }  plot_vs(data.frame(v1 = 4:6, v2 =7:9, v3 = 10:12)) 

Comments