plotly_build修改图例和标签 [英] plotly_build modifies legend and labels

查看:784
本文介绍了plotly_build修改图例和标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将ggplot2图转换为图时,我遇到问题.

I have a problem when transforming a ggplot2 graph to 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

# Plotly

gplotly <- plotly_build(gplot)
gplotly

关于ggplot2图:

About the ggplot2 graph:

如何从这些点上完全删除边框(颜色="white",笔划= 0.1)?

How to completely remove the border (color="white", stroke = 0.1) from the points?

关于绘图版本:

为什么修改图例?

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

Why x.axis labels are modified?

为什么修改了geom_lines并现在在图中显示白点?

Why geom_lines are modified and now white points are shown in the graph?

推荐答案

根据要求,我在此处发布我的解决方案.这是我的特殊问题,非常具体,但可能可以帮助某人处理绘图对象的结构:

As requested, I post here my solution. It is very specific of my particular problem, but probably it could help someone to deal with the structure of a plotly object:

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的名称 x轴值的7个标签

The arguments of the function are: 1 ggplotly output 2 names of the diferent series of the plot 3 does the series have lines? 4 does the series have points? 5 name of xaxis var 6 name of yaxis var 7 labels of x-axis values

这篇关于plotly_build修改图例和标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