Black Lives Matter. Please consider donating to Black Girls Code today.
Learn how to use COVID-19 data in open source Dash apps. Register for the Sept 23rd webinar with IQT!

R Plotly bar chat - How can i show factor terms in the legend?

Hi All

I have a dataframe in R like so which shows dates and word counts of sentences plus the topic type (as computed using LDA)

             date rowTotals wordcount topic

1 2016-03-11 13:18:38 2 3 7
2 2016-03-11 13:18:54 3 4 7
3 2016-03-11 13:34:58 4 8 2
4 2016-03-11 13:35:27 3 6 1
5 2016-03-11 13:37:22 5 6 8
6 2016-03-11 13:37:58 6 8 1

The topic terms are saved in a separate matrix called terms:

term
Topic 1 Topic 2 Topic 3 Topic 4 Topic 5 Topic 6 Topic 7 Topic 8
[1,] “still” “used” “look” “ok” “see” “time” “seconds” “yes”
[2,] “ipad” “android” “problem” “get” “android” “will” “team” “server”
[3,] “conversation” “hexagon” “like” “ios” “ios” “well” “room” “new”
[4,] “worked” “ios” “android” “screen” “message” “let” “see” “good”
[5,] “connection” “local” “server” “take” “name” “fresh” “list” “id”
[6,] “lost” “chat” “gap” “hexagon” “user” “update” “hi” “latest”

I can plot a barchart in ggplot like so

However when i try and create the same plot in ploly the topic terms don’t appear in the legend:

As you can see the topic number are listed but the corresponding terms are not. I suspect the issue is down to how ggplot uses the fill= parameter with term[topic]

I’ve included the sample code for each plot below:

GGplot code:

topic_bar <- ggplot(aes(x=date, y=wordcount, group = topic, fill = term[topic]), data = topics.final.df.2) +
geom_bar(stat = ‘identity’, position = ‘dodge’, width = 25) +
xlab(“Time”) + ylab(“Instant Message - Line Word Count”) +
ggtitle(“IM’s by topic type (Per Message)”)

Plotly code

Plot bar chart of time (plotly)

p <- plot_ly(
x = topics.final.df.2$date,
y = topics.final.df.2$wordcount,
color = as.factor(topics.final.df.2$topic),
showlegend = TRUE,
legendgroup = c(“term[1:6]”),
type = “bar”) %>%
layout(title = “Instant Message - Topic type over time”,
xaxis = list(title = “Date / Time”),
yaxis = list(title = “Word Count”)
)

Thanks
J

PS i tried to use ggplotly but the char does not render as expected.