R中ggplotly中图例的奇怪格式 [英] Strange formatting of legend in ggplotly in R

查看:91
本文介绍了R中ggplotly中图例的奇怪格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将ggplot变成一个密谋. ggplot可以很好地呈现,但是当我将其放置在ggplotly中时,图例突然在标签后面加上了括号和,1".

I'm trying to turn a ggplot into a plotly. The ggplot renders fine, but when I put it through ggplotly, suddenly the legend adds parenthesis and ",1" after the label.

这是样本伪造数据:

sorted1<-data.frame(CommDate=c(as.Date("2017-09-12"), as.Date("2017-10-15")), CommName=c("Foo", "Bar"), PubB4=c(2,3))

这是我要在其上运行的代码:

And here's the code I'm trying to run on it:

ggplotly(ggplot(sorted1, aes(x=as.Date(CommDate), y=PubB4))+
           geom_smooth(level=0.0, aes(colour="Moving average"), se=FALSE)+
           geom_point(aes(fill=CommName), size=4)+
           expand_limits(y=c(0,4.5))+
           geom_line(mapping=aes(y=4),colour="orangered3",size=1)+
           geom_text(mapping=aes(y=4.2, x=min(sorted1$CommDate)+4), label="Target", size=3)+
           xlab("Committee Date")+
           guides(fill=guide_legend(title="Committee Names"), colour=guide_legend(title.theme=element_blank(),title=NULL))+
           scale_x_date(labels = date_format("%b-%y"))+
           theme_light()+
           theme(plot.title=element_text(hjust=0.5, size=12),panel.grid.major.x = (element_blank()), 
                 panel.grid.minor.x = (element_blank()), 
                 axis.title = element_text(size=8), legend.title = element_text(size=10),
                 legend.text = element_text(size=8), legend.box = 'vertical', legend.spacing.y = unit(-2,"mm"))+
           scale_colour_manual(name="",values="#0072B2"))

(geom_smooth不在此处渲染,但可以处理全部数据.)

(the geom_smooth doesn't render here, but it does with the full data.)

这就是我从中得到的:

为什么图例显示为(foo,1)"?

Why does the legend show up as "(foo,1)"?

我尝试删除实际上解决了问题的geom_smooth,但是我在那儿需要它-如何保留它但要修复图例?

I tried removing the geom_smooth which actually solved the problem, but I need it there - how can I keep it but fix the legend?

谢谢!

更新:好的,我开始注释掉东西以查看会发生什么.如果我从geom_smooth中删除aes(),只要我也保持对scale_colour_manual的注释,这也可以解决问题.但是我真的很想控制geom_smooth的美学,并将其包含在传说中.因此,我正在取得进步,但仍然不尽人意...

Update: OK, I started commenting out stuff to see what happens. If I remove the aes() from the geom_smooth, that also fixes the problem, as long as I keep the scale_colour_manual commented off as well. But I really would like to have control over the geom_smooth's aesthetics, and include it in the legend. So I'm making progress, but still not quite there...

推荐答案

由于我什么都没做,因此开始潜水并自学了如何直接在plotly中创建图表.为了使将来的观看者受益,下面是在plotly中重新创建图表的方式(除了一些美化之处,我将再次讨论):

Since nothing I did worked, I took the dive and taught myself how to create the chart directly in plotly. For the benefit of possible future viewers, here's how the chart is recreated in plotly (sans some beautification that I'll get around to another time):

plot_ly(sorted, x=~CommDate) %>%
  add_markers(y=~PubB4, color=~factor(CommName), size=I(15)) %>% 
  add_lines(x=loess.smooth(sorted$CommDate,sorted$PubB4)$x,         
    y=loess.smooth(sorted$CommDate,sorted$PubB4)$y, name = "Rolling Average", 
    showlegend = TRUE, size=I(3)) %>%
  add_lines(x=c(min(sorted$CommDate),max(sorted$CommDate)),y=4, 
    color=I("red"), name="target", size=I(3)) %>%
  layout(yaxis=(list(range=c(0,max(c(4.5,sorted$PubB4))))),xaxis=list(range=c(min(sorted$CommDate)-10, max(sorted$CommDate)+5))) 

这篇关于R中ggplotly中图例的奇怪格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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