如何在 ggplotly R 中保留来自 ggplot 的原始趋势线并动态添加网格线
How to retain original trendline from ggplot in ggplotly R and add gridlines dynamically
我在 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)
我正在尝试使用 ggplot 和 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"))
我无法在 B 中保留 plott1 中生成的趋势线(y 趋势线弯曲)并且网格线消失。我请求有人指导我,因为我无法解决这个问题。非常感谢
你的线性趋势线变弯了,因为你在将 ggplot 转换为 plotly 对象时切换到对数刻度。因此,为了解决您的问题,我建议在 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)
我在 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)
我正在尝试使用 ggplot 和 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"))
我无法在 B 中保留 plott1 中生成的趋势线(y 趋势线弯曲)并且网格线消失。我请求有人指导我,因为我无法解决这个问题。非常感谢
你的线性趋势线变弯了,因为你在将 ggplot 转换为 plotly 对象时切换到对数刻度。因此,为了解决您的问题,我建议在 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)