Maral Dorri
Maral Dorri

Reputation: 478

comma and apostrophe in annotate in ggplot2

I would like to create a label with a comma as follows:

label2 <- "epsilon[f[c]']"
label3 <- "epsilon[f[c],adj]"

I use the following:

... + annotate("text", x = -0.003, y= -0.25, label = label2, size = fontsize, 
          family = "sans", parse = TRUE) +
      annotate("text", x = -0.003, y= -0.25, label = label3, size = fontsize, 
          family = "sans", parse = TRUE)

But for label2: the ' needs to be removed for the code to even run and for label3: everything after "ult" is ignored in the subscript

EDIT:

Data:

> dput(material)
structure(list(strain = c(-0.00375, -0.00248, -0.00195, -0.00121, 
-0.000248, 0, 0.000132, 0.00145, -0.0046736, -0.0034036, -0.0028676, 
-0.0021336, -0.0011707, -0.0009226), stress = c(-4.6011, -5, 
-4.8555, -3.9414, -0.9901, 0, 0.5303, 0, -4.6011, -5, -4.8555, 
-3.9414, -0.9901, 0), type = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Standard 5 ksi Concrete", 
"Adjusted 5 ksi Concrete"), class = "factor")), row.names = c(NA, 
-14L), class = "data.frame")

Plot:

plot <- ggplot(material) + theme_void() +
 aes(x = strain, y = stress, colour = type, group = type) +
 geom_xspline(size = 0.7, spline_shape = 0.5) + labs(x = "Strain (in/in)", 
 y = "Stress (ksi)", color = element_blank()) + theme(legend.position = "bottom", 
   axis.title.y = element_text(size=10, colour="black", angle = 90, vjust = -22, hjust = 0.55),
   axis.title.x = element_text(size=10, colour="black", vjust = 4, hjust = 0.6),
   axis.text.y = element_blank(), axis.text.x = element_blank(),
   legend.text=element_text(size=10), legend.direction="vertical",
   legend.spacing.x = unit(0, 'cm')) + scale_colour_manual(values = 
    c("Standard 5 ksi Concrete" = "blue", "Adjusted 5 ksi Concrete" = "red")) +
  scale_y_continuous(trans = "reverse") + scale_x_continuous(trans = "reverse") + 
  geom_vline(aes(xintercept = 0), size = 0.3) + geom_hline(aes(yintercept = 0), size = 0.3) + 
  geom_segment(aes(x = 0, y = 0, xend = -0.0009226, yend = 0), size = 0.7) +
  geom_segment(aes(x = -0.0009226, y = 0, xend = -0.0011707, yend = -0.94), size = 0.7) +
  geom_segment(aes(x = 0, y = -4.958, xend = -0.00333, yend = -4.958), 
           size = 0.6, linetype = "dotted", colour = "black") +
  geom_segment(aes(x = -0.00333, y = 0, xend = -0.00333, yend = -4.958), 
           size = 0.6, linetype = "dotted", colour = "black") +
  geom_segment(aes(x = -0.0024, y = 0, xend = -0.0024, yend = -4.958), 
           size = 0.6, linetype = "dotted", colour = "black") +
  geom_segment(aes(x = -3.75E-03, y = 0, xend = -3.75E-03, yend = -4.6011), 
           size = 0.6, linetype = "dotted", colour = "black") +
  geom_segment(aes(x = -0.0046736, y = 0, xend = -0.0046736, yend = -4.6011), 
           size = 0.6, linetype = "dotted", colour = "black") +
  annotate("text", x = 0.0002, y= -5, label = label0, size = fontsize, family = "sans", parse = TRUE) +
  annotate("text", x = -0.0006, y= 0.2, label = label1, size = fontsize, family = "sans", parse = TRUE) +
  annotate("text", x = -0.0021, y= -0.25, label = label2, size = fontsize, family = "sans", parse = TRUE) +
  annotate("text", x = -0.003, y= 0.2, label = label3, size = fontsize, family = "sans", parse = TRUE) +
  annotate("text", x = -0.0041, y= -0.25, label = label4, size = fontsize, family = "sans", parse = TRUE) +
  annotate("text", x = -0.0045, y= 0.2, label = label5, size = fontsize, family = "sans", parse = TRUE)

Upvotes: 1

Views: 590

Answers (1)

kangaroo_cliff
kangaroo_cliff

Reputation: 6222

Change the label2 as below.

label2 <- "epsilon[f[c]*'\\'']"
label3 <- "epsilon[f[c*',adj']]"

I checked as below and it works fine.

p + annotate("text", x = -0.003, y= 0.25, label = label2, parse = TRUE, size = 12) + 
 annotate("text", x = -0.003, y= -0.25, label = label3, size = 12, parse = TRUE)

enter image description here

Upvotes: 1

Related Questions