I Have the following dataframe in R
A<-data.frame("Col1"= c(21.5 ,22.5 ,15.5, 20.5 ,17.5 ,14.5 ,23.5, 11.5, 16.5, 25.5 ,18.5, 24.5 ,10.5 , 9.5, 19.5, 26.5, 13.5, 12.5 ,27.5, 4.5 , 5.5, 8.5, 6.5, 7.5))
A$Col2=c(0.619219548, 0.723265668,0.122833055, 0.536849680, 0.257225692 ,0.081648474, 0.794797325 ,0.023125359, 0.194364553, 0.909681117, 0.343930779, 0.857658382, 0.018791029 ,0.014457257, 0.467485576 ,0.950865217, 0.062140165, 0.040464671, 0.989875246, 0.001502443,0.003637989 ,0.012290763, 0.005796326, 0.007959621)
I am trying to create a chart in with logscales using ggplot and plotly
library(ggplot)
library(plotly)
plott1<- ggplot(A, aes(x=Col1, y=Col2)) + geom_point()+ geom_smooth(method = "lm") +scale_x_continuous(minor_breaks = seq(1,max(A$Col1) , 1))+scale_y_continuous(minor_breaks = seq(0,1,0.1))+theme( panel.grid.major = element_line(colour = "green", size = 0.5), panel.grid.minor= element_line(colour = "green", size = 0.2))
B<-ggplotly(plott1, dynamicTicks = TRUE, originalData = T )%>%layout(B, yaxis = list(type = "log"),xaxis = list(type = "log"))
I am unable to retain the trendline generated in plott1 in B(y The trendline gets curved) and the gridlines vanish. I request someone to guide me as I am unable to solve this issue. Many thanks in advance
Your linear trend line gets curved because you switch to log scales when converting the ggplot to a plotly object. Hence, to solve your issue I would suggest to do the log transformation in ggplot:
library(plotly)
ggplot(A, aes(x=Col1, y=Col2)) +
geom_point()+
geom_smooth(method = "lm") +
scale_x_log10(minor_breaks = seq(1,max(A$Col1) , 1))+
scale_y_log10(minor_breaks = seq(0,1,0.1))+
theme( panel.grid.major = element_line(colour = "green", size = 0.5), panel.grid.minor= element_line(colour = "green", size = 0.2))
ggplotly(originalData = T)