sent_plot <- plot_ly(
data = sentiment_summary,
x = ~airline,
y = ~Percentage,
color = ~airline_sentiment,
colors = c(
"negative" = "#CC79A7",
"neutral" = "#999999",
"positive" = "#0072B2"
),
type = "bar",
text = ~label,
textposition = "outside",
cliponaxis = FALSE,
hovertext = ~paste(
"<b>", airline, "</b>",
"<br>Sentiment: ", sentiment_label,
"<br>Percentage: ", sprintf("%.1f%%", Percentage),
"<br>Tweets: ", Count,
"<br><br><b>Example Tweet</b><br>",
short_example
),
hoverinfo = "text"
) %>%
layout(
title = list(
text = paste0(
"<b>Negative tweets outnumbered positive tweets for every airline</b>",
"<br><span style='font-size:14px;'>",
"Tweet sentiment by airline (16–24 February 2015)",
"</span>"
),
x = 0.5,
xanchor = "center",
font = list(size = 22)
),
barmode = "group",
bargap = 0.25,
paper_bgcolor = "white",
plot_bgcolor = "white",
margin = list(
t = 120,
b = 160,
l = 70,
r = 30
),
xaxis = list(
title = "",
tickangle = -20,
showgrid = FALSE,
zeroline = FALSE,
categoryorder = "array",
categoryarray = airline_order
),
yaxis = list(
title = "",
range = c(0, 105),
ticksuffix = "%",
dtick = 20,
showgrid = TRUE,
gridcolor = "#EFEFEF",
gridwidth = 1,
zeroline = FALSE
),
legend = list(
title = list(text = ""),
orientation = "h",
x = 0.5,
xanchor = "center",
y = -0.28,
yanchor = "top"
),
annotations = list(
list(
x = 0,
y = -0.57,
xref = "paper",
yref = "paper",
showarrow = FALSE,
xanchor = "left",
text = "Source: Twitter US Airline Sentiment dataset (Kaggle)",
font = list(
size = 11,
color = "gray50"
)
)
)
)
sent_plot