简体   繁体   English

ggplot-当限制x和y时,绝对消失

[英]ggplot - abline disappears when limiting x and y

I have the following data: 我有以下数据:

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]))
  }

Now I want to plot the data from the ei.data data frame in a point plot and then add the date from the data data frame in form of lines using abline . 现在,我想在点图中绘制来自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

However, the problem I have is, that if I limit x and y scale to [0,1] the line I created using abline disappears. 但是,我的问题是,如果我将xy比例限制为[0,1]则使用abline创建的行abline消失。 How can I still plot the lines even when I am limiting x and y ? 即使限制xy我仍如何绘制线条?

As the comment on your question already says, zooming in on that part of your region will make your abline always dissapear to the greatest extend. 正如您对问题的评论所言,放大您所在地区的那部分将使您的腹泻始终最大程度地消失。 So this answer might not be 100% satisfying. 因此,这个答案可能不是100%令人满意的。 Still, what you can do is to change the limits nevertheless or you can highlight the small section of your abline so that it becomes visible more easily without changing the limits. 尽管如此,您仍然可以更改限制,也可以突出显示abline的一小部分,以便在不更改限制的情况下更容易看到。

Here's an example: 这是一个例子:

# 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM