0

See below for my code. This creates 2 plots, however I want to annote the JK Rowling book on my Fiction plot only. Genres are only Fiction and Non Fiction.

ggplot(data=books_df_new)+
  geom_jitter(mapping = aes(x=Year,y=Rating))+
  facet_wrap(~Genre)+
  annotate(
  "text",
  x=2012,
  y=3.37,
  label="The Casual Vacancy by JK Rowling (3.3)",
  size=2.3,
  fontface="bold")
  • 1
    Could you edit your question to be reproducible? Good luck! – jpsmith Jul 26 '23 at 17:45
  • Welcome to SO, Jordan Omar Roche! Questions on SO (especially in R) do much better if they are reproducible and self-contained. By that I mean including attempted code (you have this, thanks!), sample representative data (perhaps via `dput(head(x))` or building data programmatically (e.g., `data.frame(...)`), possibly stochastically), perhaps actual output (with verbatim errors/warnings) versus intended output. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Jul 26 '23 at 17:58
  • 1
    You might come close replacing your annotate with something like `geom_text(aes(label=lbl), data=data.frame(Year=2012, Rating=3.37, lbl="Casual Vacancy ..."))`. (If that works, I encourage saving that 1-row frame into a formal variable. Not required, but "cleaner code".) – r2evans Jul 26 '23 at 18:00
  • @r2evans - Thanks! Being a newbie on SO I will look to improve my questions. I'm working through a Data Analytics course and figuring out as I go. As far as label=lbl, can you explain what lbl is? – Jordan Omar Roche Jul 26 '23 at 18:10
  • 1
    in r2evans's example, the `geom_text` layer is receiving its own data source, a data.frame with a column called `lbl` (it could be called anything), and that layer also has a mapping `aes(label=lbl)` which tells the layer to use that `lbl` column from that data.frame for the text to be shown. – Jon Spring Jul 26 '23 at 18:14
  • 1
    ^ I think you'll also want the data.frame provided to that layer to include `Genre = "Fiction"` so that it gets sent to only that facet – Jon Spring Jul 26 '23 at 18:20
  • Thanks for explaining and helping to clarify my comment code, @JonSpring, I appreciate the follow-through (I had stepped away). For completeness: `geom_text(aes(label=lbl), data=data.frame(Year=2012, Rating=3.37, Genre="Fiction", lbl="Casual Vacancy ..."))` – r2evans Jul 26 '23 at 18:23

0 Answers0