I created an animated bar plot using ggplot and gganimate. The data labels are numbers and are comma separated using the comma function in the scales library. But while the bars are changing from one year to another year the data labels are also changing and during the transition hex codes are visible instead of numbers. How to resolve this issue?
The code is as follows:
staticplot <- ggplot(data, aes(rank, group = variable_name,
fill = as.factor(variable_name), color = as.factor(variable_name))) +
geom_tile(aes(y = value / 2,
height = value,
width = 0.9), alpha = 0.8, color = NA) +
geom_text(aes(y = 0, label = paste(variable_name, " "), hjust = 1),
vjust = 0.2, size = 6) +
geom_text(aes(y = value / 2, label = Value_lbl , hjust = -1),
color = "black", size = 8) +
coord_flip(clip = "off", expand = FALSE) +
scale_y_continuous(labels = scales::comma) +
scale_x_reverse() +
guides(color = FALSE, fill = FALSE) +
theme(axis.line = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
legend.position = "none",
panel.background = element_blank(),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.title = element_text(size = 30, hjust = 0.5, face = "bold", colour = "black", vjust = 0),
plot.subtitle = element_text(size = 18, hjust = 0.5, face = "italic", color = "black"),
plot.caption = element_text(size = 8, hjust = 0.5, face = "italic", color = "black"),
plot.background = element_blank(),
plot.margin = margin(2, 2, 2, 4, "cm"))
anim <- staticplot + transition_states(year, transition_length = 20, state_length = 30, wrap = TRUE) +
view_follow(fixed_x = FALSE, fixed_y = TRUE) +
labs(title = 'Yearly Comparison : {closest_state}')
A snippet of the data is as follows:
A snippet of the gif is as follows which shows the hexcodes:
5