Untitled

 avatar
unknown
r
2 years ago
1.3 kB
9
Indexable
library(readxl)
library(ggplot2)

file <- "TIME_USE_24092022.csv"

string_to_number <- function(value) {
    number_value <- as.numeric(value)
}

read_file <- function(file_name) {
    data <- read.csv(nome_ficheiro, encoding = "UTF-8")
    colsNames <- c("Pais", "Ocupacao", "Sexo", "Tempo")
    names(data) <- colsNames
    # we read the data from China, Norway and Finland and ignore the total values
    filtered_data <- subset(data, (Pais == "China" | Pais == "Noruega" | Pais == "Finlândia") & Sexo != "Total")
    plot_data <- data.frame(filtered_data["Pais"], filtered_data["Ocupacao"], filtered_data["Sexo"], lapply(filtered_data["Tempo"], string_to_number))
    colnames(plot_data) <- colsNames
    plot_data
}


plot_data <- read_file(file)


draw_plot <- function() {
    ggplot(plot_data, aes(x = Sexo, y = Tempo, fill = Ocupacao)) +
        labs(
            fill = "Ocupação",
            x = "",
        ) +
        geom_bar(stat = "identity", position = position_stack()) +
        scale_fill_brewer(palette = "Paired") +
        theme_minimal() +
        facet_grid(~ Pais, space = "free_x", scales = "free_x", switch = "x") +
        theme(
            strip.placement = "outside",
            panel.spacing = unit(0, "cm")
        )
}

draw_plot()
Editor is loading...