Reputation:
I have data with the word alpha
in it, and I'd like to use ggplot2 to render the alpha
in the breaks as the symbol.
df <- data.frame(Method = c("Method (alpha = 0.01)", "Method (alpha = 0.05)"),
Value = c(2,3))
ggplot(df, aes(x = Method,
y = Value)) +
geom_point()
I couldn't find this on the site, but I don't think it will be that difficult a question. I can get single values in breaks to work using the expression
command in ggplot2::xlab
, etc., but I can't figure out how to create a vector of expressions. For example, the code
c(expression("Method (alpha = 0.01)"),
+ expression("Method (alpha = 0.05)"))
gives as output
expression("Method (alpha = 0.01)", "Method (alpha = 0.05)")
Upvotes: 2
Views: 125
Reputation: 32426
You can use parse
as in the following possibilities. I think this is easier than having to write out lists of expressions.
To increase the space between 'Method' and the rest,
df$Method <- gsub("Method", "Method~", as.character(df$Method))
Then, plot
ggplot(df, aes(x = Method, y = Value)) +
geom_point() +
scale_x_discrete(labels = parse(text=gsub('=','==',as.character(df$Method))))
or
ggplot(df, aes(x = Method, y = Value)) +
geom_point() +
scale_x_discrete(labels = parse(text=paste("alpha", c(0.01, 0.05), sep="==")))
The result from the first one,
Upvotes: 1