Isaac
Isaac

Reputation: 100

Using variable elements as superscripts and subscripts for Plot labels on ggplot2

Using geom_jitter(), I plot a and b. For each point on the plot I want to display a label of the form $c^(d)_(e)$ ie, variable c with superscript d and subscript e, where c, d and e are the corresponding values associated with a and b in data.

I've tried to use geom_text() along with bquote(.(c)^d[e]), but it displays an error :

Don't know how to automatically pick scale for object of type call. Defaulting to continuous.
Error: Aesthetics must be either length 1 or the same as the data (42): label, x, y

Below, you can find the code and data I'm using.

a <- c(1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
       3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3)

b <- c(9.75, 19.0, 9.75, 4.02, 3.1, 19.0, 9.27, 17.2, 9.27, 19.0, 7.78, 6.06, 9.75, 
       4.02, 3.05, 3.1, 2.59, 2.29, 19.0, 9.27, 3.93, 17.2, 3.05, 15.59, 9.27, 
       3.93, 3.05, 19.0, 9.27, 7.47, 17.2, 7.47, 9.27, 5.87, 5.87, 8.82, 19.0, 
       7.78, 5.87, 6.06, 5.01, 4.45)

c <- c('(0, 1)', '(1, 0)', '(0, 2)', '(0, 2)', '(0, 2)', '(1, 1)', '(1, 1)', '(1, 1)', 
       '(1, 1)', '(2, 0)', '(2, 0)', '(2, 0)', '(0, 3)', '(0, 3)', '(0, 3)', '(0, 3)', 
       '(0, 3)', '(0, 3)', '(1, 2)', '(1, 2)', '(1, 2)', '(1, 2)', '(1, 2)', '(1, 2)', 
       '(1, 2)', '(1, 2)', '(1, 2)', '(2, 1)', '(2, 1)', '(2, 1)', '(2, 1)', '(2, 1)', 
       '(2, 1)', '(2, 1)', '(2, 1)', '(2, 1)', '(3, 0)', '(3, 0)', '(3, 0)', '(3, 0)', 
       '(3, 0)', '(3, 0)')

d <- c('(0, 0)', '(0, 0)', '(0, 0)', '(0, 0)', '(0, 1)', '(0, 0)', '(0, 0)', '(0, 1)', 
       '(1, 0)', '(0, 0)', '(0, 0)', '(1, 0)', '(0, 0)', '(0, 0)', '(0, 0)', '(0, 1)', 
       '(0, 1)', '(0, 2)', '(0, 0)', '(0, 0)', '(0, 0)', '(0, 1)', '(0, 1)', '(0, 2)', 
       '(1, 0)', '(1, 0)', '(1, 1)', '(0, 0)', '(0, 0)', '(0, 0)', '(0, 1)', '(0, 1)', 
       '(1, 0)', '(1, 0)', '(1, 1)', '(2, 0)', '(0, 0)', '(0, 0)', '(0, 0)', '(1, 0)', 
       '(1, 0)', '(2, 0)')

e <- c('(0, 1)', '(1, 0)', '(0, 1)', '(0, 2)', '(0, 2)', '(1, 0)', '(1, 1)', '(1, 1)', 
       '(1, 1)', '(1, 0)', '(2, 0)', '(2, 0)', '(0, 1)', '(0, 2)', '(0, 3)', '(0, 2)', 
       '(0, 3)', '(0, 3)', '(1, 0)', '(1, 1)', '(1, 2)', '(1, 1)', '(1, 2)', '(1, 2)', 
       '(1, 1)', '(1, 2)', '(1, 2)', '(1, 0)', '(1, 1)', '(2, 1)', '(1, 1)', '(2, 1)', 
       '(1, 1)', '(2, 1)', '(2, 1)', '(2, 1)', '(1, 0)', '(2, 0)', '(3, 0)', '(2, 0)', 
       '(3, 0)', '(3, 0)')

data <- data.frame(a, b, c, d, e)

library(ggplot2)
c <- ggplot(data, aes(a, b)) + 
  geom_jitter(width = 0.2) + 
  geom_text(aes(label = bquote(.(c)^.(d)[.(e)])), 
            position = position_jitter(width = 0.2, height = 0))

Upvotes: 0

Views: 367

Answers (1)

user10399874
user10399874

Reputation: 21

try this,

dd <- data.frame(a, b, c, d, e, stringsAsFactors = FALSE)
dd$lab <- apply(dd, 1, function(row) bquote(.(row[1])^.(row[2])[.(row[3])]))

library(ggplot2)
ggplot(dd, aes(a, b)) + 
  geom_jitter(width = 0.2) + 
  geom_text(aes(label = lab), parse = TRUE,
            position = position_jitter(width = 0.2, height = 0))

Upvotes: 2

Related Questions