分组条形图

时间:2019-12-16 04:11:40

标签: r rstudio plotly

我正尝试使用 plotly 使用以下数据制作条形图:

enter image description here

对此有任何帮助,我们深表感谢。

我希望它看起来与此图类似,但它不是男人和女人,而是以年份或收入作为传说 enter image description here

Dataframe:

maleincomequalification <-read.csv("MaleIncomeQualification.csv")
femaleincomequalification <-read.csv("FemaleIncomeQualification.csv")

column names/variables:

 "Year"                                      
 "No.Qualification"                          
 "Lower.secondary.school.qualification"      
 "Upper.secondary.school.qualification"      
 "Level.1.3.post.school.certificate"         
 "Level.4.6.certificate.or.diploma"          
 "Bachelors.degree.and.level.7.qualification"
 "Postgraduate.qualification"

data.frame(Year = 2013:2019, 
                   No_Qualification = c(561,575,579, 627, 682, 674, 707), 
                   Lower_secondary_school_qualification = c(678, 690, 686, 759, 710, 770, 798), 
                   Upper_secondary_school_qualification = c(626, 605, 645, 688, 722, 761, 805), 
                   Level_1_3_post_school_certificate = c(631, 651, 657, 660, 672, 703, 762),
                   Level_4_6_certificate_or_diploma = c(753, 780, 763, 848, 844, 815, 863), 
                   Bachelors_degree = c(980,1006,1028, 1077, 1091, 1125, 1151), 
                   Postgraduate_qualification = c(1178, 1245, 1244, 1331, 1399, 1385, 1474))

1 个答案:

答案 0 :(得分:0)

这是一种解决方案:

data <- data.frame(Year = 2013:2019, 
                   No_Qualification = c(561,575,579, 627, 682, 674, 707), 
                   Lower_secondary_school_qualification = c(678, 690, 686, 759, 710, 770, 798), 
                   Upper_secondary_school_qualification = c(626, 605, 645, 688, 722, 761, 805), 
                   Level_1_3_post_school_certificate = c(631, 651, 657, 660, 672, 703, 762),
                   Level_4_6_certificate_or_diploma = c(753, 780, 763, 848, 844, 815, 863), 
                   Bachelors_degree = c(980,1006,1028, 1077, 1091, 1125, 1151), 
                   Postgraduate_qualification = c(1178, 1245, 1244, 1331, 1399, 1385, 1474))


library(plotly)
library(tidyverse)

data %>% 
  plot_ly(x = ~Year,y = ~No_Qualification, type = 'bar', name = 'No_Qualification') %>%
  add_trace(y = ~Lower_secondary_school_qualification, name = 'Lower_secondary_school_qualification') %>%
  add_trace(y = ~Upper_secondary_school_qualification, name = 'Upper_secondary_school_qualification') %>%
  add_trace(y = ~Level_1_3_post_school_certificate, name = 'Level_1_3_post_school_certificate') %>%
  add_trace(y = ~Level_4_6_certificate_or_diploma, name = 'Level_4_6_certificate_or_diploma') %>%
  add_trace(y = ~Bachelors_degree, name = 'Bachelors_degree') %>%
  add_trace(y = ~Postgraduate_qualification, name = 'Postgraduate_qualification') %>%
  layout(yaxis = list(title = 'Count'), barmode = 'group')