1

I need to annotate different facet with different labels in a faceted ggplots. Yes, I try creating an additional data frame to hold the annotations as per a response to this similar question. Annotating text on individual facet in ggplot2

The challenge is that since the annotation includes subscripts; I have had to use function 'expression' to construct them so I can then use geom_text.

ModelDisplay = data.frame(
  label = c(expression(model:0.0034+0.0011*Sph[4216]*(h)), expression(model:0.0034+0.0011*Sph[4216]*(h)), expression(model:0.0034+0.0011*Sph[4216]*(h)),expression(model:0.0034+0.0011*Sph[4216]*(h))),
  dir.hor   = c(45, 90, 135, 180)
)

This code gave the error statement:

Error in as.data.frame.default(x[[i]], optional = TRUE) : 
  cannot coerce class ""expression"" to a data.frame

Please any help would be appreciated.

Edit: Here is my ggplot2 code.

ggplot(data = allazimvariogram_EmpandMod_lagbased, aes(x = dist, y = gamma))+
  labs(x = "Lag Distance, m", y = "Porosity Empirical Variogram")+
  geom_point(color = "blue") +
  coord_cartesian(xlim = c(0, 8000), ylim = c(0, 0.008))+
  scale_x_continuous(breaks = seq(0, 8000, by = 2000))+
  scale_y_continuous(breaks = seq(0, 0.008, by = 0.001))+
  geom_smooth(aes(x=dist, y=Modgamma), se = FALSE, colour = "red")+
  geom_text(data    = ModelDisplay, mapping = aes(x = -Inf, y = -Inf, label = label),hjust   = -0.1,vjust   = -1, parse = T)+
  facet_wrap(~ dir.hor, nrow = 2)
TTOWG
  • 31
  • 2
  • 2
    Use `parse = TRUE`, maybe relevant post: https://stackoverflow.com/q/15125628/680068 – zx8754 Feb 04 '19 at 21:33
  • Please also show your ggplot2 code. – Roman Luštrik Feb 04 '19 at 22:14
  • @zx8754. I used parse = T when adding the geom_text layer to my ggplot. See my ggplot2 code just added.The challenge is not at geom_text call; rather the challenge is at the data frame I created to hold the annotation strings and their respective facet variable values. I needed the data frame since I am labeling a faceted plot with each facet having its own label different from others. Thank you for your time. – TTOWG Feb 05 '19 at 07:39
  • Keep the expressions in quotes `"..."`, so we can keep them in a dataframe as a string, then [evaluate](https://stackoverflow.com/q/1743698/680068). – zx8754 Feb 05 '19 at 08:01
  • @zx8754. Thank you so much. It worked just perfectly! Just in time for my presentation in about 2 hours from now. Am inexperienced in SO; tell me how to see questions others asked so I might be helpful too. – TTOWG Feb 05 '19 at 08:34
  • Usually Google search is pretty good. – zx8754 Feb 05 '19 at 08:48
  • I closed the post, but please do not delete it. It is still useful for other users having similar questions. – zx8754 Feb 05 '19 at 08:49
  • I mean how do I see when people ask questions so I can help them too. – TTOWG Feb 05 '19 at 08:53

0 Answers0