Home > OS >  Show unique values on bubbles graph in R
Show unique values on bubbles graph in R

Time:04-08

I'm working with a db which looks more or less like this:

dput(ex)

structure(list(clave = c("01", "02", "03", "04", "05", "06", 
"07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", 
"18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", 
"29", "30", "31", "32", "33"), n = c(2127, 3519, 153, 2070, 3089, 
2971, 3005, 152, 53409, 2351, 4599, 3121, 4828, 7588, 25714, 
4218, 3032, 295, 3856, 3885, 7044, 3246, 2589, 2559, 2223, 2316, 
3560, 2695, 2465, 6742, 4024, 2065, 1627)), row.names = c(NA, 
-33L), class = c("tbl_df", "tbl", "data.frame"))

And I'm using the packcircles package to create a bubble graph for just one variable since standard bubbles are added as a third dimension on ggplot. I'm using paletteer library too:

library(packcircles)
library(paletteer)

Next code creates the data.frame that will be used on the graph. First I create the coordinates for the bubbles (circles) and then I incorporate my clave and n variables from original data.frame:

# Create circles
ex_ <- circleProgressiveLayout(ex$n)
ex_ <- circleLayoutVertices(ex_, npoints=50)

# Incorporate variables
ex_$clave <- rep(ex$clave, each=51)
ex_$n <- rep(ex$n, each=51)

# Palette
colors <- paletteer_c("ggthemes::Green-Gold", 33)

Now we're ready to graph:

ggplot(data = ex_, aes(x, y, fill=clave))  
    geom_polygon()  
    coord_fixed(ratio = 4/5)  
    theme_void()  
    scale_fill_manual(values = rev(colors))  
    geom_text(size = 3, label= unique(ex_$n))-> my_graph

plotly::ggplotly(my_graph)

Code above throws following error:

Error in `check_aesthetics()`:
! Aesthetics must be either length 1 or the same as the data (1683): label
Run `rlang::last_error()` to see where the error occurred.

If I use instead:

ggplot(data = ex_, aes(x, y, fill=clave))  
    geom_polygon()  
    coord_fixed(ratio = 4/5)  
    theme_void()  
    scale_fill_manual(values = rev(colors))  
    geom_text(size = 3, label= ex_$n)-> my_graph

plotly::ggplotly(my_graph)

Now every circle is surrounded by text (51 times same text). What I want is that only clave and one value of n were showed when you pass the mouse pointer through each circle.

Any advice or idea to handle with this will be much appreciated.

CodePudding user response:

How about this. The mouse-over doesn't work in the static picture below, but if you run the code, it should.

  ex <- structure(list(clave = c("01", "02", "03", "04", "05", "06", 
                               "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", 
                               "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", 
                               "29", "30", "31", "32", "33"), n = c(2127, 3519, 153, 2070, 3089, 
                                                                    2971, 3005, 152, 53409, 2351, 4599, 3121, 4828, 7588, 25714, 
                                                                    4218, 3032, 295, 3856, 3885, 7044, 3246, 2589, 2559, 2223, 2316, 
                                                                    3560, 2695, 2465, 6742, 4024, 2065, 1627)), row.names = c(NA, 
                                                                                                                              -33L), class = c("tbl_df", "tbl", "data.frame"))

library(tidyverse)
library(packcircles)
library(paletteer)
ex_ <- circleProgressiveLayout(ex$n)
ex_ <- circleLayoutVertices(ex_, npoints=50)

# Incorporate variables
ex_$clave <- rep(ex$clave, each=51)
ex_$n <- rep(ex$n, each=51)

# Palette
colors <- paletteer_c("ggthemes::Green-Gold", 33)

ex_ <- ex_ %>% 
  mutate(lab = paste0("clave: ", clave, "\nN: ", n))

ggplot(data = ex_, aes(x, y, fill=clave, text=lab))  
  geom_polygon()  
  coord_fixed(ratio = 4/5)  
  theme_void()  
  scale_fill_manual(values = rev(colors))-> my_graph

plotly::ggplotly(my_graph, tooltip = "text")

Created on 2022-04-06 by the reprex package (v2.0.1)

  • Related