0

enter image description hereI 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

Test data

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
2
  • Added test data. Commented May 16, 2021 at 6:13
  • This seems to work. Need to get sub-heading also. g= ggplot(data=df,aes(x=batsman,y=runs,fill=nonStriker))+ geom_bar(data=df,stat="identity") + xlab("Batmen") + ylab("Runs Scored") + ggtitle(plot.title) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) ggplotly(g) Commented May 16, 2021 at 7:01

1 Answer 1

1

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>')))
Sign up to request clarification or add additional context in comments.

1 Comment

Yes. Got it work based on datascott.com/blog/subtitles-with-ggplotly which that you need to add HTML tags to get subtitle

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.