How to Get Geom_Vline to Honor Facet_Wrap

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

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

How to use loop with geom_vline and facet_wrap?

I'm not sure that this is the best way, but you could do all of them in one go using map2 from tidyr. This would save you time from having to write out individual sapply.

library(tidyverse)

ggplot(dat) +
geom_boxplot(aes(y = daily_count,
x = yearmonth(date),
group = paste(yearmonth(date), category),
fill = category)) +
labs(x = 'Month & Year',
y = 'Count',
fill = "Category") +
theme_bw() +
theme(axis.text=element_text(size=10),
axis.title=element_text(size=10),
legend.position="none") +
geom_vline(xintercept = lubridate::ymd("2013-08-23"),
linetype=1, colour="red", size = 0.5)+
map2(point_dates, category_name,
~geom_vline(data=filter(dat, category==.y),
aes(xintercept = .x),
linetype=3, colour="black", size = 1))+
facet_wrap(~category, nrow = 2)

Sample Image

Using geom_vline with facet_wrap

Change the geom_vline bit to:

geom_vline(xintercept=as.numeric(vLines$Date))

geom_vline and facet_wrap error in ggplot

This is happening because the class of V1 (the summary column returned by data.table) is table, rather than a numeric vector. Change it to a vector and it should work.

output_gg <-
ggplot(temp_plotdata, aes(x=Q9)) +
geom_histogram(binwidth=1, fill='lightblue') +
geom_vline(data=temp_plotdata[, as.vector(summary(Q9)[c(2,3,5)]), by=Treatment_Code],
aes(xintercept=V1),
linetype='dashed', color='darkred') +
facet_wrap(~ Treatment_Code, ncol=1)

Compare the structure of the data frame before and after:

str(temp_plotdata[, summary(Q9)[c(2,3,5)], by=Treatment_Code])
Classes ‘data.table’ and 'data.frame':    9 obs. of  2 variables:
$ Treatment_Code: chr "Control" "Control" "Control" "Second Mailing" ...
$ V1 :Class 'table' num [1:9] 10.25 11.5 12.75 4.75 6.5 ...
- attr(*, ".internal.selfref")=<externalptr>
str(temp_plotdata[, as.vector(summary(Q9)[c(2,3,5)]), by=Treatment_Code])
Classes ‘data.table’ and 'data.frame':    9 obs. of  2 variables:
$ Treatment_Code: chr "Control" "Control" "Control" "Second Mailing" ...
$ V1 : num 10.25 11.5 12.75 4.75 6.5 ...
- attr(*, ".internal.selfref")=<externalptr>


Related Topics



Leave a reply



Submit