R:ggplot2散点图和效果图之间的重叠
我做了以下二项glm:R:ggplot2散点图和效果图之间的重叠,r,plot,ggplot2,glm,R,Plot,Ggplot2,Glm,我做了以下二项glm: fit_MLPAlog2 = glm(cbind(infected, not_infected)~treatment*log(day+1), family = binomial, data = data) 我想画出下面的图: 我试过了 plot(allEffects(mod = fit_MLPAlog2), type = "response", ylim = c(0,1),lwd = 0.5, ylab = "Above detection treshold", xl
fit_MLPAlog2 = glm(cbind(infected, not_infected)~treatment*log(day+1), family = binomial, data = data)
我想画出下面的图:
我试过了
plot(allEffects(mod = fit_MLPAlog2), type = "response", ylim = c(0,1),lwd = 0.5, ylab = "Above detection treshold", xlab = "Day", multiline = FALSE)
所以我得到了下面的图表:
我还尝试使用ggplot2进行绘图,然后得到数据点:
p1 <- ggplot(data, aes(day, propinfected, colour = treatment)) +
geom_point(size = 3) + geom_smooth(method="glm") +
labs(x = "Day", y = "Above treshold") + theme_bw() +
theme(legend.position = "top", legend.text = element_text(size = 12),
legend.title = element_text(size = 14))
提前谢谢 所以我认为这很接近:
library(reshape2)
set.seed(1234)
rawdata <- data.frame(day = 1:16)
rawdata$CTRL <- c(0, 22,44,55, 56,62,68, 70,75,78, 82,84,87, 89,92,95)/100 +
c(0,rnorm(15,0,0.03))
rawdata$DIW <- pmin(1.0,((160-rawdata$day)/170) + rnorm(16,0,0.02))
#rawdata$CTRLdots <- c(0,55,60,75,77)
data <- melt(rawdata,id.var=c("day"),value.name="propinfected",variable.name="treatment")
clrs <- c("CTRL"="grey","DIW"="grey")
ltyp <- c("CTRL"="solid","DIW"="dotted")
fils <- c("CTRL"="black","DIW"="white")
p1 <- ggplot(data, aes(day, propinfected,
colour = treatment,fill=treatment,linetype=treatment)) +
geom_point(size = 4,shape=21) +
geom_smooth(method="loess",fill=I("grey")) +
scale_y_continuous(limits=c(0,1)) +
scale_color_manual(values=clrs)+
scale_fill_manual(values=fils)+
scale_linetype_manual(values=ltyp)+
labs(x = "Day", y = "Above detection threshold (%)") + theme_bw() +
theme(legend.text = element_text(size = 12),
legend.title = element_text(size = 14),
legend.justification=c(1,0),
legend.position=c(1,0)
)
print(p1)
library(重塑2)
种子集(1234)
rawdata您可以使用dput()将数据集添加到您的帖子中吗?我在下面添加了数据集。@Mike Wise answer有什么问题吗?这是一个很好的答案,但我不太理解R对命令rawdata的作用。谢谢您的提示,但我不明白crls之前会发生什么。你能给我解释一下吗?谢谢。我会的,但在此期间进行表决将不胜感激。我现在需要去吃晚饭。ggplot
通常需要作为“长数据”而不是“宽数据”的数据。看看这篇文章melt所做的只是为您提供一种灵活的方式,将宽数据“融化”为长数据。前三条语句(rawdata
assignments)只是创建与您的数据有点相似的伪数据。没什么特别的。如果你从一开始就提供数据,我就不会这么做了。很高兴能帮上忙。如果您能把它标对,我将不胜感激。
library(reshape2)
set.seed(1234)
rawdata <- data.frame(day = 1:16)
rawdata$CTRL <- c(0, 22,44,55, 56,62,68, 70,75,78, 82,84,87, 89,92,95)/100 +
c(0,rnorm(15,0,0.03))
rawdata$DIW <- pmin(1.0,((160-rawdata$day)/170) + rnorm(16,0,0.02))
#rawdata$CTRLdots <- c(0,55,60,75,77)
data <- melt(rawdata,id.var=c("day"),value.name="propinfected",variable.name="treatment")
clrs <- c("CTRL"="grey","DIW"="grey")
ltyp <- c("CTRL"="solid","DIW"="dotted")
fils <- c("CTRL"="black","DIW"="white")
p1 <- ggplot(data, aes(day, propinfected,
colour = treatment,fill=treatment,linetype=treatment)) +
geom_point(size = 4,shape=21) +
geom_smooth(method="loess",fill=I("grey")) +
scale_y_continuous(limits=c(0,1)) +
scale_color_manual(values=clrs)+
scale_fill_manual(values=fils)+
scale_linetype_manual(values=ltyp)+
labs(x = "Day", y = "Above detection threshold (%)") + theme_bw() +
theme(legend.text = element_text(size = 12),
legend.title = element_text(size = 14),
legend.justification=c(1,0),
legend.position=c(1,0)
)
print(p1)