How to add different lines for facets

Make sure that the variable species is identical in both datasets. If it a factor in one on them, then it must be a factor in the other too library(ggplot2) dummy1 <- expand.grid(X = factor(c(“A”, “B”)), Y = rnorm(10)) dummy1$D <- rnorm(nrow(dummy1)) dummy2 <- data.frame(X = c(“A”, “B”), Z = c(1, 0)) ggplot(dummy1, aes(x = … Read more

different size facets proportional of x axis on ggplot 2 r

If I understand you correctly, space = “free_x” does what you want in facet_grid. As far as I know, facet_wrap has never supported a space argument, but many facet_wrap commands can be cast as facet_grid commands. library(ggplot2) ggplot(mydf, aes(X, Y)) + geom_point()+ facet_grid (.~ groups, scales = “free_x”, space = “free_x”) And if you want … Read more

How do you add a general label to facets in ggplot2?

As the latest ggplot2 uses gtable internally, it is quite easy to modify a figure: library(ggplot2) test <- data.frame(x=1:20, y=21:40, facet.a=rep(c(1,2),10), facet.b=rep(c(1,2), each=20)) p <- qplot(data=test, x=x, y=y, facets=facet.b~facet.a) # get gtable object z <- ggplotGrob(p) library(grid) library(gtable) # add label for right strip z <- gtable_add_cols(z, unit(z$widths[[7]], ‘cm’), 7) z <- gtable_add_grob(z, list(rectGrob(gp = … Read more

R + ggplot2 => add labels on facet pie chart [duplicate]

I would approach this by defining another variable (which I call pos) in df that calculates the position of text labels. I do this with dplyr but you could also use other methods of course. library(dplyr) library(ggplot2) df <- df %>% group_by(year) %>% mutate(pos = cumsum(quantity)- quantity/2) ggplot(data=df, aes(x=factor(1), y=quantity, fill=factor(prod))) + geom_bar(stat=”identity”) + geom_text(aes(x= … Read more

Setting individual axis limits with facet_wrap and scales = “free” in ggplot2

Here’s some code with a dummy geom_blank layer, range_act <- range(range(results$act), range(results$pred)) d <- reshape2::melt(results, id.vars = “pred”) dummy <- data.frame(pred = range_act, value = range_act, variable = “act”, stringsAsFactors=FALSE) ggplot(d, aes(x = pred, y = value)) + facet_wrap(~variable, scales = “free”) + geom_point(size = 2.5) + geom_blank(data=dummy) + theme_bw()

How to order data by value within ggplot facets

We can use (1) reorder_within() function to reorder term within tissue facets. library(tidyverse) library(forcats) tdat <- tdat %>% mutate(term = factor(term), tissue = factor(tissue, levels = c(“tissue-C”, “tissue-A”, “tissue-D”, “tissue-B”), ordered = TRUE)) reorder_within <- function(x, by, within, fun = mean, sep = “___”, …) { new_x <- paste(x, within, sep = sep) stats::reorder(new_x, by, … Read more

Nested facets in ggplot2 spanning groups

I’m sorry necroing this thread and unintended self-promotion, but I had a go at generalizing this to a facet_nested() function and it can be found in the ggh4x package. The function isn’t tested extensively but I thought it might be of some convenience to people. Maybe some good feedback will come from this. There are … Read more