首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何格式化添加到ggplot中的R2和RMSE表?

如何格式化添加到ggplot中的R2和RMSE表?
EN

Stack Overflow用户
提问于 2020-12-16 19:01:50
回答 1查看 272关注 0票数 1

我正在尝试将R2和RMSE添加到多面的ggplot中。为此,我使用了以下代码

代码语言:javascript
运行
复制
library(caret)
library(tidyverse)
library(ggpmisc)

summ <- iris %>% 
  group_by(Species) %>% 
  summarise(Rsq = R2(Sepal.Length, Petal.Length),
            RMSE = RMSE(Sepal.Length, Petal.Length)) %>% 
  mutate_if(is.numeric, round, digits=2) 

my.formula <- y ~ x

p <- ggplot(data=iris, aes(x = Sepal.Length, y = Petal.Length)) +
  geom_point(color="blue",alpha = 1/3) + 
  facet_wrap(Species ~ ., scales="free") +
  geom_smooth(method=lm, fill="black", formula = my.formula) +
  xlab("Sepal Length") +
  ylab("Petal Length") + theme_bw() +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) 

p + geom_table_npc(data = summ,label = split(summ, summ$Species),
                   npcx = 0.00, npcy = 1, hjust = 0, vjust = 1, size=3,
                   table.theme = ttheme_gtlight)

这给出了下面的图

从图中我们可以看到,geom_table_npc的物种列是不必要的。现在我怎样才能得到如下图

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-16 19:11:25

我的答案分为两部分。第一部分建议您继续使用geom_table_npc添加信息。而第二部分解释了如何获得您所要求的输出。

首先,您可以简单地从结果中删除该列。

代码语言:javascript
运行
复制
p + geom_table_npc(data = summ, label = lapply(split(summ, summ$Species),
                                               FUN = function(entry) {subset(entry, select = -Species)}),
                   npcx = 0.00, npcy = 1, hjust = 0, vjust = 1, size=3,
                   table.theme = ttheme_gtlight)

所以,如果我运行这段代码

代码语言:javascript
运行
复制
library(caret)
library(tidyverse)
library(ggpmisc)

summ <- iris %>% 
  group_by(Species) %>% 
  summarise(Rsq = R2(Sepal.Length, Petal.Length),
            RMSE = RMSE(Sepal.Length, Petal.Length)) %>% 
  mutate_if(is.numeric, round, digits=2) 

p <- ggplot(data=iris, aes(x = Sepal.Length, y = Petal.Length)) +
  geom_point(color="blue",alpha = 1/3) + 
  facet_wrap(Species ~ ., scales="free") +
  geom_smooth(method=lm, fill="black", formula = y ~ x) +
  xlab("Sepal Length") +
  ylab("Petal Length") + theme_bw() +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) 

# The key is here. By using lapply I remove the Species column from each
# data frame in the list.
p + geom_table_npc(data = summ, label = lapply(split(summ, summ$Species),
                                               FUN = function(entry) {subset(entry, select = -Species)}),
                   npcx = 0.00, npcy = 1, hjust = 0, vjust = 1, size=3,
                   table.theme = ttheme_gtlight)

我得到了这个输出。

其次,您可以使用geom_text()annotate()来实现所需的输出。让我们使用geom_text()

代码语言:javascript
运行
复制
# ... the other code - plot creation - from above.

# Here we create our annotations data frame.
df.annotations <- data.frame()
# Rsq
df.annotations <- rbind(df.annotations,
                        cbind(as.character(summ$Species),
                              paste("Rsq", summ$Rsq,
                                    sep = " = ")))

# RMSE
df.annotations <- rbind(df.annotations,
                        cbind(as.character(summ$Species),
                              paste("RMSE", summ$RMSE,
                                    sep = " = ")))

# This here is important, especially naming the first column
# Species
colnames(df.annotations) <- c("Species", "label")

df.annotations$x <- rep.int(c(4.5, 5.5, 5.5), times = 2)
df.annotations$y <- c(1.75, 5.0, 6.8,
                      1.7, 4.9, 6.7)


p + geom_text(data = df.annotations,
              mapping = aes(x = x, y = y, label = label))

给出了下面的图。

哈!

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65321984

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档