我正在尝试将R2和RMSE添加到多面的ggplot
中。为此,我使用了以下代码
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
的物种列是不必要的。现在我怎样才能得到如下图
发布于 2020-12-16 19:11:25
我的答案分为两部分。第一部分建议您继续使用geom_table_npc
添加信息。而第二部分解释了如何获得您所要求的输出。
首先,您可以简单地从结果中删除该列。
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)
所以,如果我运行这段代码
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()
。
# ... 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))
给出了下面的图。
哈!
https://stackoverflow.com/questions/65321984
复制相似问题