How to Produce Different Geom_Vline in Different Facets in R

How to produce different geom_vline in different facets in R?

Here's how you can put in different geom_vline for different iris species:

ggplot(iris, aes(Sepal.Length, Petal.Length)) + facet_wrap(~Species, scales="free") + geom_point() + 
geom_vline(data=filter(iris, Species=="setosa"), aes(xintercept=5), colour="pink") +
geom_vline(data=filter(iris, Species=="versicolor"), aes(xintercept=6), colour="blue") +
geom_hline(data=filter(iris, Species=="virginica"), aes(yintercept=6), colour="green")

Sample Image

Adding grouped geom_vline to multiple facets

Facets don't use the group aesthetic. The plot is faceting on Well, so cpdates needs a Well variable.

Here is some dummy data to reproduce the problematic plot.

library('ggplot2')
library('dplyr')

cpdates <- data.frame(
Well_LOC = c(4, 5, 6, 8),
CPT_DATE = c(1, 2, 3,4))

dd <- data.frame(
Date = mpg$displ,
TDS = mpg$cty,
Well = mpg$cyl)


dd %>%
ggplot(aes(Date, TDS)) +
theme_bw() +
geom_point(aes(group = Well, color = Well), size = 1) +
ggtitle(paste("WMA A-AX TDS")) +
ylab("mg/L") +
geom_smooth(aes(group = Well), method = "loess", color = "black", se = FALSE) +
geom_vline(data = cpdates, aes(group = Well_LOC, xintercept=as.numeric(CPT_DATE)), color ="blue", lwd=0.5, lty=1) +
facet_grid(Well~.)

bad plot

Adding Well to cpdates fixes the problem. We can also get rid of the group asthetic.

cpdates$Well = cpdates$Well_LOC

dd %>%
ggplot(aes(Date, TDS)) +
theme_bw() +
geom_point(aes(group = Well, color = Well), size = 1) +
ggtitle(paste("WMA A-AX TDS")) +
ylab("mg/L") +
geom_smooth(method = "loess", color = "black", se = FALSE) +
geom_vline(data = cpdates, aes(xintercept=as.numeric(CPT_DATE)), color ="blue", lwd=0.5, lty=1) +
facet_grid(Well~.)

fixed plot

How to get geom_vline to honor facet_wrap?

If you pass in the presumarized data, it seems to work:

ggplot(mydata, aes(x=sensitivity, weight=contrib)) +
geom_bar(binwidth=1) +
geom_vline(data = ddply(mydata, "panel", summarize, wavg = sum(contrib)), aes(xintercept=wavg)) +
facet_wrap(~ panel) +
ylab("contrib") +
theme_bw()

Sample Image

Different vertical line for each facet in ggplot

The issue was with that 'Mean' variable. Its format is not consistent with primary dataset. Structure it in the same way and the faceting will work as expected.

library(dplyr)
library(tidyr)
library(ggplot2)
# For all characteristics :
Mean <- colMeans(iris[-5]) %>% as_tibble(rownames = "Characteristic")
iris %>% pivot_longer(cols = !Species,names_to = "Characteristic",values_to = "Value") %>%
ggplot(aes(x=Value,fill=Species))+
geom_density()+
geom_vline(aes(xintercept = value),data=Mean) +
facet_wrap(~Characteristic)

Can you specify different geoms for different facets in a ggplot?

here is another approach by subsetting data:

ggplot(mtcars, aes(mpg, disp)) + facet_wrap(~cyl) + 
geom_point(data = subset(mtcars, cyl == 4)) +
geom_line(data = subset(mtcars, cyl == 6)) +
geom_text(data = subset(mtcars, cyl == 8), aes(label = gear))

Sample Image



Related Topics



Leave a reply



Submit