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)
8
  • 2
    Use parse = TRUE, maybe relevant post: stackoverflow.com/q/15125628/680068 Commented Feb 4, 2019 at 21:33
  • Please also show your ggplot2 code. Commented Feb 4, 2019 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. Commented Feb 5, 2019 at 7:39
  • Keep the expressions in quotes "...", so we can keep them in a dataframe as a string, then evaluate. Commented Feb 5, 2019 at 8: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. Commented Feb 5, 2019 at 8:34

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.