简体   繁体   中英

Italic and not-italic words in legend in R?

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)
)

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)
)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM