我正在尝试使用回归模型中的相关数据来注释 ggplot 中的图。我遵循了这篇 SO 帖子中的建议,并尝试修改该函数,以便在图中的换行符中添加几个附加项目。这是我对新功能的尝试:
lm_eqn = function(m){
eq <- substitute(italic(y) == a %.% italic(x)^b*","~~italic(r)^2~"="~r2*","~~italic(n)~"="~nn*","~~italic(p-value)~"="~pv,
list(a = format(exp(coef(m)[1]), digits = 3),
b = format(coef(m)[2], digits = 3),
r2 = format(summary(m)$r.squared, digits = 3),
nn=format(summary(m)$df[2]),
pv=format(summary(m)$coefficients[,4][2])))
as.character(as.expression(eq));
}
Run Code Online (Sandbox Code Playgroud)
它产生预期的输出:全部在一行中。但我想将文本分成两行,第二行以italic(n)=. 但是如果我引入 a \n,它在找到时会抛出错误\n。如果我在引号内引入 \n:"\n"那么它似乎会被忽略并且文本保留在一行中。我还没有找到任何关于如何在这样的表达式中引入换行符的参考。我们将非常感谢您的帮助。
谢谢。
编辑:根据@Tim 的评论,我提出了重写的代码和调整的问题。
我想要进行ggplot对数回归并显示 R2 和 p 值。我尝试过stat_cor,但它只显示线性回归的 R2 和 p 值。我尝试将“formula=y~log(x)”合并到 中stat_cor,但说未知参数:公式。我是否必须使用不同的函数才能实现这一点?
ggplot(data = Data,aes(x=Carbon_per,y=Pyrite_per,col=Ecosystem,shape=Ecosystem)) +
geom_smooth(method='lm', formula=y~log(x))+
geom_point() +
stat_cor(aes(label = paste(..rr.label.., ..p.label.., sep = "~`,`~")))
Run Code Online (Sandbox Code Playgroud)
干杯,格洛丽亚
我正在根据同一图上的众多变量绘制物种的出现.还有很多其他变量,但我只是为了这篇文章而保留了重要的变量:
> str(GH)
'data.frame': 288 obs. of 21 variables:
$ Ee : int 2 2 1 7 6 3 0 9 3 7 ...
$ height : num 14 25.5 25 21.5 18.5 36 18 31.5 28.5 19 ...
$ legumes : num 0 0 55 30 0 0 55 10 30 0 ...
$ grass : num 60 50 30 35 40 35 40 40 35 30 ...
$ forbs : num 40 70 40 50 65 70 40 …Run Code Online (Sandbox Code Playgroud) 我是来自MatLab的R/ggplot新手.
我想使用ggplot创建一个函数,在图上打印线性回归方程(在图中 添加回归线方程和R2中讨论).但是在这里,我试图用它来构建一个函数但是没有成功.
我一直收到错误 - "eval中的错误(expr,envir,enclos):找不到对象'标签'".
一种解决方法是在函数之外定义"label"变量,但我只是不明白为什么这不起作用.
有谁能解释为什么?
df <- data.frame(x = c(1:100))
df$y <- 2 + 3 * df$x + rnorm(100, sd = 40)
f <- function(DS, x, y, z) {
label <- z
print(label)
ggplot(DS, aes(x=x, y=y)) +
geom_point() +
labs(y=y) +
labs(title=y) +
xlim(0,5)+
ylim(0,5)+
geom_smooth(method="lm", se=FALSE)+
geom_text (aes(x=1, y=4, label=label))
}
f(df, x, y, "aaa") #execution line
Run Code Online (Sandbox Code Playgroud) 使用df和下面的代码
library(dplyr)
library(ggplot2)
library(devtools)
df <- diamonds %>%
dplyr::filter(cut%in%c("Fair","Ideal")) %>%
dplyr::filter(clarity%in%c("I1" , "SI2" , "SI1" , "VS2" , "VS1", "VVS2")) %>%
dplyr::mutate(new_price = ifelse(cut == "Fair",
price* 0.5,
price * 1.1))
ggplot(df, aes(x= new_price, y= carat, color = cut))+
geom_point(alpha = 0.3)+
facet_wrap(~clarity, scales = "free_y")+
geom_smooth(method = "lm", se = F)
Run Code Online (Sandbox Code Playgroud)
我有这个情节
感谢@ kdauria对这个问题的回答,我将回归方程和R2添加到如下图中
source_gist("524eade46135f6348140")
ggplot(df, aes(x= new_price, y= carat, color = cut))+
stat_smooth_func(geom="text",method="lm",hjust=0,parse=TRUE)+
geom_point(alpha = 0.3)+
facet_wrap(~clarity, scales = "free_y")+
geom_smooth(method = "lm", se …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建ggplot并添加我所做的相关性测试的结果。类似的东西:
p+annotate("text",x=12.5,y=15.25,label=c(cor.test$estimate,cor.test$p.value))
Run Code Online (Sandbox Code Playgroud)
无论我尝试什么,我都会收到错误消息。有任何想法吗?
可能重复:
ggplot2:在图表上添加回归线方程和R2
我用散点图绘制数据
ggplot(work.rootsfnp.h1, aes(x=fnpltrfac, y=rootsscore, group=1)) +
geom_smooth(method=lm, se = F) + geom_point(shape=1)
Run Code Online (Sandbox Code Playgroud)
是否有一种"快速"方式来添加基本图例,其中包括最佳拟合线的公式以及相关系数?
我比较了三个不同组的分类数据。
我想知道是否可以轻松地将卡方检验的 p 值添加到构面 ggplots 中(因为我正在分析一个大数据集)。我刚刚读到,在比较平均值时,有一种奇妙的方法可以做到这一点https://www.r-bloggers.com/add-p-values-and-significance-levels-to-ggplots/。但是,我找不到其他测试的解决方案(例如我的例子中的 chisq.test )。
d.test <- data.frame(
results = sample(c("A","B","C"), 30, replace =TRUE),
test = sample(c("test1", "test2","test3"), 30, replace = TRUE)
)
chisq.test(d.test$results,d.test$test)
ggplot(d.test, aes(results) ) +
geom_bar() + facet_grid(test ~ .)
Run Code Online (Sandbox Code Playgroud)
非常感谢您的帮助!;D
我不知道如何得到回归线方程,我用函数geom_smooth绘制的线性回归的r ^ 2和p值.
这是我的代码:
g <- ggplot(data=data.male, aes(x=mid_year, y=mean_tc, colour=data.male$survey_type))
g <- g + geom_point(shape = 20, size =2)
g <- g + geom_smooth(method=lm, na.rm = FALSE, se = TRUE, aes(group=1), colour = "black")
g <- g + theme_gray(base_size=24)
g <- g+ xlab("Year")
g <- g + ylab("Mean serum total cholesterol (mmol/L)")
g <- g + theme(legend.position="bottom")
g <- g + scale_y_continuous(limits=c(3.5,6.5), breaks=c(3.5,4,4.5,5,5.5,6,6.5))
g <- g + scale_x_continuous(limits=c(1980,2015), breaks=c(1980,1990,2000,2010))
g <- g + scale_colour_manual(name = "Survey Type", values= c("Red", "Blue", …Run Code Online (Sandbox Code Playgroud) 在这里,我想为简单的线性回归、二阶和三阶多项式模型创建一个公式列表。我就是这样做的。对于一些变量来说可能没问题,但我的数据是很多变量。那么我怎样才能避免使用另一种方式来做同样的事情而使工作过载呢?
ind1.lm <- lm(dep ~ ind1, data = df)
ind1.qd <- lm(dep ~ poly(ind1, 2, raw = TRUE), data = df)
ind1.cb <- lm(dep ~ poly(ind1, 3, raw = TRUE), data = df)
ind2.lm <- lm(dep ~ ind2, data = datAll)
ind2.qd <- lm(dep ~ poly(ind2, 2, raw = TRUE), data = df)
ind2.cb <- lm(dep ~ poly(ind2, 3, raw = TRUE), data = df)
ind3.lm <- lm(dep ~ ind3, data = df)
ind3.qd <- lm(dep ~ poly(ind3, 2, raw …Run Code Online (Sandbox Code Playgroud) 我是r的新手,但我正在将它用于一个项目,我希望在同一个散点图上代表3个不同的y值和相同的x值,包括每个的线性回归线以及值.我不知道到目前为止我所做的是最好的,但是:
leafdata.long<-melt(leafdata, id="Percent.Area.Loss", measure=c("R...mean", "G.mean", "B.mean"))
ggplot(leafdata.long, aes(Percent.Area.Loss, value, color=variable))+
geom_point()+geom_smooth(method=lm, se=FALSE)+opts(title="Compiled Leaf Data")
Run Code Online (Sandbox Code Playgroud)
以下是它制作的情节:http: //imgur.com/eXNFY5d
任何有关更改x和y标签以及图例的帮助也将受到赞赏.我很失落.
我想将回归线方程和 r 平方值添加到我的ggplot2散点图中。
我发现了一个类似的问题,它给出了下面的代码,但是当我通过截距强制回归时它不起作用:
library(devtools)
source_gist("524eade46135f6348140")
df = data.frame(x = c(1:100))
df$y = 2 + 5 * df$x + rnorm(100, sd = 40)
ggplot(data = df, aes(x = x, y = y, label=y)) +
stat_smooth_func(geom="text",method="lm",hjust=0,parse=TRUE, formula=y~x-1) +
geom_smooth(method="lm",se=FALSE, formula=y~x-1) +
geom_point()
Run Code Online (Sandbox Code Playgroud)
通过添加formula=y~x-1,显示的文本显示系数为截距,截距为NA。有没有办法解决这个问题?
r ×12
ggplot2 ×10
regression ×4
lm ×2
annotations ×1
chi-squared ×1
expression ×1
facet ×1
formula ×1
function ×1
ggpubr ×1
label ×1
list ×1
newline ×1
p-value ×1
substitution ×1
text ×1