Advertisement
GTnik

Untitled

Apr 26th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. library(tidyverse)
  2. library(ggplot2)
  3.  
  4. # First graph
  5. percent <- function(x, digits = 0, format = "f", ...) {
  6. paste0(formatC(100 * x, format = format, digits = digits, ...), "%")
  7. }
  8. job_data <- read_csv("data_01.csv", col_types= cols(Women = col_double(), Men = col_double()))
  9. job_data <- job_data[-c(28), ]
  10. job_data$sorted_descr <- factor(job_data$descr, levels = job_data$descr[order(job_data$Men)])
  11. job_data$custom_color <- ifelse(job_data$Men >= job_data$Women, rgb(076, 101, 112, maxColorValue = 255),
  12. ifelse(job_data$Women >= job_data$Men, rgb(177, 89, 83, maxColorValue = 255), "grey"))
  13. job_data$men_perc <- percent(job_data$Men)
  14. job_data$women_perc <- percent(job_data$Women)
  15. job_data$custom_women_loc <- ifelse(job_data$Men >= job_data$Women, 1.3,
  16. ifelse(job_data$Women >= job_data$Men, -0.3, 0))
  17. job_data$custom_men_loc <- ifelse(job_data$Men >= job_data$Women, -0.3,
  18. ifelse(job_data$Women >= job_data$Men, 1.3, 0))
  19.  
  20.  
  21. ggplot(job_data) +
  22. geom_segment(aes(x=sorted_descr, xend=sorted_descr, y=Women, yend=Men), color=job_data$custom_color, size=2, alpha=0.7) +
  23. geom_point(aes(x=sorted_descr, y=Women), color=rgb(177, 89, 83, maxColorValue = 255), size=2) +
  24. geom_text(aes(x=sorted_descr, y=Women,label=women_perc), hjust=job_data$custom_women_loc, vjust=0.3, size=3, color=rgb(177, 89, 83, maxColorValue = 255)) +
  25. geom_point(aes(x=sorted_descr, y=Men), color=rgb(076, 101, 112, maxColorValue = 255), size=2) +
  26. geom_text(aes(x=sorted_descr, y=Men,label=men_perc), hjust=job_data$custom_men_loc, vjust=0.3, size=3, color=rgb(076, 101, 112, maxColorValue = 255)) +
  27. coord_flip() +
  28. theme_light() +
  29. scale_y_continuous(breaks = seq(0, 1, 0.1)) +
  30. theme(
  31. legend.position = "none",
  32. panel.border = element_blank(),
  33. ) +
  34. xlab("") +
  35. ylab("")
  36.  
  37.  
  38. # Second graph
  39. library(tidyverse)
  40.  
  41. women_rate <- read_csv("data_02.csv")[ ,1:6]
  42. gender_dataset_clean <- women_rate %>%
  43. gather(key, Value, -year) %>%
  44. separate(key, c("IndustryName"), "\\.") %>%
  45. filter(IndustryName != 'Management, administration, banking & insurance, legal professions')
  46. gender_dataset_clean$NumValue <- as.numeric(gender_dataset_clean$Value)
  47. gender_dataset_clean$FactorIndustryName <- as.factor(gender_dataset_clean$IndustryName)
  48. print.data.frame(gender_dataset_clean)
  49.  
  50. ggplot(gender_dataset_clean) +
  51. geom_smooth(aes(x=year, y=NumValue, group = FactorIndustryName, col = FactorIndustryName),se=FALSE) +
  52. geom_point(data = subset(gender_dataset_clean, year == max(year)|year == min(year)),
  53. aes(x = year, y = NumValue, group = FactorIndustryName, col = FactorIndustryName), shape=18, size = 3)+
  54. geom_text(data = subset(gender_dataset_clean, year == max(year)),
  55. aes(x = year, y = NumValue, label = FactorIndustryName, col = FactorIndustryName))+
  56. theme_bw()+
  57. theme(panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(),panel.border = element_blank())+
  58. theme(legend.position = "none")+
  59.  
  60. scale_y_continuous(breaks = seq(0, 600000, 50000)) +
  61. scale_x_continuous(breaks = seq(1970, 2016, 5)) +
  62. scale_color_brewer(palette = "Reds") +
  63. geom_text(data = subset(gender_dataset_clean, year == max(year)), aes(label = IndustryName, colour = FactorIndustryName, x = Inf, y = NumValue), hjust = -.1) +
  64. theme(plot.margin = unit(c(1,3,1,1), "lines"))+
  65. xlab("") +
  66. ylab("") +
  67. ggtitle("The rise in the employment rate of women has mostly happened in higher-qualified sectors",
  68. subtitle = "Female labour participation in Switzerland by job cluster, 1970-2016")
  69.  
  70.  
  71. # Third graph
  72. library(readxl)
  73. library(ggplot2)
  74. library(tidyr)
  75.  
  76. yea = c(2016,2015,2014,2013,2012,2011,2010,2000,1990,1980,1970)
  77. my_data3 = data.frame()
  78. for(i in 4:14){
  79. dat = read_xlsx('data_03.xlsx', sheet = i, skip = 4)
  80. dat = dat[440:456,5:8]
  81. names(dat) = c('Profession', 'Total', 'Men', 'Women')
  82. dat$Women = as.numeric(dat$Women)
  83. dat$Men = as.numeric(dat$Men)
  84. dat$Total = as.numeric(dat$Total)
  85. dat$Men = round(dat$Men / dat$Total, digits = 4) * 100
  86. dat$Women = 100 - dat$Men
  87. dat$Total = 100
  88. dat$Year = yea[i-3]
  89. my_data3 <- rbind(my_data3, dat)
  90. }
  91.  
  92. head(my_data3)
  93. my_data_4 <- my_data3 %>%
  94. split(my_data3$Profession) %>%
  95. map_df(~data.frame(men_approx = approx(my_data3$Year, my_data3$Men, n = 80),
  96. women_approx = approx(my_data3$Year, my_data3$Women, n = 80),
  97. Profession = my_data3$Profession))
  98. head(my_data_4)
  99.  
  100. ggplot(my_data3, aes(x = Year)) +
  101. geom_ribbon(aes(ymin = Men, ymax = Women, fill = Men < Women)) +
  102. geom_line(aes(y = Men), color = "black") + geom_line(aes(y = Women), color = "red") +
  103. scale_fill_manual(values=alpha(c("black", "red"), 0.4), name="fill") +
  104. facet_wrap(Profession ~ ., ) +
  105. theme(legend.position = "none", axis.ticks.length = unit(.25, "cm")) +
  106. xlab("") +
  107. ylab("")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement