user3646834
user3646834

Reputation:

Symbols in ggplot2 breaks

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()

enter image description here

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

Answers (1)

Rorschach
Rorschach

Reputation: 32426

You can use parse as in the following possibilities. I think this is easier than having to write out lists of expressions.

Edit

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, enter image description here

Upvotes: 1

Related Questions