Wednesday, August 26, 2020

Blog 35: Line Chart using plotly in R

Line Chart using plotly library


Introduction

In this blog, we will look at how to create a simple line chart using plotly library


Installing libraries

Lets install plotly and other libraries used to create the plot

package.name<-c("dplyr","tidyr","Ecdat","plotly")

for(i in package.name){

  if(!require(i,character.only = T)){

    install.packages(i)
  }
  library(i,character.only = T)

}


# Ecdat package has the 'Health Insurance and Hours Worked By Wives' data
data(HI)
df<-HI
head(df)
  whrswk hhi whi hhi2  education  race hispanic experience kidslt6 kids618
1      0  no  no   no 13-15years white       no       13.0       2       1
2     50  no yes   no 13-15years white       no       24.0       0       1
3     40 yes  no  yes    12years white       no       43.0       0       0
4     40  no yes  yes 13-15years white       no       17.0       0       1
5      0 yes  no  yes  9-11years white       no       44.5       0       0
6     40 yes yes  yes    12years white       no       32.0       0       0
   husby       region   wght
1 11.960 northcentral 214986
2  1.200 northcentral 210119
3 31.275 northcentral 219955
4  9.000 northcentral 210317
5  0.000 northcentral 219955
6 15.690 northcentral 208148


Step 1:Average experience for different levels of education


interim.df<-df%>%
  group_by(education)%>%
  summarise(MeanExperience=mean(experience))

interim.df
# A tibble: 6 x 2
  education  MeanExperience
  <ord>               <dbl>
1 <9years              32.0
2 9-11years            27.7
3 12years              25.0
4 13-15years           20.4
5 16years              18.2
6 >16years             19.3


Step 2:Initialising the plotly object

# For font
t = list(family = "Georgia",color = 'black')


line_chrt <- interim.df %>% plot_ly(x = ~education, y = ~MeanExperience, type = 'scatter', mode = 'lines',
                   textposition = 'inside',
                   text = ~paste(interim.df$MeanExperience),
                   textinfo = 'text',
                   insidetextfont = list(color = '#FFFFFF'),
                   hovertemplate = paste(round(interim.df$MeanExperience,2),'<extra></extra>'),
                   
                   marker = list(colors = colors,
                                 line = list(color = '#FFFFFF', width = 1)),
                   #The 'pull' attribute can also be used to create space between the sectors
                   showlegend = TRUE,width = 500)

line_chrt
Warning: `arrange_()` is deprecated as of dplyr 0.7.0.
Please use `arrange()` instead.
See vignette('programming') for more help
This warning is displayed once every 8 hours.
Call `lifecycle::last_warnings()` to see where this warning was generated.
A marker object has been specified, but markers is not in the mode
Adding markers to the mode...
Warning: 'scatter' objects don't have these attributes: 'textinfo', 'insidetextfont'
Valid attributes include:
'type', 'visible', 'showlegend', 'legendgroup', 'opacity', 'name', 'uid', 'ids', 'customdata', 'meta', 'selectedpoints', 'hoverinfo', 'hoverlabel', 'stream', 'transforms', 'uirevision', 'x', 'x0', 'dx', 'y', 'y0', 'dy', 'stackgroup', 'orientation', 'groupnorm', 'stackgaps', 'text', 'texttemplate', 'hovertext', 'mode', 'hoveron', 'hovertemplate', 'line', 'connectgaps', 'cliponaxis', 'fill', 'fillcolor', 'marker', 'selected', 'unselected', 'textposition', 'textfont', 'r', 't', 'error_x', 'error_y', 'xcalendar', 'ycalendar', 'xaxis', 'yaxis', 'idssrc', 'customdatasrc', 'metasrc', 'hoverinfosrc', 'xsrc', 'ysrc', 'textsrc', 'texttemplatesrc', 'hovertextsrc', 'hovertemplatesrc', 'textpositionsrc', 'rsrc', 'tsrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

No comments:

Post a Comment

Web Scraping Tutorial 4- Getting the busy information data from Popular time page from Google

Popular Times Popular Times In this blog we will try to scrape the ...