How to add a complex label with italics and a variable to ggplot?

拟墨画扇 提交于 2020-01-11 03:08:27

问题


I have read many postings on this topic using expression(), paste(), and bquote(), or some combination. I think I am close to solving my problem, but I just can't get there. The following script generates a plot labelled with "y = 1 + 2(x); r^2= 0.9". How can I italicize "y" and "x", and italicize the "r" and superscript the 2 of "r^2"? If I have overlooked a relevant earlier post, sorry, but please direct me to it.

df <- data.frame(x=c(1:5), y=c(1:5))
a <- 1    
b <- 2
r2 <- 0.9
eq <- paste("y = ", a, " + ", b, "(x); r^2=", r2)
ggplot(data=df, aes(x=x, y=y))+
  geom_point(color="black")+
  geom_text(x=2, y=4,label=eq, parse=FALSE)

回答1:


You could use annotate() which allows you to paste directly into the plot.

library(ggplot2)
ggplot(data=df, aes(x=x, y=y)) +
  geom_point(color="black") +
  annotate('text', 2.5, 4, 
           label=paste("italic(y)==", a, "+", b, 
                       "~italic(x)~';'~italic(r)^2==", r2), 
           parse=TRUE, 
           hjust=1, size=5)

Yields:

Data:

df <- data.frame(x=c(1:5), y=c(1:5))
a <- 1
b <- 2
r2 <- 0.9



回答2:


You can use a combination of substitute and plotmath (https://www.rdocumentation.org/packages/grDevices/versions/3.5.1/topics/plotmath) to italicize the text-

# setup
set.seed(123)
library(ggplot2)

# dataframe
df <- data.frame(x = c(1:5), y = c(1:5))

# label
eq <- substitute(
  expr =
    paste(
      italic("y"),
      " = ",
      a,
      " + ",
      b,
      "(",
      italic("x"),
      "); ",
      italic("r") ^ 2,
      " = ",
      r2
    ),
  env = base::list(a = 1,
                   b = 2,
                   r2 = 0.9)
)

# plot
ggplot(data = df, aes(x = x, y = y)) +
  geom_point(color = "black") +
  labs(subtitle = eq)

Created on 2018-12-04 by the reprex package (v0.2.1)




回答3:


In addition to the answer by Indrajit Patil & jay-sf, I would like to add that there is an automated way to fit regression lines (I believe there are many), using a package called ggpmisc. The letters that you want in italic, are already formatted in such a way. The code that needs to be used is:

> install.packages('ggpmisc'); library(ggpmisc); formula <- y ~ x
> df <- data.frame(x=c(1:5), y=c(1:5))
> ggplot(data = df, aes(x, y)) + geom_point(color="black") + 
       geom_smooth(method =   "lm", formula = formula) + 
       stat_poly_eq(aes(label =   paste(..eq.label.., ..adj.rr.label.., sep = "~~~~")),
       formula = formula, parse = TRUE)

It shows the fitted lines also, which I hope is not an impediment to the main goal.

EDIT: The line can be removed using linetype = 0, compatible with most of the aesthetics in ggplot2.

... + geom_smooth(method =   "lm", formula = formula, linetype = 0) + ...


来源:https://stackoverflow.com/questions/53620614/how-to-add-a-complex-label-with-italics-and-a-variable-to-ggplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!