【问题标题】:Adding italicised r with correlation coefficient to a scatter plot chart in ggplot将带有相关系数的斜体r添加到ggplot中的散点图
【发布时间】:2015-09-29 00:50:10
【问题描述】:

我正在尝试使用下面的代码生成一个简单的散点图,其相关系数将在图上放置斜体 r

data(mtcars)

# Load required libraries
require(ggplot2)               # To derive the graphs
require(ggthemes)              # To apply ggplot themes to the chart
require(scales)                # For pretty breaks

# Function to generate correlation coefficient for the charts
corr_eqn <- function(x,y, digits = 2) {
  corr_coef <- round(cor(x, y), digits = digits)
  corr_coef <- expression(paste(italic(r)," = ", corr_coef))
  return(corr_coef)
}

# Provide a scatter plot for income and health deprivation
ggplot(mtcars, aes(x = drat, y = wt)) +
  geom_point(shape = 19, size = 2, aes(colour = as.factor(cyl))) +
  geom_smooth(colour = "red", fill = "lightgreen", method = 'lm') +
  ggtitle("Example") +
  xlab("drat") +
  ylab("wt") +
  scale_colour_tableau("tableau10") +
  geom_text(x = 3, y = 3,
            label = corr_eqn(mtcars$drat,
                             mtcars$wt), parse = TRUE) +
  theme(legend.key = element_blank(),
        legend.background = element_rect(colour = 'black'),
        legend.position = "bottom",
        legend.title = element_blank(),
        plot.title = element_text(lineheight = .8, face = "bold", vjust = 1),
        axis.text.x = element_text(size = 11, vjust = 0.5,
                                   hjust = 1, colour = 'black'),
        axis.text.y = element_text(size = 11, colour = 'black'),
        axis.title = element_text(size = 10, face = 'bold'),
        axis.line = element_line(colour = "black"),
        plot.background = element_rect(colour = 'black', size = 1),
        panel.background = element_blank())

代码在控制台中以? 标记停止。运行代码:

#   geom_text(x = 3, y = 3,
#             label = corr_eqn(mtcars$drat, mtcars$wt), parse = TRUE) +

注释,生成如下图表:

我猜我生成格式为 r = 0.7 的方程的函数不起作用,我该如何解决?

【问题讨论】:

  • @MYaseen208 事实上,我试图使用链接问题中的讨论来编写我的函数,但它不起作用。很明显,我做错了什么。

标签: r ggplot2 annotations expression scatter-plot


【解决方案1】:

正如您所怀疑的,您只需要调整您的功能。你可以使用substitute,如this answer,但你也可以在这里使用paste

corr_eqn <- function(x,y, digits = 2) {
  corr_coef <- round(cor(x, y), digits = digits)
  paste("italic(r) == ", corr_coef)
}

请注意,如果您将 as.character 添加到原始函数返回的内容中,则内容将被解析。但是,结果将是 corr_coef 作为字符串,而不是您想要的实际相关系数。

我还应该补充一点,如果您不将标签和坐标放入新的 data.frame,geom_text 可能会导致分辨率下降。

labels = data.frame(x = 3, y = 3, label = corr_eqn(mtcars$drat, mtcars$wt))

然后使用data 参数和aesgeom_text

geom_text(data = labels, aes(x = x, y = y,
                        label = label), parse = TRUE)

参见annotategeom = "text" 作为避免新data.frame 的另一个选项。

【讨论】:

  • 非常感谢您的全面回答。调整字体会不会很困难,比如改变大小和字体?
  • @Konrad 使用parse = TRUE 时,您绝对可以使用sizefamily 参数更改geom_text 中标签的字体大小和系列,但不能更改字体。跨度>
  • corr_eqn &lt;- function(x,y, digits = 2) { + corr_coef &lt;- round(cor(x, y), digits = digits) + paste("italic(r) == ", corr_coef) + } labels = data.frame(x = 3, y = 3, label = corr_eqn(mtcars$drat, mtcars$wt)) 产生一个只有一行的df ` x y label 3 3 italic(r) == -0.71`
  • 只是为了强调 aosmith 的观点,标签不仅必须存储在数据框中,而且还必须在 geom_text 内部传递,或者换句话说,传递labels$labelgeom_text 不够好。
猜你喜欢
  • 2015-08-02
  • 1970-01-01
  • 1970-01-01
  • 2022-01-03
  • 2013-05-05
  • 1970-01-01
  • 1970-01-01
  • 2020-08-27
  • 1970-01-01
相关资源
最近更新 更多