I have a solution to format nested facet labels that is working really well. The solution was provided here .
I needed to raise another question as I need to be able to subset a list, but apparently I cant figure it out without help!
What I need to do:
I want to be able to exclude Fac2 from the list. So the graph will only show Fac1 and Fac3. I think I need to subset this list List <- split(data,data$Fac_Map)
.
- In the real data it is a long list of Facs and a wide graph so I want to exclude rather than list all of the inclusions.
- I will be putting Fac2 at the end of the graph joined by ggarrange (there are two reasons for this but no need to go into the complexity of the real data). So I need to be able to filter the list within the first function G.
Data:
data <- structure(
list(
Fac_Map = structure(
c(1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 3L, 3L, 3L, 3L),
.Label = c("Fac1", "Fac2", "Fac3"),
class = "factor"
),
S_Residency = structure(
c(1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L,
1L, 1L, 2L, 2L),
.Label = c("Intl", "Local"),
class = "factor"
),
Period = structure(
c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L),
.Label = c("2019 P2", "2020 P2"),
class = "factor"
),
Result = c(92.9, 91.1, 85.8, 87.9, 94.1, 91.7, 87.5, 88.6,
90, 90.1, 87.4, 88.9)
),
class = "data.frame",
row.names = c(NA,-12L)
)
Working solution to format facet labels provided by @Duck here
#Create list
List <- split(data,data$Fac_Map)
#Now function to plot
myplotfun <- function(x)
{
G <- ggplot(x,aes(x=Period,y=Result))+
geom_point() +
facet_grid(. ~ S_Residency,
scales = "free", space = "free") +
ylim(range(x$Result)) +
xlab("")+ylab("")+ggtitle(unique(x$Fac_Map))
return(G)
}
#Apply for plots
List2 <- lapply(List,myplotfun)
#Wrap final plot
wrap_plots(List2,nrow = 1)
Is it possible to filter the list so I can just have Fac1 and Fac3 without having to subset the whole dataset which would prevent a ggarrange?
The output of the code: