|
1 | 1 | library(ggplot2) |
2 | 2 | library(ggiraph) |
3 | 3 |
|
4 | | -counts <- as.data.frame(table(x = rpois(100,5))) |
5 | | -counts$x <- as.numeric( as.character(counts$x) ) |
6 | | -counts$xlab <- paste0("bar",as.character(counts$x) ) |
7 | | - |
8 | | -gg_segment_1 <- ggplot(data = counts, aes(x = x, y = Freq, |
9 | | - yend = 0, xend = x, tooltip = xlab ) ) + |
10 | | - geom_segment_interactive( size = I(10)) |
| 4 | +counts <- as.data.frame(table(x = rpois(100, 5))) |
| 5 | +counts$x <- as.numeric(as.character(counts$x)) |
| 6 | +counts$xlab <- paste0("bar", as.character(counts$x)) |
| 7 | + |
| 8 | +gg_segment_1 <- ggplot( |
| 9 | + data = counts, |
| 10 | + aes(x = x, y = Freq, yend = 0, xend = x, tooltip = xlab) |
| 11 | +) + |
| 12 | + geom_segment_interactive(size = I(10)) |
11 | 13 | x <- girafe(ggobj = gg_segment_1) |
12 | | -if( interactive() ) print(x) |
13 | | - |
14 | | -dataset = data.frame(x=c(1,2,5,6,8), |
15 | | - y=c(3,6,2,8,7), |
16 | | - vx=c(1,1.5,0.8,0.5,1.3), |
17 | | - vy=c(0.2,1.3,1.7,0.8,1.4), |
18 | | - labs = paste0("Lab", 1:5)) |
19 | | -dataset$clickjs = paste0("alert(\"",dataset$labs, "\")" ) |
| 14 | +if (interactive()) { |
| 15 | + print(x) |
| 16 | +} |
| 17 | + |
| 18 | +dataset = data.frame( |
| 19 | + x = c(1, 2, 5, 6, 8), |
| 20 | + y = c(3, 6, 2, 8, 7), |
| 21 | + vx = c(1, 1.5, 0.8, 0.5, 1.3), |
| 22 | + vy = c(0.2, 1.3, 1.7, 0.8, 1.4), |
| 23 | + labs = paste0("Lab", 1:5) |
| 24 | +) |
| 25 | +dataset$clickjs = paste0("alert(\"", dataset$labs, "\")") |
20 | 26 |
|
21 | 27 | gg_segment_2 = ggplot() + |
22 | | - geom_segment_interactive(data=dataset, mapping=aes(x=x, y=y, |
23 | | - xend=x+vx, yend=y+vy, tooltip = labs, onclick=clickjs ), |
24 | | - arrow=grid::arrow(length = grid::unit(0.03, "npc")), |
25 | | - size=2, color="blue") + |
26 | | - geom_point(data=dataset, mapping=aes(x=x, y=y), |
27 | | - size=4, shape=21, fill="white") |
| 28 | + geom_segment_interactive( |
| 29 | + data = dataset, |
| 30 | + mapping = aes( |
| 31 | + x = x, |
| 32 | + y = y, |
| 33 | + xend = x + vx, |
| 34 | + yend = y + vy, |
| 35 | + tooltip = labs, |
| 36 | + onclick = clickjs |
| 37 | + ), |
| 38 | + arrow = grid::arrow(length = grid::unit(0.03, "npc")), |
| 39 | + size = 2, |
| 40 | + color = "blue" |
| 41 | + ) + |
| 42 | + geom_point( |
| 43 | + data = dataset, |
| 44 | + mapping = aes(x = x, y = y), |
| 45 | + size = 4, |
| 46 | + shape = 21, |
| 47 | + fill = "white" |
| 48 | + ) |
28 | 49 |
|
29 | 50 | x <- girafe(ggobj = gg_segment_2) |
30 | | -if( interactive() ) print(x) |
| 51 | +if (interactive()) { |
| 52 | + print(x) |
| 53 | +} |
31 | 54 |
|
32 | 55 | df <- data.frame(x1 = 2.62, x2 = 3.57, y1 = 21.0, y2 = 15.0) |
33 | 56 | p <- ggplot(df, aes(x = x1, y = y1, xend = x2, yend = y2)) + |
34 | | - geom_curve_interactive(aes(colour = "curve", tooltip=I("curve"))) + |
35 | | - geom_segment_interactive(aes(colour = "segment", tooltip=I("segment"))) |
36 | | - |
37 | | -x <- girafe(ggobj = p) |
38 | | -if( interactive() ) print(x) |
39 | | - |
40 | | - |
41 | | -p <- ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) + |
42 | | - geom_point(color = "grey50", alpha = 0.3, size = 2) + |
43 | | - geom_hpline_interactive(data = iris[1:5,], mapping = aes(tooltip = Species)) + |
44 | | - theme_bw() |
| 57 | + geom_curve_interactive(aes(colour = "curve", tooltip = I("curve"))) + |
| 58 | + geom_segment_interactive(aes(colour = "segment", tooltip = I("segment"))) |
45 | 59 |
|
46 | 60 | x <- girafe(ggobj = p) |
47 | | -if( interactive() ) print(x) |
48 | | - |
49 | | - |
50 | | - |
51 | | - |
| 61 | +if (interactive()) { |
| 62 | + print(x) |
| 63 | +} |
0 commit comments