[英]ggplot - abline disappears when limiting x and y
我有以下数据:
set.seed(123456)
Test_1 <- round(rnorm(20,mean=40,sd=5),0)/100
Test_2 <- round(rnorm(20,mean=60,sd=5),0)/100
ei.data <- as.data.frame(cbind(Test_1,Test_2))
intercept <- as.data.frame(matrix(0,20,1))
slope <- as.data.frame(matrix(0,20,1))
data <- cbind(intercept,slope)
colnames(data) <- c("intercept","slope")
for (i in 1:nrow(ei.data)){
data[i,1] <- (ei.data[i,2]/(1-ei.data[i,1]))
data[i,2] <- (ei.data[i,2]/(1-ei.data[i,2]))
}
现在,我想在点图中绘制来自ei.data数据框的数据,然后使用abline
以线的形式添加来自数据数据框的日期。
ei <- ggplot(ei.data, aes(Test_1,Test_2))+
geom_point()+
theme_bw()+
scale_y_continuous(limits = c(0, 1))+
scale_x_continuous(limits = c(0, 1))+
geom_abline(slope =data[1,2] , intercept =data[1,1])
ei
但是,我的问题是,如果我将x
和y
比例限制为[0,1]
则使用abline
创建的行abline
消失。 即使限制x
和y
我仍如何绘制线条?
正如您对问题的评论所言,放大您所在地区的那部分将使您的腹泻始终最大程度地消失。 因此,这个答案可能不是100%令人满意的。 尽管如此,您仍然可以更改限制,也可以突出显示abline的一小部分,以便在不更改限制的情况下更容易看到。
这是一个例子:
# Changing the limits
ggplot(ei.data, aes(Test_1,Test_2))+
geom_point()+
theme_bw()+
scale_y_continuous(limits = c(0, 2))+
scale_x_continuous(limits = c(0, 2))+
geom_abline(slope =data[1,2] , intercept =data[1,1])
# Not changing the limits, but making it visible more easily
ggplot(ei.data, aes(Test_1,Test_2))+
geom_point()+
theme_bw()+
scale_y_continuous(limits = c(0, 1))+
scale_x_continuous(limits = c(0, 1))+
geom_abline(slope =data[1,2] , intercept =data[1,1],
color="red", size=2)+
geom_segment(aes(x = 0.2, y = 0.75, xend = 0.05, yend = 0.9),
colour='red', size=2,arrow = arrow(length = unit(0.5, "cm")))+
annotate("text", label="Mind the abline!", x=0.25, y=0.74)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.