gpt4 book ai didi

r - 在ggplot方面绘制不同值的垂直线

转载 作者:行者123 更新时间:2023-12-02 04:13:16 25 4
gpt4 key购买 nike

我有这样的数据

> dput(testdat)
structure(list(Type = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Saline",
"Compound 1"), class = "factor"), Treatment = structure(c(1L,
2L, 3L, 4L, 6L, 5L), .Label = c(".0032uM", ".016uM", ".08uM",
".4uM", "2uM", "10uM"), class = "factor"), Peak = c(1071.28430020209,
1458.23366806524, 2714.49856342393, 3438.83453920159, 3938.86391759534,
2980.10159109856), Area1 = c(3312.99749863082, 4798.35142770291,
9044.21362002965, 11241.1497514069, 11575.3444645068, 9521.69011119236
), SS1 = c(781.759834505516, 1191.6273298958, 2180.02082601411,
2601.33855989239, 2492.11886600804, 2185.39715502702), Conc = c(0.0032,
0.016, 0.08, 0.4, 10, 2), logconc = c(-2.49485002168009, -1.79588001734408,
-1.09691001300806, -0.397940008672038, 1, 0.301029995663981),
Conc_nm = c(3.2, 16, 80, 400, 10000, 2000), logconc_nm = c(0.505149978319906,
1.20411998265592, 1.90308998699194, 2.60205999132796, 4,
3.30102999566398)), .Names = c("Type", "Treatment", "Peak",
"Area1", "SS1", "Conc", "logconc", "Conc_nm", "logconc_nm"), row.names = 2:7, class = "data.frame")

代码如下:

testdat$Conc_nm = as.numeric(gsub("([0-9]+).*$", "\\1", testdat$Treatment))*1000
testdat$logconc_nm = log10(testdat$Conc_nm)
testdatMelt = melt(testdat,id.vars = c('Type','Treatment','Conc','logconc','Conc_nm','logconc_nm'))



val=NULL # EC50
vallog=NULL# logEC50
allDR=NULL
for (i in 3:5){
currentfit=tryCatch(nls(testdat[,i] ~ SSfpl(logconc_nm,A,B,xmid,scal),dat=testdat),error=function(e) 0)
if(typeof (currentfit)=='list')
vallog[i]= summary(currentfit)$coefficients[3]
val[i]=10^summary(currentfit)$coefficients[3]
}
vallog=vallog[-c(1:2)]
val=val[-c(1:2)]


ggplot(data = testdatMelt,aes(logconc_nm,value))+
facet_grid(.~variable)+
geom_point()+
scale_x_log10(breaks=round(testdat$logconc_nm,2))+
geom_smooth(method = 'nls',
formula = y ~ SSfpl(x,A,B,xmid,scal),se=FALSE)+
geom_vline(color='red',xintercept = 20,alpha=.5)
geom_text(aes(x=valog-.225,y=9000,color=variable,label = paste('EC50',val,'nM')))

这会根据变量生成 3 个面,这非常好,并且它可以用自己的 NLS 拟合来拟合每个面。我的问题是如何在 logEC50 值 (val) 处绘制一条垂直线并用 EC50 (val) 注释该线

我的想法是在 geom_line() 参数内使用 for 循环,但这不起作用。 IE ggplot+geom_line(color='red', xintercept = for (i in 1:3){vallog[i]}) 。这显然是行不通的。还有其他想法吗?

最佳答案

您可以通过创建具有适当结构的数据框来在ggplot2中添加图层。在这种情况下,您希望在每个面板的不同位置有一条垂直线。这意味着您需要一个带有变量的数据框,该变量指示特定行适用于哪个面板,以及相应的截距:

vert_line_df <- data.frame(variable = c('Peak','Area1','SS1'),
vallog = vallog)

然后使用该特定数据在绘图中添加一个图层:

+ geom_vline(data = vert_line_df,aes(xintercept = vallog),color = "black")

关于r - 在ggplot方面绘制不同值的垂直线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35322711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com