0

I have many dataframe school_skill_score_ff which has three column 'Skill_full_form', '2020', '2021'. I'm trying to create a pipeline for bar graph. My code is here.

school_bar<-school_skill_score_ff%>%
     e_chart(Skill_full_form,backgroundColor='#0d1117',center=c('50%','35%'))%>%
     e_bar(`2021`,label=list(show=TRUE,color='#fff',position='top'))%>%
     e_bar(`2020`,label=list(show=TRUE,color='#fff',position='top'))%>%  
     #e_tooltip(trigger = "axis")%>%
     e_tooltip(trigger = "axis",axisPointer=list(type='shadow'))%>%  
     #e_title("Skills Score") %>%
     e_theme("westeros")%>%
     e_toolbox_feature("dataZoom")%>%
     e_animation(duration = 2000)%>%
     e_hide_grid_lines('x')%>%
     e_y_axis(splitLine=list(lineStyle=list(color='#0f375f')),axisLabel=list(fontSize=10,color='#fff',fontWeight='normal'),name='Skill Score',nameLocation='middle',nameGap=30,nameColor='#fff')%>%  
     e_x_axis(axisLabel=list(rotate=16,fontSize=10,color='#fff',fontWeight='normal'))%>%  
     #e_axis_labels(x='Skill Name', y = "Skill Score")%>%
     e_y_axis(name='Skill Score',nameLocation='middle',nameGap=38,splitLine=list(lineStyle=list(color='#0f375f')))%>%
     #e_title("School Skill Score",top='0%',left='45%',textStyle=list(color='#fff'))%>%
      e_legend(top='8%',left='46%',textStyle=list(color='#fff'))%>%
      #e_title("School Skill Graph",textStyle=list(color='#fff',fontWeight='normal'),left='center',top='5%') %>%
      #e_x_axis(axisLabel=list(rotate=17,fontSize=12))%>%
      e_grid(show=TRUE,top='4%',width='90%',left='5%')#,name='Skill Names',nameGap=60,nameLocation='middle')

My issue is that I'm fetching the file directly from an excel file and many files doesn't have column '2020'. This generates an error. I want to handle this error automatically such that graph for one column at least appear when 2nd column is absent. I'm using echarts4r library.

stefan
  • 90,330
  • 6
  • 25
  • 51

1 Answers1

0

One approach to achieve your desired result would be to reshape your data to long format to add a column year to your data and group your data by year before passing it to e_charts. This way different number of years are treated automatically.

Using some toy example data this approach works fine for two years ...

library(echarts4r)
library(dplyr)
library(tidyr)

# Example data
school_skill_score_ff <- data.frame(
  LETTERS[1:10],
  1:10,
  11:20
)
names(school_skill_score_ff) <- c("Skill_full_form", "2020","2021")

school_skill_score_ff_long <- school_skill_score_ff %>% 
  tidyr::pivot_longer(-Skill_full_form, names_to = "year", values_to = "score")

school_skill_score_ff_long %>%
  group_by(year) %>% 
  e_chart(Skill_full_form,backgroundColor='#0d1117',center=c('50%','35%'))%>%
  e_bar(score, label=list(show=TRUE,color='#fff',position='top'))

enter image description here

as well as for only one year present in the data:

# Now remove column 2020
school_skill_score_ff <- select(school_skill_score_ff, -`2020`)

school_skill_score_ff_long1 <- school_skill_score_ff %>% 
  tidyr::pivot_longer(-Skill_full_form, names_to = "year", values_to = "score")

school_skill_score_ff_long1 %>%
  group_by(year) %>% 
  e_chart(Skill_full_form,backgroundColor='#0d1117',center=c('50%','35%'))%>%
  e_bar(score, label=list(show=TRUE,color='#fff',position='top'))

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Hey...Thanks. This is a great technique or else I was about to use tryCatch. But that'd make my code lengthy. Thanks man – Mausam Singh Jun 26 '21 at 18:29