Ggplot2: adding geometry without limits

I would like to add additional geometries to the ggplot density graph, but without changing the displayed data limits and without having to calculate the desired restrictions on the user code. To give an example:

set.seed(12345)
N = 1000
d = data.frame(measured = ifelse(rbernoulli(N, 0.5), rpois(N, 100), rpois(N,1)))
d$fit = dgeom(d$measured, 0.6)
ggplot(d, aes(x = measured)) + geom_density() + geom_line(aes(y = fit), color = "blue")

ggplot(d, aes(x = measured)) + geom_density() + geom_line(aes(y = fit), color = "blue") + coord_cartesian(ylim = c(0,0.025))

In the first graph, the correspondence curve (which matches the “measured” data very poorly) obscures the shape of the measured data: Actual result I would like to trim the graph to include all the data from the first geometry, but trim the appropriate curve, as in the second figure: Desired Conclusion

While I can create a second graph with coord_cartesian, this has two drawbacks:

  • I have to calculate the limits with my own code (which is bulky and error prone)
  • . (AFAIK) coord_cartesian. facet_wrap(scales = "free")

, - R-?

R: coord_cartesian facet_grid , .

+4
2

, , - fit geom_density(aes(y = ..scaled..)

fit 0 1:

d$fit_scaled <- (d$fit  - min(d$fit)) / (max(d$fit) - min(d$fit))

fit_scaled ..scaled..:

ggplot(d, aes(x = measured)) + 
  geom_density(aes(y = ..scaled..)) + 
  geom_line(aes(y = fit_scaled), color = "blue")

output_1

facet_wrap():

d$group <- rep(letters[1:2], 500) #fake group

ggplot(d, aes(x = measured)) + 
  geom_density(aes(y = ..scaled..)) + 
  geom_line(aes(y = fit_scaled), color = "blue") + 
  facet_wrap(~ group, scales = "free")

ouput_2

, :

multiplot() http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/

multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  library(grid)
  plots <- c(list(...), plotlist)

  numPlots = length(plots)

  if (is.null(layout)) {

    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
                    ncol = cols, nrow = ceiling(numPlots/cols))
  }

 if (numPlots==1) {
    print(plots[[1]])

  } else {

    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))

    for (i in 1:numPlots) {

      matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))

      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
                                      layout.pos.col = matchidx$col))
    }
  }
}

, :

multiplot(
  ggplot(d, aes(x = measured)) + 
    geom_density() +
    facet_wrap(~ group, scales = "free"),
  ggplot(d, aes(x = measured)) +  
    geom_line(aes(y = fit), color = "blue") + 
    facet_wrap(~ group, scales = "free")
)

:

output_3

, facet_grid() facet_wrap() cols = 2 multiplot():

multiplot(
  ggplot(d, aes(x = measured)) + 
    geom_density() +
    facet_grid(group ~ ., scales = "free"),
  ggplot(d, aes(x = measured)) +  
    geom_line(aes(y = fit), color = "blue") + 
    facet_grid(group ~ ., scales = "free"),
  cols = 2
)

:

output_4

+2

y-. .

d1 <- d %>% 
  mutate(max_dens=round(max(density(measured)$y), 2))

ggplot(d1, aes(x=measured)) + 
   geom_line(aes(y=fit), color = "blue") +
   geom_density() + 
   coord_cartesian(ylim = c(0, unique(d1$max_dens)))
-1

Source: https://habr.com/ru/post/1688070/


All Articles