I am working with the R programming language. I found this really good tutorial that shows how to make Bubble Plots using the ggplot library: https://www.data-to-viz.com/graph/bubble.html :
# Libraries
library(tidyverse)
library(hrbrthemes)
library(viridis)
library(gridExtra)
library(ggrepel)
library(plotly)
# The dataset is provided in the gapminder library
library(gapminder)
data <- gapminder %>% filter(year=="2007") %>% dplyr::select(-year)
# Prepare data
tmp <- data %>%
mutate(
annotation = case_when(
gdpPercap > 5000 & lifeExp < 60 ~ "yes",
lifeExp < 30 ~ "yes",
gdpPercap > 40000 ~ "yes"
)
) %>%
mutate(pop=pop/1000000) %>%
arrange(desc(pop)) %>%
mutate(country = factor(country, country))
# Plot
my_plot = ggplot( tmp, aes(x=gdpPercap, y=lifeExp, size = pop, color = continent)) +
geom_point(alpha=0.7) +
scale_size(range = c(1.4, 19), name="Population (M)") +
scale_color_viridis(discrete=TRUE) +
theme_ipsum() +
theme(legend.position="none") +
geom_text_repel(data=tmp %>% filter(annotation=="yes"), aes(label=country), size=4 )
I would like to make this into an interactive plotly plot, but keep some the labels/captions. I tried to do this with the "ggplotly" function:
ggplotly(my_plot)
But as we can see here, the labels (eg South Africa, USA) have disappeared? Is there a way to restore these labels and keep the plot as an interactive plot?
Thanks!
See recent thread with similar issue.
As per warning message:
geom_GeomTextRepel() has yet to be implemented in plotly
To work around that you can use geom_text
instead and play with the y/x values:
my_plot = ggplot( tmp, aes(x=gdpPercap, y=lifeExp, size = pop, color = continent)) +
geom_point(alpha=0.7) +
scale_size(range = c(1.4, 19), name="Population (M)") +
scale_color_viridis(discrete=TRUE) +
theme_ipsum() +
theme(legend.position="none") +
geom_text(data=tmp %>% filter(annotation=="yes"), aes(y=lifeExp-1.5, label=country), size=4 )
ggplotly(my_plot)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.