I am creating a plot using ggplot package in R, however I need a plotly object at the end, therefore I am applying the ggplotly function. Unfortunately, this step changes the y-position of my bar labels. What can I change to prevent this from happening (or fix using plotly later)?
# initiate dataframe
my_data <- data.frame(type = c("friendly", "friendly", "friendly","friendly"),
gender = c("male", "female", "male", "female"),
level = c(1,1,2,2),
percent_change =c(10, 11, 9, 8))
# save the x variable
x_var <- unique(my_data$type)
# create the plot - with bar labels
gg <- ggplot(my_data) +
geom_bar(aes(x = x_var, y = percent_change, fill = factor(level)),
stat = 'identity', position = position_dodge(width = 1)) +
geom_text(aes(x = x_var, y = percent_change, label = percent_change, group = interaction(x_var, level)),
position = position_dodge(width = 1), vjust = -0.5) +
scale_fill_manual(values = c("#154273", "#e17000", "#39870c")) +
facet_wrap(~gender)
# transform to plotly object (messes up the labels!)
final_plot <- ggplotly(gg)
I already tried using correcting using plotly afterwards with textposition which didn’t work out.
Recognized by R Language Collective
New contributor
Afke Politiek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.