我正在尝试用ggplot2绘制我的数据,但我的x轴数据应该进行对数转换.我尝试使用函数coord_trans因为我想保留原始标签并只是转换我的坐标,但是当我这样做时,我的所有点都会从我的图形中消失.我不知道为什么会这样?可能是因为我的数据中有零吗?我该如何解决这个问题?
这是我制作ggplot的输入:
library(afex)
library(car)
library(MASS)
library(rockchalk)
library(multcomp)
library(lsmeans)
library(gplots)
library(ggplot2)
library(tidyr)
library(effects)
library(scales)
library(ggthemes)
library(gtools)
theme_set(theme_few(base_size=14))
p=ggplot(data, aes(x=day, y=(propinfected), linetype=treatment, group=treatment)
p + geom_smooth(aes(fill=treatment),colour="black", method="glm",
family="quasibinomial")+
coord_trans(xtrans = "log10")+
geom_point(aes(fill=treatment),size=5,pch=21)+
labs(x="Day", y="Above detection treshold", size=10)+
scale_y_continuous(labels= percent,breaks=seq(0,1,by=0.2),limits=c(0,1))+
scale_x_continuous(breaks=seq(0,16,by=4),limits=c(0,16))+
scale_fill_manual(values=c("black","grey"))+
theme(legend.justification=c(1,0), legend.position=c(1,0),
legend.title=element_blank(),axis.text=element_text(size=20),
axis.title=element_text(size=20),legend.text=element_text(size=15))
这是我使用的模型:
fitlog=glm(propinfected~log10(day+1)*treatment,family=quasibinomial(link=logit), data=data)
这是我使用的数据:
dput(data)
structure(list(treatment = structure(c(1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L), .Label = c("CTRL", "DWV"), class = "factor"),
day = c(0L, 0L, 4L, 4L, 8L, 8L, 12L, 12L, 16L, 16L), infected = c(0L,
20L, 11L, 20L, 15L, 18L, 16L, 19L, 19L, 19L), not_infected = c(20L,
0L, 9L, 0L, 5L, 2L, 4L, 1L, 1L, 1L), propinfected = c(0,
1, 0.55, 1, 0.75, 0.9, 0.8, 0.95, 0.95, 0.95)), .Names = c("treatment",
"day", "infected", "not_infected", "propinfected"), row.names = c(NA,
-10L), class = "data.frame")
当我用allEffects函数绘制我的图形时,我得到正确的图,具有正确的线条和置信带.
但我想在ggplot中这样做,因为allEffects的情节并不那么漂亮.
plot(allEffects(fitlog),ylab="Above detection treshold",type="response")
谢谢你的帮助!
最佳答案 如果将geom_smooth中的公式定义为适合glm的模型的公式,则根本不需要coord_trans.在你的情况下,它将是y~log10(x 1).
ggplot(data, aes(x=day, y=propinfected, linetype=treatment, group=treatment)) +
geom_smooth(aes(fill=treatment), formula = y ~ log10(x + 1),
colour="black", method="glm",
method.args = list(family="quasibinomial")) +
geom_point(aes(fill=treatment),size=5,pch=21) +
labs(x="Day", y="Above detection treshold", size=10) +
scale_y_continuous(labels= percent,breaks=seq(0,1,by=0.2),limits=c(0,1)) +
scale_x_continuous(breaks=seq(0,16,by=4),limits=c(0,16)) +
scale_fill_manual(values=c("black","grey")) +
theme(legend.justification=c(1,0), legend.position=c(1,0),
legend.title=element_blank(), axis.text=element_text(size=20),
axis.title=element_text(size=20), legend.text=element_text(size=15))
要按照以前的方式进行,您实际上需要使用适当的scale_x函数,然后使用coord_trans.我总是需要一段时间来思考这个问题,来自coord_trans帮助页面的这句话总能帮助我:
The difference between transforming the scales and
transforming the coordinate system is that scale
transformation occurs BEFORE statistics, and coordinate
transformation afterwards.
因此,如果要在拟合模型之前变换x变量,则需要使用比例进行变换.要以原始比例显示所有内容,您需要进行坐标转换.所以你最终会用两者来获得你想要的最终产品.
这是使用scale_x_log10和coord_trans的简短示例:
ggplot(data, aes(x=day + 1, y=(propinfected), linetype=treatment, group=treatment)) +
geom_smooth(aes(fill=treatment),
colour="black", method="glm",
method.args = list(family="quasibinomial")) +
geom_point(aes(fill=treatment),size=5,pch=21) +
labs(x="Day", y="Above detection treshold", size=10) +
scale_y_continuous(labels= percent,breaks=seq(0,1,by=0.2),limits=c(0,1)) +
scale_x_log10(labels = seq(0,16,by = 4), breaks=seq(1,17,by=4), limits=c(1,17)) +
coord_trans(x = exp_trans(base = 10))