facet_grid help

Hey R-community.
I used ggplot2, and I got this:
image

However, I think using a faceit_grid 2x2 is better to visualize the data clearer, but I have no idea on how to approach this.
My code for the plot in the image is:

       DF <- data.frame(DF)
       
       DF_long <- DF %>% pivot_longer(Jordbruk_skogbruk_fiske:Uoppgitt, names_to = "Næring", values_to = "Value")
       
       head(DF_long)
       ggplot(DF_long,aes(x = kvartal, y = Value,color = Næring, group = Næring)) + geom_line(size=1.7) + labs(x="År", y = "Antall sysselsatte") +   scale_color_manual(values = c("aquamarine4","burlywood4","chartreuse","chocolate1","black","blue","blueviolet","gold","gold4","deeppink","lightcoral","olivedrab2","tomato","saddlebrown","mediumvioletred","lightseagreen","lightslateblue"))

To help us help you, could you please turn this into a proper reproducible example (reprex) including sample data? Please have a look at this guide, to learn how to do it:

Also, try to explain what would be the grouping criteria to assign your data into four groups

My dataset :slight_smile: Called DF

# A tibble: 6 x 18
  kvartal    Jordbruk_skogbr… Bergverksdrift_… Industri Elektrisitet_va…
  <date>                <dbl>            <dbl>    <dbl>            <dbl>
1 2011-12-01             6854            23189    24869             2089
2 2012-12-01             6232            25594    25079             2110
3 2013-12-01             6069            26759    25784             2280
4 2014-12-01             5995            27242    26255             2352
5 2015-12-01             5803            24577    23926             2448
6 2016-12-01             5756            21807    22654             2680```

That is not copy/paste friendly, please read the guide I gave you and try to make a proper reproducible example

#Dataset
data.frame(
  kvartal = c("2011-12-01",
              "2012-12-01",
              "2013-12-01"),
  Jordbruk_skogbruk_fiske = c(6854,6232,
                              6069),
  Bergverksdrift_og_utvinning = c(23189,25594,
                                  26759),
  Industri = c(24869,25079,
               25784)

#Making it long 
 DF_long <- DF %>% pivot_longer(Jordbruk_skogbruk_fiske:Industri, names_to = "Næring", values_to = "Value")
 head(DF_long)  
data.frame(
  stringsAsFactors = FALSE,
  kvartal = c("2011-12-01","2011-12-01",
              "2011-12-01",
  Næring = c("Jordbruk_skogbruk_fiske",
             "Bergverksdrift_og_utvinning","Industri",
  Value = c(6854,23189,24869
)

#GGplot
ggplot(data = DF_long, aes(x=kvartal, y=Value, group=Næring, colour=Næring))
 + geom_line(size=1.7) 
+ labs(x="År", y = "Antall sysselsatte") + facet_grid(Næring ~.) + expand_limits(y=0)

Then I got this plot:

How was this my friend?

Still not reproducible your code produces an error and I can't take a look at the plotting part, which is the important one for this issue.

library(tidyverse)

DF <- data.frame(
    kvartal = c("2011-12-01",
                "2012-12-01",
                "2013-12-01"),
    Jordbruk_skogbruk_fiske = c(6854,6232,
                                6069),
    Bergverksdrift_og_utvinning = c(23189,25594,
                                    26759),
    Industri = c(24869,25079,
                 25784))
    
    #Making it long 
    DF_long <- DF %>% pivot_longer(Jordbruk_skogbruk_fiske:Uoppgitt, names_to = "Næring", values_to = "Value")
#> Error: Can't subset columns that don't exist.
#> x Column `Uoppgitt` doesn't exist.
#Making it long 
 DF_long <- DF %>% pivot_longer(Jordbruk_skogbruk_fiske:Industri, names_to = "Næring", values_to = "Value")

Try this instead

#Dataset
data.frame(
  kvartal = c("2011-12-01",
              "2012-12-01",
              "2013-12-01"),
  Jordbruk_skogbruk_fiske = c(6854,6232,
                              6069),
  Bergverksdrift_og_utvinning = c(23189,25594,
                                  26759),
  Industri = c(24869,25079,
               25784)

#Making it long 
 DF_long <- DF %>% pivot_longer(Jordbruk_skogbruk_fiske:Industri, names_to = "Næring", values_to = "Value")
 head(DF_long)  
data.frame(
  stringsAsFactors = FALSE,
  kvartal = c("2011-12-01","2011-12-01",
              "2011-12-01",
  Næring = c("Jordbruk_skogbruk_fiske",
             "Bergverksdrift_og_utvinning","Industri",
  Value = c(6854,23189,24869
)

#GGplot
ggplot(data = DF_long, aes(x=kvartal, y=Value, group=Næring, colour=Næring))
 + geom_line(size=1.7) 
+ labs(x="År", y = "Antall sysselsatte") + facet_grid(Næring ~.) + expand_limits(y=0)

Then I got this plot (You wont get this, because I limited it to only 3 for you, but I want to make a facet grid 2x2 to make it show all..

As I said, you need to define criteria to separate Næring into four groups (2x2 grid), you haven't provided enough data to exemplify so this is the best I can do with the example you have provided. (BTW this would be a proper reprex)

library(tidyverse)

DF <- data.frame(
    kvartal = c("2011-12-01",
                "2012-12-01",
                "2013-12-01"),
    Jordbruk_skogbruk_fiske = c(6854,6232,
                                6069),
    Bergverksdrift_og_utvinning = c(23189,25594,
                                    26759),
    Industri = c(24869,25079,
                 25784))

DF_long <- DF %>%
    pivot_longer(Jordbruk_skogbruk_fiske:Industri,
                 names_to = "Næring",
                 values_to = "Value") %>% 
    mutate(group = case_when(
        Næring %in% c("Bergverksdrift_og_utvinning") ~ 1,
        Næring %in% c("Industri") ~ 2,
        Næring %in% c("Jordbruk_skogbruk_fiske") ~ 3
    )) 

ggplot(data = DF_long, aes(x=kvartal, y=Value, group=Næring, colour=Næring)) +
    geom_line(size=1.7) +
    labs(x="År", y = "Antall sysselsatte") +
    facet_wrap(~group, nrow = 2, ncol = 2) +
    expand_limits(y = 0) +
    theme(strip.background = element_blank(),
          strip.text.x = element_blank())

1 Like

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