I have a problem when transforming a ggplot2 graph to plotly.
Here it is a reproducible code:
library(ggplot2)
library(plotly)
# Build the data
dgraf<-data.frame(num=seq(0,2*pi,length.out=100),
name=sample(apply(expand.grid(letters,letters),1,function(x) paste(x,collapse="\n")),100),stringsAsFactors = F)
dgraf$y<-sin(dgraf$num)
rpoint<-sample(25:75,1)
dgraf$ypoint<-NA
dgraf$ypoint[rpoint]<-dgraf$y[rpoint]
dgraf$ymin<-NA
dgraf$ymin[1:rpoint]<-runif(1,0.25,0.75)
dgraf$ymax<-NA
dgraf$ymax[rpoint:100]<-runif(1,-0.75,-0.25)
# ggplot
labels<-c("Data y","Breaking point","Lower line","Higher line")
shapes<-c(21,21,NA,NA)
colors<-c("grey","white","cyan","green")
fills<-c("black","red","black","black")
sizes<-c(3,4,1,1)
linetypes<-c("solid","blank", "solid", "dashed")
dgrafgg<-reshape2::melt(dgraf,id.var="num", measure.var=c("y", "ypoint", "ymin", "ymax"))
gplot<-ggplot(dgrafgg) +
geom_line(aes(x=num,y=value,group=variable, color=variable, linetype=variable),size=1.2) +
geom_point(aes(x=num,y=value,group=variable, color=variable, size=variable, fill=variable, shape=variable), color="white", stroke = 0.1) +
scale_shape_manual(values=shapes, name="Legend", labels=labels) +
scale_color_manual(values=colors, name="Legend", labels=labels) +
scale_fill_manual(values=fills, name="Legend", labels=labels) +
scale_size_manual(values=sizes, name="Legend", labels=labels) +
scale_linetype_manual(values=linetypes, name="Legend", labels=labels) +
scale_x_continuous(breaks = dgraf$num[seq(1,100,by=10)], labels = dgraf$name[seq(1,100,by=10)]) +
labs(title = "Main title", x = "x", y = "y") +
ggthemes::theme_few()
gplot
# Plotly
gplotly <- plotly_build(gplot)
gplotly
About the ggplot2 graph:
How to completely remove the border (color="white", stroke = 0.1) from the points?
About the plotly version:
Why legend is modified?
Why x.axis labels are modified?
Why geom_lines are modified and now white points are shown in the graph?