user436994
user436994

Reputation: 601

ggplot2 plotting mathematical expressions

I have the following dataframe:

colframe <- data.frame(x=rep(1,5), y=seq(1,2,.25))

and the vectors

texti <- c("R %in% [0,0.01)", "R %in% [0.01,0.05)", "R %in% [0.05,0.1)", "R %in% [0.1,0.15)", "R > 0.15")
l <- colorRampPalette(c("red","yellow","green"))
hexi <- l(5)

and I want to plot with

ggplot(colframe, aes(x=x, y=y)) +
  geom_point(color=hexi[5:1], size=7) +
  geom_label(aes(label=texti, group=texti), x=1.05, hjust=0) + 
  theme(legend.position = "none",
        panel.background = element_blank(),
        axis.text  = element_blank(),
        axis.ticks = element_blank()) +
  xlab("") + ylab("")

Obviously, $in$ should be represented by an 'element in' symbol and the brackets should also be displayed accordingly. I tried a lot with expressions() but I could not get the correct representation.

Any ideas how I could plot this nicely?

Upvotes: 0

Views: 748

Answers (1)

wici
wici

Reputation: 1711

Set parse=TRUE inside geom_label() to get expressions as described in ?plotmath. This means you have to rewrite your intervalls. I illustrate how it works with with the same text for all labels:

texti <- rep("R %in% group('[',list(0,0.01),')')",5)

ggplot(colframe, aes(x=x, y=y)) +
  geom_point(color=hexi[5:1], size=7) +
  geom_label(aes(label=texti, group=texti), x=1.05, hjust=0, parse = TRUE) + 
  theme(legend.position = "none", 
        panel.background = element_blank(),
        axis.text  = element_blank(),
        axis.ticks = element_blank()) +
  xlab("") + ylab("")

Upvotes: 2

Related Questions