如何在刻面ggplot2中自动定位R2和RMSE?
我正在尝试在facetted如何在刻面ggplot2中自动定位R2和RMSE?,r,ggplot2,R,Ggplot2,我正在尝试在facettedggplot中自动定位R2和RMSE。答案帮助我使用以下代码手动将R2和RMSE放入facettedggplot library(caret) library(tidyverse) summ <- iris %>% group_by(Species) %>% summarise(Rsq = R2(Sepal.Length, Petal.Length), RMSE = RMSE(Sepal.Length, Peta
ggplot
中自动定位R2和RMSE。答案帮助我使用以下代码手动将R2和RMSE放入facettedggplot
library(caret)
library(tidyverse)
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())
# 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))
库(插入符号)
图书馆(tidyverse)
总和%
组别(种类)%>%
总结(Rsq=R2(萼片长度,花瓣长度),
RMSE=RMSE(萼片长度,花瓣长度))%>%
如果(是数字,四舍五入,数字=2)则进行变异
p这不是您想要的,但是您可以将Rsq&RMSE移到方面标题中,例如
# copy species, and change its values to include the Rsq and RMSE. Levels should match if both grouped alphabetically
iris$Species2 <- iris$Species
levels(iris$Species2) <- sprintf("%s \n Rsq=%s \n RMSE=%s",
levels(iris$Species), summ$Rsq, summ$RMSE)
# change facet wrap to Species2
p <- ggplot(data=iris, aes(x = Sepal.Length, y = Petal.Length)) +
geom_point(color="blue",alpha = 1/3) +
facet_wrap(~Species2, 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())
p
#复制物种,并更改其值以包括Rsq和RMSE。如果两个级别都按字母顺序分组,则级别应匹配
iris$Species2基本上我们在geom_text()中将x
和y
设置为-Inf
和+Inf
然后用hjust
和vjust
调整位置。
因为我们有两行:Rsq。。。我们在geom\u text()之前定义vertical\u adjustment
整个代码:
library(caret)
library(tidyverse)
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())
# 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")
vertical_adjustment = ifelse(grepl("Rsq",df.annotations$label),1.5,3)
p + geom_text(data=df.annotations,aes(x=-Inf,y=+Inf,label=label),
hjust = -0.1, vjust = vertical_adjustment, size=3.5)
不,我不想把Rsq&RMSE移到刻面标题中。我已经在我的问题中展示了预期的产出。不用担心。我当时并没有一个解决方案,但我知道另一条路线,所以我想我会和大家分享,以防它也解决了你的问题:)谢谢你的回答。这正是我想要的。您能解释一下ifelse(grepl(“Rsq”,df.annotations$label),1.5,3)的part在做什么吗?特别是值1.5和3在做什么?使用grpl
我们检查标签列中是否有字符串“Rsq”->如果为true,则将其置于垂直位置1.5,否则在这种情况下,垂直位置3上的RMSE支持我想添加另一行,即Rsq、RMSE和NSE,然后这部分如何ifelse(grepl(“Rsq”,df.annotations$label),1.5,3)
是否应该更改?使用ifelse
时,您只会得到true和false,以区分更多的元素,我们应该在时使用case\u。这意味着在df.annotations$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))
vertical_adjustment = ifelse(grepl("Rsq",df.annotations$label),1.5,3)
p + geom_text(data=df.annotations,aes(x=-Inf,y=+Inf,label=label),
hjust = -0.1, vjust = vertical_adjustment, size=3.5)