R ggplot 中的 kolmogorov-smirnov 图
kolmogorov-smirnov plot in R ggplot
我正在尝试在 r 中绘制 KS 图,一切似乎都很顺利——除了我只能使用颜色来可视化两个不同的样本而不是线型。
我尝试了以下方法:
sample1<-SD13009
sample2<-SD13009PB
group <- c(rep("sample1", length(sample1)), rep("sample2", length(sample2)))
dat <- data.frame(KSD = c(sample1,sample2), group = group)
cdf1 <- ecdf(sample1)
cdf2 <- ecdf(sample2)
minMax <- seq(min(sample1, sample2), max(sample1, sample2), length.out=length(sample1))
x0 <- minMax[which( abs(cdf1(minMax) - cdf2(minMax)) == max(abs(cdf1(minMax) - cdf2(minMax))) )]
y0 <- cdf1(x0)
y1 <- cdf2(x0)
#尝试 1
plot<-ggplot(dat, aes(x = KSD, group = group, colour = group, linetype=group))+
stat_ecdf(size=1) +
mytheme + xlab("mm") +scale_x_continuous(limits=c(0,1))+
ylab("Cumulitive Distibution") +
#geom_line(aes(group=group,size=1)) +
geom_segment(aes(x = x0[1], y = y0[1], xend = x0[1], yend = y1[1]),
linetype = "dashed", color = "red") +
geom_point(aes(x = x0[1] , y= y0[1]), color="red", size=1) +
geom_point(aes(x = x0[1] , y= y1[1]), color="red", size=1) +
ggtitle("K-S Test: Sample 1 / Sample 2")
#attempt 2
cdf <- ggplot(dat, aes(x=KSD, group=group,linetype=group)) + stat_ecdf(aes(linetype=group)) + coord_cartesian(xlim = c(0, 0.8)) + geom_segment(aes(x = x0[1], y = y0[1], xend = x0[1], yend = y1[1]),
linetype = "dashed", color = "red") +
geom_point(aes(x = x0[1] , y= y0[1]), color="red", size=1) +
geom_point(aes(x = x0[1] , y= y1[1]), color="red", size=1) +
ggtitle("K-S Test: Sample 1 / Sample 2")
这是我得到的:
我无法使用以下代码重现此内容:
# Make two random samples
sample1<-rnorm(1000)
sample2<-rnorm(1000, 2, 2)
group <- c(rep("sample1", length(sample1)), rep("sample2", length(sample2)))
dat <- data.frame(KSD = c(sample1,sample2), group = group)
cdf1 <- ecdf(sample1)
cdf2 <- ecdf(sample2)
minMax <- seq(min(sample1, sample2), max(sample1, sample2), length.out=length(sample1))
x0 <- minMax[which( abs(cdf1(minMax) - cdf2(minMax)) == max(abs(cdf1(minMax) - cdf2(minMax))) )]
y0 <- cdf1(x0)
y1 <- cdf2(x0)
ggplot(dat, aes(x = KSD, group = group, colour = group, linetype=group))+
stat_ecdf(size=1) +
xlab("mm") +
ylab("Cumulitive Distibution") +
geom_segment(aes(x = x0[1], y = y0[1], xend = x0[1], yend = y1[1]),
linetype = "dashed", color = "red") +
geom_point(aes(x = x0[1] , y= y0[1]), color="red", size=1) +
geom_point(aes(x = x0[1] , y= y1[1]), color="red", size=1) +
ggtitle("K-S Test: Sample 1 / Sample 2")
在你的情节中,线条似乎靠得很近,你看不出它们是不同的线型,但它们是。
我正在尝试在 r 中绘制 KS 图,一切似乎都很顺利——除了我只能使用颜色来可视化两个不同的样本而不是线型。
我尝试了以下方法:
sample1<-SD13009
sample2<-SD13009PB
group <- c(rep("sample1", length(sample1)), rep("sample2", length(sample2)))
dat <- data.frame(KSD = c(sample1,sample2), group = group)
cdf1 <- ecdf(sample1)
cdf2 <- ecdf(sample2)
minMax <- seq(min(sample1, sample2), max(sample1, sample2), length.out=length(sample1))
x0 <- minMax[which( abs(cdf1(minMax) - cdf2(minMax)) == max(abs(cdf1(minMax) - cdf2(minMax))) )]
y0 <- cdf1(x0)
y1 <- cdf2(x0)
#尝试 1
plot<-ggplot(dat, aes(x = KSD, group = group, colour = group, linetype=group))+
stat_ecdf(size=1) +
mytheme + xlab("mm") +scale_x_continuous(limits=c(0,1))+
ylab("Cumulitive Distibution") +
#geom_line(aes(group=group,size=1)) +
geom_segment(aes(x = x0[1], y = y0[1], xend = x0[1], yend = y1[1]),
linetype = "dashed", color = "red") +
geom_point(aes(x = x0[1] , y= y0[1]), color="red", size=1) +
geom_point(aes(x = x0[1] , y= y1[1]), color="red", size=1) +
ggtitle("K-S Test: Sample 1 / Sample 2")
#attempt 2
cdf <- ggplot(dat, aes(x=KSD, group=group,linetype=group)) + stat_ecdf(aes(linetype=group)) + coord_cartesian(xlim = c(0, 0.8)) + geom_segment(aes(x = x0[1], y = y0[1], xend = x0[1], yend = y1[1]),
linetype = "dashed", color = "red") +
geom_point(aes(x = x0[1] , y= y0[1]), color="red", size=1) +
geom_point(aes(x = x0[1] , y= y1[1]), color="red", size=1) +
ggtitle("K-S Test: Sample 1 / Sample 2")
这是我得到的:
我无法使用以下代码重现此内容:
# Make two random samples
sample1<-rnorm(1000)
sample2<-rnorm(1000, 2, 2)
group <- c(rep("sample1", length(sample1)), rep("sample2", length(sample2)))
dat <- data.frame(KSD = c(sample1,sample2), group = group)
cdf1 <- ecdf(sample1)
cdf2 <- ecdf(sample2)
minMax <- seq(min(sample1, sample2), max(sample1, sample2), length.out=length(sample1))
x0 <- minMax[which( abs(cdf1(minMax) - cdf2(minMax)) == max(abs(cdf1(minMax) - cdf2(minMax))) )]
y0 <- cdf1(x0)
y1 <- cdf2(x0)
ggplot(dat, aes(x = KSD, group = group, colour = group, linetype=group))+
stat_ecdf(size=1) +
xlab("mm") +
ylab("Cumulitive Distibution") +
geom_segment(aes(x = x0[1], y = y0[1], xend = x0[1], yend = y1[1]),
linetype = "dashed", color = "red") +
geom_point(aes(x = x0[1] , y= y0[1]), color="red", size=1) +
geom_point(aes(x = x0[1] , y= y1[1]), color="red", size=1) +
ggtitle("K-S Test: Sample 1 / Sample 2")
在你的情节中,线条似乎靠得很近,你看不出它们是不同的线型,但它们是。