简体   繁体   中英

I am trying to use ggiraph to add interactivity to a ggplot2 graph, it's not working

I have found this code that does exactly what I want but apparently it is using a fake dataset created with charlatan. What it does is make each line of a datatable into a selectable graph line. It enables tooltips for each line and fades the other lines while one is selected.

library(ggplot2)
library(ggiraph)
library(data.table)
library(charlatan)

species <- ch_taxonomic_species(n = 10)
dat <- lapply(species, function(species, n){
  data.table(
    date = as.Date(seq_len(n), origin = "2018-10-25"),
    sales = cumsum(runif(n, -1, 1)),
    species = species,
    name = ch_name(n = n)
  )
}, n = 200)
dat <- rbindlist(dat)

gg <- ggplot(dat, aes(x = date, y = sales, 
                      colour = species, group = species)) +
  geom_line_interactive(aes(tooltip = name, data_id = species)) +
  scale_color_viridis_d() + 
  labs(title = "move mouse over lines")

x <- girafe(ggobj = gg, width_svg = 8, height_svg = 6,
  options = list(
    opts_hover_inv(css = "opacity:0.1;"),
    opts_hover(css = "stroke-width:2;")
  ))
x

I already have my dataset, called g from which I have been able to make a ggplot graph. My code looks like this:

g <- ggplot() +
   geom_line(data = dat22, aes(x=YEAR, y=RO_MM, group = HUC8, color= HUC8))+
   geom_line(data = dat21, aes(x=YEAR, y=RO_MM, group = HUC8, color= HUC8))+
   geom_line(data = dat20, aes(x=YEAR, y=RO_MM, group = HUC8, color= HUC8))+ 
   geom_line(data = dat19, aes(x=YEAR, y=RO_MM, group = HUC8, color= HUC8))+
   geom_line(data = dat18, aes(x=YEAR, y=RO_MM, group = HUC8, color= HUC8))+
   geom_line(data = dat17, aes(x=YEAR, y=RO_MM, group = HUC8, color= HUC8))+
   geom_line(data = dat16, aes(x=YEAR, y=RO_MM, group = HUC8, color= HUC8))+
   geom_line(data = dat15, aes(x=YEAR, y=RO_MM, group = HUC8, color= HUC8))+ 
   geom_line(data = dat14, aes(x=YEAR, y=RO_MM, group = HUC8, color= HUC8))+
   geom_line(data = dat13, aes(x=YEAR, y=RO_MM, group = HUC8, color= HUC8))+
   geom_line(data = dat12, aes(x=YEAR, y=RO_MM, group = HUC8, color= HUC8))+
   geom_line(data = dat11, aes(x=YEAR, y=RO_MM, group = HUC8, color= HUC8))+
scale_color_manual(values = c("gray","gray","gray","gray","gray","gray","gray","gray","gray","gray","gray","black"))

Each dat table, from which I created each line in the graph, looks similar to this (but larger):

dat1 <- read.table(text = "HUC8 YEAR    RO_MM
bcc1_45Fall_1020004 1961    112
bcc1_45Fall_1020004 1962    243.7
bcc1_45Fall_1020004 1963    233.3
bcc1_45Fall_1020004 1964    190.3
bcc1_45Fall_1020004 1965    200.1
bcc1_45Fall_1020004 1966    220.8
bcc1_45Fall_1020004 1967    130.4
bcc1_45Fall_1020004 1968    90
bcc1_45Fall_1020004 1969    211.2
bcc1_45Fall_1020004 1970    157.6
bcc1_45Fall_1020004 1971    221
bcc1_45Fall_1020004 1972    223.5
bcc1_45Fall_1020004 1973    166.7
bcc1_45Fall_1020004 1974    60
bcc1_45Fall_1020004 1975    144.8
bcc1_45Fall_1020004 1976    150.6
bcc1_45Fall_1020004 1977    129.9
bcc1_45Fall_1020004 1978    223.4
bcc1_45Fall_1020004 1979    235.3
bcc1_45Fall_1020004 1980    143.4
bcc1_45Fall_1020004 1981    55.4
bcc1_45Fall_1020004 1982    214.8
bcc1_45Fall_1020004 1983    217.1
bcc1_45Fall_1020004 1984    126.5
bcc1_45Fall_1020004 1985    106.7",
    header = TRUE)

