Using column names with features of a dataframe in qplot

I have a dataset and unfortunately some of the column labels in mine dataframe

contain signs (- or +). It doesn't seem to bother dataframe

, but when I try to build 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 just gives me the category (or something like that, it gives me a graph of "1+" versus "2-" with one dot in the middle).

Can this be done easily? I looked at aes_string but didn't quite get it (at least not enough to make it work).

Thanks in advance.

PS I have searched for a solution on the internet, but cannot find anything that helps me with this (it may be related to some aspect that I do not understand), so I think it might be because it is a completely backward naming scheme. which I have: p.

+3


source to share


2 answers


As the other answer said, you have a problem because you don't have standard names. When the solution is to avoid naming backticks

, the code names must be converted to standard form. Another motivation for converting names to regular ones, you cannot use backticks

in graphics lattice

, 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 the unique = T option

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

      

If you don't like R's naming rules, here's a custom version 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

      

+2


source


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

For example:

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

      

So your call qplot()

should look like this:



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

      

enter image description here


More information can be found in ?Quotes

and?names

+3


source







All Articles