Jóhann
Jóhann

Reputation: 153

Using column names with signs of a data frame in a qplot

I have a dataset and unfortunately some of the column labels in my dataframe contain signs (- or +). This doesn't seem to bother the dataframe, but when I try to plot this with qplot it throws me an error:

x <- 1:5
y <- x
names <- c("1+", "2-")

mydf <- data.frame(x, y)
colnames(mydf) <- names
mydf
qplot(1+, 2-, data = mydf)

and if I enclose the column names in quotes it will just give me a category (or something to that effect, it'll give me a plot of "1+" vs. "2-" with one point in the middle).

Is it possible to do this easily? I looked at aes_string but didn't quite understand it (at least not enough to get it to work).

Thanks in advance.

P.S. I have searched for a solution online but can't quite find anything that helps me with this (it could be due to some aspect I don't understand), so I reason it might be because this is a completely retarded naming scheme I have :p.

Upvotes: 3

Views: 126

Answers (2)

agstudy
agstudy

Reputation: 121598

As said in the other answer you have a problem because you you don't have standard names. When solution is to avoid backticks notation is to convert colnames to a standard form. Another motivation to convert names to regular ones is , you can't use backticks in a lattice plot for example. Using gsub you can do this:

 gsub('(^[0-9]+)[+|-]+|[+|-]+','a\\1',c("1+", "2-","a--"))
[1] "a1" "a2" "aa"

Hence, applying this to your example :

 colnames(mydf) <- gsub('(^[0-9]+)[+|-]+|[+|-]+','a\\1',colnames(mydf))
 qplot(a1,a2,data = mydf)

EIDT

you can use make.names with option unique =T

 make.names(c("10+", "20-",  "10-", "a30++"),unique=T)
[1] "X10."  "X20."  "X10..1" "a30.."

If you don't like R naming rules, here a custom version with using gsubfn

library(gsubfn)
gsubfn("[+|-]|^[0-9]+", 
function(x) switch(x,'+'= 'a','-' ='b',paste('x',x,sep='')),
c("10+", "20-",  "10-", "a30++"))
"x10a"  "x20b"  "x10b" "a30aa"    ## note x10b looks better than X10..1

Upvotes: 2

Andrie
Andrie

Reputation: 179468

Since you have non-standard column names, you need to to use backticks (`)in your column references.

For example:

mydf$`1+`
[1] 1 2 3 4 5

So, your qplot() call should look like this:

qplot(`1+`, `2-`, data = mydf)

enter image description here


You can find more information in ?Quotes and ?names

Upvotes: 3

Related Questions