I created the multiline ggplot because I could not find a way to make separate graph lines out of each chunk of data in the dataset after using rbind , so I did them individually. I am able to create this graph: 在此处输入图像描述

But I would like it to be interactive. Can anyone tell me how to work my code into this ggiraph code? I want to be able to highlight/select each line to visually compare it to the black line.

I have tried adding this to the end of my code:

 geom_line_interactive(aes(tooltip = HUC8, data_id = HUC8)) +
   labs(title = "HUC8-01020004")
g

It says it "can't find HUC8". I have tried this, with the same result:

my_giraph <- g + geom_line_interactive(
   aes(data_id = HUC8), size = 1)
   girafe(code = print(my_giraph))

And a number of other things, but I'm not having any luck.

Looking for @David Gohel? (who wrote the ggiraph code) Anyone else know?

Solution to get this:

在此处输入图像描述

Step 1 Example datasets

dat1 <- read.table(text = "HUC8 YEAR    RO_MM
bcc1_45Fall_1020004 1961    112
bcc1_45Fall_1020004 1962    243.7
bcc1_45Fall_1020004 1963    233.3
bcc1_45Fall_1020004 1964    190.3
bcc1_45Fall_1020004 1965    200.1
bcc1_45Fall_1020004 1966    220.8
bcc1_45Fall_1020004 1967    130.4
bcc1_45Fall_1020004 1968    90
bcc1_45Fall_1020004 1969    211.2
bcc1_45Fall_1020004 1970    157.6
bcc1_45Fall_1020004 1971    221
bcc1_45Fall_1020004 1972    223.5
bcc1_45Fall_1020004 1973    166.7
bcc1_45Fall_1020004 1974    60
bcc1_45Fall_1020004 1975    144.8
bcc1_45Fall_1020004 1976    150.6
bcc1_45Fall_1020004 1977    129.9
bcc1_45Fall_1020004 1978    223.4
bcc1_45Fall_1020004 1979    235.3
bcc1_45Fall_1020004 1980    143.4
bcc1_45Fall_1020004 1981    55.4
bcc1_45Fall_1020004 1982    214.8
bcc1_45Fall_1020004 1983    217.1
bcc1_45Fall_1020004 1984    126.5
bcc1_45Fall_1020004 1985    106.7",
    header = TRUE)

#Two additional datasets to mirror problem

dat2 <- dat1
dat2$RO_MM <- dat2$RO_MM+100
dat2$HUC8 <- paste0(dat2$HUC8,"_02")

dat3 <- dat1
dat3$RO_MM <- dat3$RO_MM-100
dat3$HUC8 <- paste0(dat3$HUC8,"_03")

Step 2) - Combining data into one (using dplyr bind_rows() )

library(dplyr)
df <- bind_rows(dat1,dat2,dat3)

Step 3) Graph

In addition to adding the interactive geom_line_interactive() , the aes() needs to have a data_id mapping. Since in each of your datasets there is a HUC8 variable with the same values per dataset I use that. (I also add the tooltip with that):

g <- ggplot(data = df) +
   geom_line_interactive(aes(x=YEAR, y=RO_MM, group = HUC8, color= HUC8, 
                             data_id = HUC8,
                             tooltip = HUC8)) +
   scale_color_manual(values = c("gray","gray", "black"))

g

This gives:

在此处输入图像描述

Step 4) Interactive graph via girafe()

Note the added colour in the css: stroke:red;

x <- girafe(ggobj = g, width_svg = 8, height_svg = 6,
  options = list(
    opts_hover(css = "stroke:red;stroke-width:2;")
  ))
x

This gives the interactive graph displayed at the beginning.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM