special characters in column names

1,148 views
Skip to first unread message

july

unread,
May 6, 2011, 8:18:21 AM5/6/11
to ggplot2
Hi there,

my data frame has columns with special characters, e.g.:

df <- data.frame(c(1:10),c(10:1))
names(df) <- c("Special name(1)","Special name(2)")

then, I have a variable containing the name of the column which should
be plotted:

plot.Column <- "Special name(2)"

I'd like to use this variable to generate my plot. Instead of writing
this:

ggplot(df, aes(y = `Special name(2)`, x = `Special name(2)`)) +
geom_point()

I'd like to write something like this (this does not work, I know but
maybe you see what I want to do):

ggplot(df, aes(y = plot.Column, x = plotColumn)) + geom_point()


Is it possible? How?

Antje

july

unread,
May 6, 2011, 8:24:03 AM5/6/11
to ggplot2
I think, I found a solution, but I have to change the labeling on the
y-axis (now "get(plot.Column)") by adding the scale_y_...name

ggplot(df, aes(y = get(plot.Column), x = get(plot.Column))) +
geom_point()

If you know any other solution, let me know.

Luciano Selzer

unread,
May 6, 2011, 11:59:33 AM5/6/11
to july, ggplot2
Have a look at aes_string.
 
HTH
Luciano


--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

july

unread,
May 6, 2011, 1:49:39 PM5/6/11
to ggplot2
I've tried it but something was wrong. I could not get it working...
Could you please provide me example code for my example dataset?

Antje


On 6 Mai, 17:59, Luciano Selzer <luciano.sel...@gmail.com> wrote:
> Have a look at aes_string.
>
> HTH
> Luciano
>
> 2011/5/6 july <antje.niederl...@googlemail.com>

july

unread,
May 9, 2011, 2:51:01 AM5/9/11
to ggplot2
I've tested it once more. It does not work with aes_string. Is there
anything wrong?:

library(ggplot2)

df <- data.frame(c(1:10),c(10:1))

# works with simple column names
names(df) <- c("name1","name2")
parameter <- "name1"
ggplot(df, aes_string(x = parameter, y = parameter)) + geom_point()

# does not work with special characters in column names
names(df) <- c("Special name(1)","Special name(2)")
parameter <- "Special name(1)"
ggplot(df, aes_string(x = parameter, y = parameter)) + geom_point()

# works like this but then the labeling shows "get(parameter)" instead
of the parameter name
ggplot(df, aes(x = get(parameter), y = get(parameter))) + geom_point()

Ciao,
Antje

Luciano Selzer

unread,
May 9, 2011, 9:15:48 AM5/9/11
to july, ggplot2
Well, the issue is with parse and the space in the name. parse produces an expression and if the name has spaces in it then the expression would be treated like two thing instead of a a variable name. I hope this explanation makes sense. I would replace the space with an underscore or a point.

HTH
Luciano


july

unread,
May 10, 2011, 2:28:30 AM5/10/11
to ggplot2
Thanks a lot for your explanation! I cannot replace the characters
(the plots will be generated automatically and I don't know anything
about column names of the incoming tables). Instead, I will stick to
the solution with get(...) and add the correct labeling by changing
the appropriate scale name.

Ciao,
Antje



On May 9, 3:15 pm, Luciano Selzer <luciano.sel...@gmail.com> wrote:
> Well, the issue is with parse and the space in the name. parse produces an
> expression and if the name has spaces in it then the expression would be
> treated like two thing instead of a a variable name. I hope this explanation
> makes sense. I would replace the space with an underscore or a point.
>
> HTH
> Luciano
>
> 2011/5/9 july <antje.niederl...@googlemail.com>

Hadley Wickham

unread,
May 16, 2011, 10:51:16 AM5/16/11
to july, ggplot2
On Mon, May 9, 2011 at 1:51 AM, july <antje.ni...@googlemail.com> wrote:
> I've tested it once more. It does not work with aes_string. Is there
> anything wrong?:
>
> library(ggplot2)
>
> df <- data.frame(c(1:10),c(10:1))
>
> # works with simple column names
> names(df) <- c("name1","name2")
> parameter <- "name1"
> ggplot(df, aes_string(x = parameter, y = parameter)) + geom_point()
>
> # does not work with special characters in column names
> names(df) <- c("Special name(1)","Special name(2)")
> parameter <- "Special name(1)"
> ggplot(df, aes_string(x = parameter, y = parameter)) + geom_point()

You want:

names(df) <- c("Special name(1)","Special name(2)")
parameter <- "`Special name(1)`"
ggplot(df, aes_string(x = parameter, y = parameter)) + geom_point()

When you have non-syntactic names, you need to surround them in `.

Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Reply all
Reply to author
Forward
0 new messages