Sunday, February 11, 2024

Tidy Tuesday for 06-02-2024

 

Tidy Tuesday 06-02-2024
#Load the libraries
library(tidyverse)

Task

Task is to reproduce the graph below. Even though it is one of the two but I could finish only one in my weekend.

The Data

This week’s data is on three Nordic countries i.e. Norway, Denmark & Sweden. Let’s check out the data

tuesData <- read_csv('D:/R Script/Tidytuesday/06-02-24/heritage.csv')
head(tuesData)
## # A tibble: 3 × 3
##   country `2004` `2022`
##   <chr>    <dbl>  <dbl>
## 1 Norway       5      8
## 2 Denmark      4     10
## 3 Sweden      13     15
#The data needs to be turned into 'tidy' data using a `r pivot_longer()` function.
tuesDataLong <- tuesData |> 
  pivot_longer(cols = c(`2004`,`2022`), 
               names_to = "Year", 
               values_to = "values")
tuesDataLong |> 
  head(10)
## # A tibble: 6 × 3
##   country Year  values
##   <chr>   <chr>  <dbl>
## 1 Norway  2004       5
## 2 Norway  2022       8
## 3 Denmark 2004       4
## 4 Denmark 2022      10
## 5 Sweden  2004      13
## 6 Sweden  2022      15

The Plot

Now, the data is ready, let’s attempt first graph

p <- tuesDataLong |> 
  ggplot(aes(x = Year, y = values)) + 
  # put horizontal lines at a difference of 5 units, set colour & linewidth.
  geom_hline(yintercept = seq(0, 35, 5), 
             colour = 'grey',
             linewidth = 0.4) +
  # Here call the barplot geom i.e. geom_col, set width & for stacked bars use position = position_stack() and for making it as per the graph put reverse = TRUE.
  geom_col(aes(fill = country),
           width = 0.4,
           position = position_stack(reverse = TRUE))  + 
  # set the colours manually by giving HTML colour codes, put guide = 'none' so that legends will not be printed 
  scale_fill_manual(values = c('#2f3755','#f25545', '#3274d8'), 
                    guide = 'none') +
  # label the plot using geom_text, set aesthetics, data is being called by the |> pipe
  geom_text(aes(x = Year, y = values, label = values),
            vjust = 1,
            color = 'white',
            size = 4,
            position = position_stack(vjust = 0.5))+
  #setting theme_void so that least things to work with - as the background and grids are almost empty so it seemed logical.
  theme_void() +
  # get more control of panel here important is to set some margin in the right side of the plot
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    axis.text = element_text(
      colour = 'grey50',
      size = 12
    ),
    plot.margin = unit(c(1,10,1,5), 'lines'),
    axis.text.y = element_text(margin = margin(0,10,0,0)) # here putting gap/padding between axis label and tick marks
  ) +
  #set the y-axis breaks
  scale_y_continuous(breaks = seq(0, 35, 5))

p

Add the remaining labels i.e. 22 & 33. For that a new data frame I’m creating.

# As seen above the dataframe did not have 22 & 33 but the plot has them so lets create them.
df_addlabel <- data_frame(Year = c("2004", "2022"),
                          values = c(22, 33))
# Now, let's place them using geom_text
p <- p + geom_text(data = df_addlabel, # using the newly created dataset.
              aes(x = Year, 
                  y = values, 
                  label = values),
              vjust = -1, # plot it above the bar, if + it goes inside the bar
              colour = 'black',
              fontface = 'bold'
)
p

Last bit is to annotate the graph with country names

# using annotate to place country names
plt <- p +  annotate(geom = "text", 
                   x = 3, 
                   y = c(4, 13, 26), 
                   label = c("Norway", "Denmark", "Sweden"), 
                   colour = c('#2f3755','#f25545', '#3274d8'),
                   fontface = 'bold') +
          coord_cartesian(xlim = c(0.9, 2.1), clip="off") # this is important otherwise the hlines will extend outwards.
  
plt

So, apart from font-type which I could not decide upon, rest looks fine to me. Leave your comments.

No comments:

Post a Comment