0

我想知道是否有人知道为什么会这样。这是代码和屏幕截图的快速示例。

library(ggplot2)
iris$randomratio <- iris$Sepal.Length/iris$Sepal.Width
plot_iris <- ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color = Species)) + 
        geom_point() + 
        theme_light() + 
        ggrepel::geom_text_repel(data=subset(iris, randomratio > 2.7), 
                                 aes(Sepal.Length, Sepal.Width, label= Species))
plot_iris
cowplot::plot_grid(plot_iris, plot_iris, label = c('A', 'B'))

现在,当我在 RMD 文件中执行此操作时,我得到了我想要的标记点(对)但是当我尝试在 Rstudio 笔记本输出中执行此操作时,我得到以下信息: 在此处输入图像描述

注意它不适用于 ggplot 或 cowplot。使用 cowplot 它也不适用于常规文件。

有谁知道我该如何解决这个问题?

在此处输入图像描述

4

1 回答 1

0

我看到一个带有“cowplot”的警告,但是一个带有文本标签的图形。

Warning message:
In as_grob.default(plot) :
  Cannot convert object of class character into a grob.

我没有看到“拼凑而成”的警告。您需要注意,geom_text()如果要避免文本与点重叠,而不是对数据进行子设置,您必须将标签设置为“”。减小标签的大小有助于排斥。在编写情节时收集相同的图例也可以提供更多空间。下面的代码在 R 4.1.0 和最新版本的包下很好地呈现给 HTML 笔记本。

library(ggplot2)
library(patchwork)

iris$randomratio <- iris$Sepal.Length/iris$Sepal.Width
iris$label <- ifelse(iris$randomratio > 2.7, as.character(iris$Species), "")
plot_iris <- 
  ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color = Species)) + 
  geom_point() + 
  theme_light() + 
  ggrepel::geom_text_repel(aes(label = label), size = 3, min.segment.length = 0)

plot_iris + plot_iris + 
  plot_annotation(tag_levels = 'A') +
  plot_layout(guides = 'collect')

reprex 包于 2021-08-03 创建 (v2.0.0 )

于 2021-08-03T20:52:35.233 回答