I am trying to add R2 and RMSE to a facetted ggplot
. I am using the following code for that
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)
which gives me the following plot
As we can see from the plot, the species column of the geom_table_npc
is unnecessary. Now how can I get the plot as following
My answer has two parts. The first part suggests that you keep using
geom_table_npc
to add your information. While the second part explains how one can have the output you asked for.First, you can simply remove the column from your results.
So, if I run this code
I get this output.
Second, you can use either
geom_text()
orannotate()
to achieve your desired output. Let's usegeom_text()
.Gives the following plot.
HTH!