-1

I would like to have a dynamic "point tunneling" i.e. the darker gray area drawing around the neighborhood of the points. I think the great answer of the thread ggplot legends - change labels, order and title is based on experimental values defined in dtt of the thread. Code which makes a rectangular darker gray area around the points in Fig. 1

molten <- structure(list(Vars = structure(c(1L, 2L, 1L, 2L, 1L, 2L), class = "factor", .Label = c("V1", "V2")), variable = structure(c(1L, 1L, 2L, 2L, 3L, 3L), class = "factor", .Label = c("REM", "Kevyt", "Syva")), value = c(160, 150, 380, 420, 110, 180)), .Names = c("Vars", "variable", "value"), row.names = c(NA, -6L), class = c("data.table", "data.frame"))

library(ggplot2)

# https://stackoverflow.com/a/12075912/54964
ggplot(molten, aes(x = Vars, y = value, group = variable, colour = variable, ymin = 100, ymax = 450)) +
    geom_ribbon(alpha=0.2, colour=NA)+ 
    geom_line() +       
    geom_point()  +      
    facet_wrap(~variable) 

Fig. 1 Output with discrete rectangular point tunneling, Fig. 2 Expected result example from the thread

enter image description here enter image description here

Expected output: dynamic point tunnelling i.e. drawing of darker gray area around the points like in Fig. 2 but spanning for each facet individually

R: 3.4.0 (backports)
OS: Debian 8.7

Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
  • I can't tell from this what you're looking for, I think you're going to have to be more specific about what the correct result should look like. – joran May 24 '17 at 20:20
  • Do you mean you want a gray rectangle spanning `range(x)` and `range(y)` for the points within a given facet? – eipi10 May 24 '17 at 20:31

1 Answers1

2

You can define the yrange for each facet and then use that in the plot:

library(tidyverse)

ggplot(mtcars %>% group_by(cyl) %>% 
         mutate(miny=min(mpg),
                maxy=max(mpg)), 
       aes(wt, mpg, group = cyl, 
           colour = factor(cyl), 
           ymin = miny, ymax = maxy)) +
  geom_ribbon(alpha=0.2, colour=NA)+ 
  geom_line() +       
  geom_point()  +      
  facet_wrap(~cyl) +
  theme_bw()

enter image description here

Here's a function that allows you to specify the data frame, the x and y variables and the facetting/grouping variable.

my_plot = function(data, xx, yy, ff) {

  yyr = range(data[,yy])

  ggplot(data, aes_string(xx, yy, ff)) +
    geom_ribbon(aes(ymin = yyr[1], ymax = yyr[2]), alpha=0.2, colour=NA)+ 
    geom_line() +       
    geom_point()  +      
    facet_grid(paste0("~ ", ff)) +
    theme_bw()
}

my_plot(iris, "Petal.Width", "Sepal.Width", "Species")

enter image description here

If what you really wanted were confidence bands, then use geom_smooth:

ggplot(iris, aes(Petal.Width, Sepal.Width, colour=Species, fill=Species)) +
  geom_smooth(method="lm") +
  geom_point(size=1)  +    
  facet_grid(. ~ Species) +
  theme_bw()

enter image description here

eipi10
  • 91,525
  • 24
  • 209
  • 285
  • `geom_ribbon` is the important part here. – shadowtalker May 24 '17 at 20:36
  • `data.table` approach would be great too. – Léo Léopold Hertz 준영 May 24 '17 at 20:36
  • You mean you want the rectangle to cover the full y-range of the panel in all panels? – eipi10 May 24 '17 at 20:40
  • @LéoLéopoldHertz준영 Instead of taking the min/max for each group just add & subtract a fixed amount to each value for the ymax and ymin values. This really isn't anything to do with ggplot or dplyr/data.table, it's just what values you use pass to ymin/ymax in geom_ribbon. The point being, calculate the values you want up front. – joran May 24 '17 at 20:41
  • @LéoLéopoldHertz준영 This is very confused; the other post you cite is simply using pre-calculated bounds for several groups, who knows how they calculated them? If you want some sort of fitted model with error bands maybe you're looking for `geom_smooth`. – joran May 24 '17 at 20:44
  • What is the point of the point `.` in `facet_grid(. ~ Species) +`? - - I have cases where code works only without the point but not with exact reprouction of your code. - - What is the minimum amount of points for the `geom_smooth` to work? I think more than 2. – Léo Léopold Hertz 준영 May 25 '17 at 10:02
  • How can you prevent `theme_bw()` from overwrititing `theme(axis.text.x = element_text(angle = 90, hjust = 1))`? – Léo Léopold Hertz 준영 May 25 '17 at 10:16
  • To answer your questions in order: In the `facet_grid` formula, the order is `row_facets ~ column_facets`. The `.` means no facets. `geom_smooth` with `method="lm"` will work with 2 or more points, but you'll need at least 3 non-colinear points to see a confidence interval. Put any additional `theme` statements after `theme_bw`, otherwise `theme_bw` will override them. – eipi10 May 25 '17 at 13:23