Reputation: 2077
I have a ggplot2 chart with the following code
[![enter image description here][1]][1]a <-filter(match,team==theTeam)
# Group batsman with non strikers and compute partnerships
df <- data.frame(summarise(group_by(a,batsman,nonStriker),sum(runs)))
names(df) <- c("batsman","nonStriker","runs")
if(plot==TRUE){
plot.title <- paste(theTeam,"Batting partnership in match (vs.",opposition,")")
ggplot(data=df,aes(x=batsman,y=runs,fill=nonStriker))+
geom_bar(data=df,stat="identity") +
xlab("Batmen") + ylab("Runs Scored") +
ggtitle(bquote(atop(.(plot.title),
atop(italic("Data source:http://cricsheet.org/"),"")))) +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
which produces the chart as below.
But when I try to create an interactive chart with
g<-ggplot(data=df,aes(x=batsman,y=runs,fill=nonStriker))+
geom_bar(data=df,stat="identity") +
xlab("Batmen") + ylab("Runs Scored") +
ggtitle(bquote(atop(.(plot.title),
atop(italic("Data source:http://cricsheet.org/"),"")))) +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
ggplotly(g)
I get the following error
Error in unique.default(x) : unique() applies only to vectors
In addition: Warning message:
In if (robust_nchar(plot$labels$title) > 0) { :
the condition has length > 1 and only the first element will be used
Is there anything specific that needs to be done before converting ggplot -> ggplotly?
Thanks
batsman nonStriker runs JA Morkel Joginder Sharma 21 JA Morkel MS Dhoni 3 JA Morkel MS Gony 5 Joginder Sharma JA Morkel 16
MS Dhoni JA Morkel 1
MS Dhoni SK Raina 22
MS Gony JA Morkel 15
PA Patel SP Fleming 4
S Anirudha SP Fleming 1
S Badrinath SK Raina 0
SK Raina MS Dhoni 16
SK Raina S Badrinath 9
SK Raina SP Fleming 7
SP Fleming S Anirudha 5
SP Fleming SK Raina 9
Upvotes: 0
Views: 767
Reputation: 389275
This has been a long pending open issue in plotly
where the subtitles are lost from ggplot -> plotly. The fix for now is to add title and subtitle in plotly.
library(ggplot2)
library(plotly)
ggplot(data=df,aes(x=batsman,y=runs,fill=nonStriker))+
geom_bar(data=df,stat="identity") +
xlab("Batmen") + ylab("Runs Scored") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) -> g
ggplotly(g) %>%
layout(title = list(text = paste0(plot.title, '\n<sup>Data source:http://cricsheet.org/</sup>')))
Upvotes: 1