简体   繁体   English

R-使用ggplot创建断层图

[英]R - create tomography plot with ggplot

I have the following graph: 我有以下图形:

set.seed(123456)
test1_1 <- round(rnorm(20,mean=40,sd=5),0)/100
test1_2 <- round(rnorm(20,mean=60,sd=5),0)/100
test.data <- as.data.frame(cbind(test1_1,test1_2))
test <- ggplot(test.data, aes(test1_1,test1_2))+
  geom_point()+
  scale_y_continuous(limits = c(0, 1)) +
  scale_x_continuous(limits = c(0, 1)) +   # OP missing `+`
  abline(0.5,0.5)
test

Now I have point, which are created with the following formular: 现在,我有了要用以下公式创建的要点:

line <- function(beta_2, test1_1,test1_2){
beta_1 = (test1_2/(1-test1_1))-(test1_1/(1-test1_1))*beta_2
return(beta_1)}


  output1 <- as.data.frame(matrix(0,20,1))
  beta_2 <- 1
  for (i in 1:nrow(test.data)){
    output1[i,] <- line(beta_2,test.data[i,1],test.data[i,2])
  }

  output2 <- as.data.frame(matrix(0,20,1))
  beta_2 <- 0
  for (i in 1:nrow(ei.data)){
    output2[i,] <- line(beta_2,test.data[i,1],test.data[i,2])
  }

  output <- cbind(output1,output2)

I would like the add the data in the second data frame as lines in the plot created above (always connect the points per one row). 我想将第二个数据框中的数据添加为上面创建的图中的线(始终连接每一行的点)。 However, using 但是,使用

abline(output[1,1],output[1,2])

does not work. 不起作用。 How could I achieve this? 我怎样才能做到这一点?

abline() is syntax for base R, for ggplot2 you need to use geom_abline(output[1,1],output[1,2]) abline()是基数R的语法,对于ggplot2,您需要使用geom_abline(output [1,1],output [1,2])

ggplot(test.data, aes(test1_1,test1_2)) + 
geom_point()+
scale_y_continuous(limits = c(0, 1))+
scale_x_continuous(limits = c(0, 1)) + 
geom_abline(slope = output[1,1],intercept = output[1,2])

Note: Your reproducible had an error in which ei.data was used instead of test.data, and part of your ggplot call was missing a + sign, I only point this out so others (or you) do not get caught in a separate error without realizing it. 注意:您的可复制对象有一个错误,其中使用ei.data而不是test.data,并且您的ggplot调用的一部分缺少+号,我只是指出这一点,这样其他人(或您)就不会陷入另一个错误而没有意识到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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