0

I am adding a regression line to my plotly correlation graph but when I add a layout to include the COR_text to the graph it zooms out; I know this is because if I were to graph the COR onto the graph itself it would look like a vertical jagged line spanning x10 of the original range, thus making it looked zoomed out. I am curious as to why this happens.

I have looked into similar problems and one elegant solution I came across can be seen here R plotly(): Adding regression line to a correlation scatter plot though the predict function is not producing a list for my data. Below is my code, where I make a data frame with two columns consisting of the values I want to correlate, "Fe" and "SRP":

SRP_Fe <- data.frame(as.numeric(Fe_S$Value), as.numeric(SRP_F$Value))
names(SRP_Fe) <- c("Fe", "SRP")

lm_zero=lm(SRP_Fe$SRP ~ SRP_Fe$Fe, data=SRP_Fe)

COR = cor.test(SRP_Fe$Fe,SRP_Fe$SRP)[c("estimate","p.value")]
COR_text = paste(c("R=","p="),signif(as.numeric(COR,3),3),collapse=" ")

plot_ly(data= SRP_Fe, x= ~Fe, y= ~SRP, type="scatter", mode="markers", name="SRP Fe") %>%
    add_lines(x =~Fe, y =predict(lm_zero))%>% 
  layout(annotations = list(text = COR_text,showarrow =FALSE))

As I mentioned, when I have tried to integrate the operations used in the elegant solution I have found I do the following but since the prediction variable is not a list I cannot continue. Of course, I modified the solution a bit to better fit my data and believe I have looked at it enough to understand what is going on but be missing something. Thank you in advance for any clarification and help.

benson23
  • 8,765
  • 8
  • 15
  • 32
rod22
  • 31
  • 3

0 Answers0