seperate units and title from the facet_grid strip

Hi,

have a plot in which I used the facet_grid function. In that plot, I have titles and units together.


I want to separate the title and unit, or I want to create another strip for the units along with title strip.
So, one strip for the title and one strip for units

I hope I am clear on my point.

My code is

levels(BOX_HOURLY$variable) <- c(  expression(Temp ~ (degree*C)) ,
                                                    "SO[4]  ~ '('*mu*g~m^-3*')'",
                                                    "OA  ~ '('*mu*g~m^-3*')'",
                                                    "(SO[4]: OA)",
                                                    "SO[2]  ~ '('*ppb*')'","NO[x]",
                                                    "D[p](nm)",
                                                    "TNA ~ '('*cm^-3*')'")
      
      BOX_HOURLY %>%
        ggplot(aes(x=hour, y=value, color=variable, group = hour, fill = variable,alpha=0.2)) + 
        geom_boxplot(notch = F,outlier.shape = NA) +
        ylab("")+
        xlab("")+
        theme(legend.position="none")+
        theme(legend.title=element_blank())+
        theme(legend.text=element_text(size=12)) + 
        theme(axis.title = element_text(face="plain",size=14,color="black"),
              axis.text=element_text(size=14,face="plain", color="black"),
              axis.text.x=element_text(hjust=0.5),
              plot.title = element_text(size=15))+
        theme(strip.text = element_text(size=15, color="black"))+
        facet_grid(variable ~., switch = "x", scales = "free", space = "free_x",
                   labeller = label_parsed)+
        annotate("rect", fill = "red", alpha = 0.15, 
                 xmin = -0.5,
                 xmax = 9.5,
                 ymin = -Inf, 
                 ymax = Inf) 

Hi @kunal.bali9. If I am understanding the question correctly, it looks like you need to split variable into columns of title and units. Then you can do the following:

facet_grid(title + units ~ ., switch = "x", scales = "free", space = "free_x",
                   labeller = label_parsed)

Below is an example using the mtcars dataset.

library(tidyverse)

df = mtcars[1:10,] |>
  mutate(cyl = paste('cyl:', cyl),
         gear = paste('gear:', gear))

ggplot(df, aes(x = disp, y = hp)) +
  geom_point() +
  facet_grid(cyl + gear ~.)

Thank You for your time.

Actually, I don't have separate columns for title and unit. I changed my variable name with new variable name including unit as well.

for example, Initially I have a variable, named "Temp", I changed into new name with unit like
expression(Temp ~ (degree*C) and then I changed my data format in 3 columns via

melt(id.vars=1:1)

Now the data is given in 3 columns as
Screenshot 2024-01-31 at 5.12.54 PM

Like wise I have 6 more variables with units.

BOX_HOURLY$variable) <- c(  expression(Temp ~ (degree*C)) ,
                                                    "SO[4]  ~ '('*mu*g~m^-3*')'",
                                                    "OA  ~ '('*mu*g~m^-3*')'",
                                                    "(SO[4]: OA)",
                                                    "SO[2]  ~ '('*ppb*')'","NO[x]",
                                                    "D[p](nm)",
                                                    "TNA ~ '('*cm^-3*')'")

Now, I want to separate, for example, "Temp" and "degree *C."
If I separate the title and unit, then I don't have to make the image larger. I can keep the image size short.

Thanks

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.