plotly_build修改图例和标签

时间:2017-04-07 12:21:38

标签: r plot ggplot2 plotly

将ggplot2图转换为plotly时出现问题。

这是一个可重现的代码:

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

ggplot2 graph

# Plotly

gplotly <- plotly_build(gplot)
gplotly

plotly graph

关于ggplot2图表:

如何从点上完全删除边框(颜色=&#34;白色&#34;,笔画= 0.1)?

关于情节版:

为什么传奇被修改?

为什么要修改x.axis标签?

为什么修改geom_lines,现在图中会显示白点?

2 个答案:

答案 0 :(得分:1)

要在plotly_bluid(gplot)后更改图例标签,您可以使用ggplo$x$data[[i]]$nameggplo$x$data[[i]]$legendgroup更改gplot信息。

答案 1 :(得分:0)

根据要求,我在这里发布我的解决方案。这是我特定问题的具体内容,但它可能有助于某人处理一个阴谋对象的结构:

fixplotly<-function(i.plotly,i.labels,i.lines,i.points,i.xname,i.yname,i.weeklabels){

  nlabels<-length(i.labels)
  nlists<-length(i.plotly$x$data)
  if (nlists!=2*nlabels) return(i.plotly)
  # Show all labels
  for (i in 1:nlists) i.plotly$x$data[[i]]$showlegend<-T
  # Fix x.axis labels
  a<-strsplit(as.character(i.plotly$x$layout$xaxis$ticktext),"\\\n")
  a.len <- max(sapply(a, length))
  a.corrected <- lapply(a, function(x) {c(x, rep("", a.len - length(x)))})
  divideit<-matrix(unlist(a.corrected),nrow=length(i.plotly$x$layout$xaxis$ticktext), byrow=T)
  i.plotly$x$layout$margin$b<-(NCOL(divideit))*i.plotly$x$layout$margin$b
  i.plotly$x$layout$xaxis$ticktext<-apply(divideit,1,paste,collapse="<br />")
  # Fix labels names
  sequ<-1:nlists-nlabels*(floor((1:nlists-1)/nlabels))
  for (i in 1:nlists) i.plotly$x$data[[i]]$name<-i.labels[sequ[i]]
  # Fix text to showup
  for (i in 1:nlists){
    if (length(grep(i.yname,i.plotly$x$data[[i]]$text))>0){
      #i.plotly$x$data[[i]]$text
      dividetext<-matrix(unlist(strsplit(i.plotly$x$data[[i]]$text,"<br>|<br />")),nrow=length(i.plotly$x$data[[i]]$text), byrow=T)
      i.plotly$x$data[[i]]$text<-paste("Week: ",i.weeklabels,"<br />",sub(i.yname,i.labels[sequ[i]],dividetext[,2]),sep="")
    }
  }
  # For those with points and labels, i modify the mode and add the marker
  pandl<-i.points & i.lines
  index.pandl<-(1:nlabels)[pandl]
  if (length(index.pandl)>0){
    for (i in 1:length(index.pandl)){
      i.plotly$x$data[[index.pandl[i]]]$mode<-"lines+markers"
      i.plotly$x$data[[index.pandl[i]]]$marker<-i.plotly$x$data[[index.pandl[i]+nlabels]]$marker
    }
  }
  # Remove unnecesary legend entries
  panol<-i.points & !i.lines
  index.panol<-(1:nlabels)[panol]
  nopal<-!i.points & i.lines
  index.nopal<-(1:nlabels)[nopal]
  toremove<-c(index.pandl+nlabels,index.panol,index.nopal+nlabels)
  toremove<-toremove[order(toremove, decreasing = T)]
  # in reverse order, since removing changes order
  for (i in 1:length(toremove)) i.plotly$x$data[[toremove[i]]]<-NULL
  return(i.plotly)
}

gplotly <- ggplotly(gplot)
zfix<-fixplotly(gplotly,labels,c(T,F,T,T),c(T,T,F,F),"num","value",dgraf$name)
zfix

该函数的参数是: 1 ggplotly输出 2个不同系列图的名称 3系列有线吗? 4系列有分吗? 5 xaxis var的名字 6 yaxis var的名字 7个x轴值标签