0

I need to plot the amound of found specimen of violet over time. In the legend, I want to show the 2 observed species as well as the sum of them. To write scientific correct, I also need Italic for "Viola" in "Sum of Viola spec.".

I want actually the same as theforestecologist in his topright legend, but I can't reproduce his example with words. So I did it in an easy way using text.font(), but with this the whole line is italic. Abbreviations of the code I used in main() didn't help. Is it possible, to write only a part a line in the legend in italic?

vr <- sample(1:100, 10, replace = TRUE)
vo <- sample(1:100, 10, replace = TRUE)
date <- Sys.Date() + sort(sample(1:10, 10))
vs <- vr + vo
df <- data.frame(vr, vo, date, vs)

plot(
  vs ~ date,
  data = df,
  type = "o",
  ylim = c(0, max(df$vs)),
  col = "black",
  pch = 13,
  cex = 1.2,
  lwd = 2,
  main = expression(paste(italic('Viola'), " spec. [ind]")),
  ylab = "Amount"
)
lines(
  vr ~ date,
  data = df,
  type = "o",
  col = "RoyalBlue2",
  pch = 1,
  cex = 1.2
)
lines(
  vo ~ date,
  data = df,
  type = "o",
  col = "springgreen3",
  pch = 4,
  cex = 1.2
)
legend(
  "topright",
  inset = c(0.01 , 0.02),
  c("Sum of Viola spec.", "Viola odorata", "Viola reichenbachiana"),
  xpd = TRUE,
  pch = c(13, 1, 4),
  pt.cex = 1.2,
  col = c(1, "RoyalBlue2", "springgreen3"),
  cex = .9,
  y.intersp = .9,
  bty = "l",
  bg = rgb(244, 248, 249, max = 255),
  text.font = c(1, 3, 3)
)
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
Qiyuan
  • 109
  • 10

1 Answers1

1

Why not use expression() and italic() as you did for your title ?

legend(
  "topright", 
  inset=c(0.01 ,0.02), 
  c(expression(paste("Sum of ", italic('Viola'), " spec.")), 
    expression(italic("Viola odorata")), 
    expression(italic("Viola reichenbachiana"))
    ), 
  xpd = TRUE,
  pch = c(13, 1, 4), pt.cex=1.2, 
  col = c(1,"RoyalBlue2", "springgreen3"), 
  cex = .9, y.intersp=.9, bty = "l", 
  bg = rgb(244, 248, 249, max = 255), 
  text.font = c(1,3,3)
)

enter image description here

demarsylvain
  • 2,103
  • 2
  • 14
  • 33
  • I don't really know how I missed that. I definitely tried it, but I only tried to use `expression()` in that single line and that must have been my mistake. Thank you very much! – Qiyuan Mar 28 '19 at 11:17