0

i have a variable v containing a data.frame column name.

I now want to plot it against its index.

Normally, plotting a column against its index is easy:

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

But in my case, I can’t figure out what incantations to do:

plot_vs <- function(df, count = 2) {
    vs <- paste0('V', seq_len(count)) # 'V1', 'V2', ...
    for (v in vs) {
        # this won’t work because “v” is a string
        print(ggplot(df, aes(seq_along(v), v)) + geom_point())
        # maybe this? but it also 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())
        # this doesn’t work because i use seq_along, and seq_along('V1') is 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))
9
  • 1
    stackoverflow.com/questions/15458526/… Commented Jun 3, 2015 at 9:12
  • The "get" function returns the value of an object given its name as a string. ggplot(df, aes(seq_along(get(v)), get(v))) + geom_point() Commented Jun 3, 2015 at 9:13
  • @AnthonyDamico wrong, please read the code again Commented Jun 3, 2015 at 9:18
  • @JohannesNE sadly it doesn’t work, just like my second idea (“Error in function ‘get’: object ‘v’ not found”) Commented Jun 3, 2015 at 9:19
  • @flyingsheep could you edit your question and clarify why aes_string does not solve your problem? Commented Jun 3, 2015 at 9:44

3 Answers 3

1

Your question title explicitly states that you want to do this without aes_string. Here's how you do it with aes_string: use paste.

plot_vs <- function(df, count = 2) {
  vs <- paste0('V', seq_len(count)) # 'V1', 'V2', ...
  for (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))
Sign up to request clarification or add additional context in comments.

1 Comment

haha, didn’t know that the arguments of aes_string would be fully parsed as expressions. this is a solution!
0
library(ggplot2)
df <- data.frame(a = c(.4, .5, .2))
v <- "a"
df$num<-seq(nrow(df))
ggplot(df, aes_string("num", v)) + geom_point()

2 Comments

sure, this works if the data.frame is small enough and if i want to mutate it.
you have it inside your function, so any changes to df will be discarded at the end of the call. yes, it only works if you have enough ram to read the data.frame and also add a column of indices.
0

@shadow’s comment gave the hint to find the solution, namely aes_q:

plot_vs <- function(df, count = 2) {
    vs <- paste0('V', seq_len(count)) # 'V1', 'V2', ...
    for (v in vs) {
        v = as.name(v)
        print(ggplot(df, aes_q(substitute(seq_along(v)), v)) + geom_point())
    }
}
plot_vs(data.frame(V1 = 4:6, V2 = 7:9, V3 = 10:12))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.