Plotting interactive visualizations with Plotly in R

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey hello everyone and welcome to our may we're now in may um nhsr webinar and we're really pleased to be joined by both um elizabeth brown and hannah alexander who are going to be speaking with us about um plotting interactive visualizations with plotly in art so thank you both for joining us um just so everyone's aware we are recording all our webinars and um you'll be able to access both those and any materials that are used afterwards through our website nhsrcommunity.com and we usually make them available on youtube as well the webinar should last for around an hour and um do please post your questions in the q a box and hannah elizabeth will be answering them um as and when throughout and whilst we're here we'll just mention we also have our slack channel um if you have any further questions after today you can join that and we have some very active members on there who'll be happy to help you out and looking forward to next month we're planning to host another webinar on the 16th at 1pm with zoe turner who'll be speaking to us about github so do register for that if that's something you're interested in um at the end of the webinar we'll just be gathering some feedback and michael jane who's also on the line we'll be sharing that in the chat box so um any comments would be really useful so without delay i'll hand over to elizabeth hi everyone so this is a webinar about using plotly in r so plotly is an open source graphing library which allows us to create interactive plots and charts we have a our package called plotly which accesses this library through r there is also a similar package in python and as well as plotly allowing us to create interactive plots it also has easy integration with ggplot2 as well as dash and shiny there is a website you can see here that has a lot of different examples of different graphs that you can create in different charts as well as different custom controls and animations i will send a link to that at the end after the webinar so you'll be able to access that so we can install plotly in our studio um through crown by running install.packages plotly you can also download the development version from github by running devtools install github via that and we can library in the package running library plotly i'm also going to library in the tidy verse as i will use this later on so first things first is let's actually look at what an interactive plot and plotly looks like so i'm going to take the empty cards data set and using the pipe operator i'm going to pipe it into the plotly function called dot underscore l y so plotly i can give this an x variable so for the x coordinate i'm going to plot the mpg and for the y coordinate the y axis even the displacement variable so running this we can first note that the chart appears in the viewer tab as opposed to the plots tab we can click zoom to enlarge the chart and see that we have this scatter graph of mpg against displacement there's a toolbar at the top right which has a number of options this is how we interact with the plot so we first we can move up and down the y-axis and move across the x-axis like so we can also hover over points and see the exact coordinate for that point we can also zoom in and out we can pan across the point the plot like so and we can also use either a box select or lasso select to select certain points that we we would like to look at so now zooming out the other points are sort of grayed out as such so lighter color so this is the basic reactivity and interactivity of the chart and this interactive at least stays the same with all of the charts created implantly now we can actually look up the code this to the structure of the of creating a plot in plotly so there are three functions that we can use the first one is this plotly function this function takes our objects and maps them to the plotly graphing library there is the layout function so this function allows us to specify attributes of the x and y axes and um sort of chart title so we can give axes labels and set the range for axes the scales etc and then there are functions that create traces so traces are similar to geometries in ggplot2 and these are essentially the type of plot that we would like to look at so here we had a scatter plot so the scatter trace is one type of trace if i run this piece of code this shows all of the different functions that plotly provides to add traces so there's the add trace function which is a general function to add any trace so in this function we could specify that we wanted a scatter plot or a box plot but there are also more specific functions so we have add underscore box plot which would produce a box plot we have add underscore histogram add underscore heat map for example so there are many different types so we'll look at an example of this layout function if we take the empty cars data set as before and pipe it into the plotly function and again give the x and y variables notice that here we've used a tilde symbol this is necessary um for plotly we can then pipe this into the layout function so here we can set a chart title and some attributes of the axes so first we can use the argument title and give this the title of our chart next we can use the argument x axes this is where we specify attributes of the x-axis so this takes a list and we can give the x-axis a label so title equals miles per gallon we can also set a range for the x-axis so by range this means the initial range of the axis when the chart is produced with the interactivity this range can change you can manipulate um the chart so this range is the initial range that's shown when creating the chart like i can say that the range is going to be between 10 and 35 really i can do pretty much the same for the y-axis using the y-axis argument so again this this takes a list i can give the y-axis a label displacement i can set a range so here the range is between 50 and 500. so running this and enlarging it we get the same scatter plot as before but now you can see we have our x and y axis labels we have a chart title and also notice the initial ranges here this the y axis is initially between 50 to 500 and x axis 10 to 35. you may have noticed that in both examples shown so far we haven't actually used any of these ad trace functions in the console you can see there's two messages here so the first message says that there was no trace type specified so the scatter trace seemed appropriate and also there was no scatter mode specified so they chose markers this function here the plotly function is very clever in the sense that if you don't provide any address functions then it will work out a an appropriate trace to add so here it decided that a scatter trace would be appropriate so we can use this plotly function to create traces as well as the add trace function so let's have a look at an example so if we take the mtcas data set and pipe it into the plotly function here we'll go along the x-axis we're going to have the variable cylinder here this factor function turns this variable into a categorical variable so we are just plotting four six and eight which are the three categories for that variable the y-axis we're going to plot the mpg and i can specify that i would like the trace to be of type scatter i can also use the name argument to specify a name which will come clear in a moment i can then pipe this into the add underscore boxplot function so this function will overlay a box plot trace onto the chart so i can give this box plot i don't need to specify the x x and y variables again because they've already been specified in this property function so if i run this i get this chart so this box plot is interactive so when i hover over it i can see the max and the min median and the quartiles and i can also do everything that i could before zoom in select areas notice that in the top right hand corner we have this scatter and box plot these correspond to the name argument that we provided here so the name is scatter and box plot if i click scatter this removes the scatter plot on the chart and similarly if i remove the box plot this removes the box plot from the chart so now we can just see the points this also reinforces the idea that each trace overlays on top of each other so we can overlay many different traces another example of using the plotly function to specify a trace as opposed to using an actual address function is as follows so we can take the air quality data set type this into the function plotly here i'm going to specify that my x variable is going to be the temperature variable and instead of using plotly to specify the trace i'm actually going to use the address function so here i can say that the my y variable is the ozone and i can set the type of trace to be scatter i can then also pipe this into the layout function that i showed before to specify the title for the chart and x and y axis labels so here we have the x-axis label is temperature and the y-axis label is ozone so running this we get a scatter plot it's interactive not that i can although before i was zooming in i can still do all of the interactivity of the chart in within the viewer if i copy and paste the same code but instead i'm going to get rid of this add trace function and specify the trace within the plotly function so here i can say that x is my temp value y is the ozone and the type of trace is going to be a scatter running this i get exactly the same chart so partly is very quite flexible and how you want to write the code to create the the chart so you could either specifically use the add trace function or use plotly itself to add a trace as well as using a number of different specific functions like add underscore box plot and add underscore histogram are there any questions before i move on to the next part there's been a few but i've been answering them they're not too tricky there was one that i wasn't sure that i thought i might ask you someone's asked um do you have any advice on a nice plotly chart to powerpoint workflow in which the charts come through as vectors and text rather than bitmaps with undistorted labels oh i'm not sure yeah i wasn't i've started to have a google but i personally don't know i wasn't if he did i'm not sure i'd have to do some further digging around to work that out to find out but the um the website is has a lot of thorough documentation so first port of call would tip would be to go to the website which i i'll send put a link as a reply to that question for now if that helps sounds good um also will asked um if you'd had um had to do anything with um so i have been asked to do any r into power bi i said i hadn't and i assumed you haven't but i wasn't sure no i haven't either unfortunately new problem oh we've got another one from i think maybe a different will um have you um um are there any limitations of putting this into our markdown um are there any good specific visualizations that plotly has that ggplot doesn't and vice versa so um plotly has which we'll see um soon probably has a lot of um can achieve plotting with geospatial data really well and um and has a lot of sort of different functions that allow you to do this easily with a lot of different customizations and um sort of other things to do with it and also partly has really good integration with ggplot2 which we'll see later as well and i would assume that that prod plotly probably does have um good integration with our markdown and that's purely based off its good integration with a lot of other things like shiny and dash and gigi plot two i would have to look further into it but i feel like yeah i i've used a reasonable amount in our markdown and i don't i've come across any limitations it i think it works pretty much exactly the same as uh not in our markdown and um it's quite good to use it like within dashboards using our markdown and yeah i don't think there's any limitations is there a way to insert an interactive property plot into powerpoint i'm not sure is my honest answer um i've never tried it before hannah do you have any sorry what was that question i was just refined to another one oh sorry at the um peter higgins it's in the the new the new ones is there a way to insert an interactive plot you put into powerpoint um i will have a google i don't i haven't seen any um because i think with partly you have to be connected to the web to be able to for them to stay interactive um so i think you could potentially do again like a dashboard or an i an i o presentation um but yeah i'll look now for you but i i've not seen an interactive one in a powerpoint per se because of that exact reason like i think you need to be connected to the internet for plotly to work yeah um if we save this file as a jpeg how can we make it interactive i'm not sure if you can make it interactive once you've saved it but you could so you can download the um the chart as an image so you can do it through here as well you can download the plot as a png but the other way around i'm not don't think would be possible purely because of the internet connection so this so this code is linking to the online library so the this scrapping library here um so yeah i don't think that would be possible but if you still have the code that created the plot you can just sort of use that i think that's all the questions oh look there's a way to insert html here okay shall i i'll continue then yeah you crack on thank you cool okay so the next thing i wanted to show you was custom controls so we can add a range of custom controls to our plots so these are like buttons drop down drop down selections and sliders so i'm going to show an example of a range slider so this allows us to control the x-axis in this case first i'm going to create a vector called x which is going to range from minus 2 pi to 2 pi in very small increments then i'm going to create a data frame okay x to be this x vector y variable which is going to be sine of x and the z variable which is going to be cosine of x the goal is to create a plot of the sine function and the cosine function so i can take this data frame function and set the x axis to be the x variables so these points from minus to pi to 2 pi then first going to use the add lines trace to add the curve of the sine graph i use add underscore lines and set the y-axis to be the y variable in the data frame and then i can also give this trace a name call it sign i can then again pipe this into add lines and this time my y variable is actually going to be the cosine of x so z and i can give this trace a name cosine then i'm going to pipe it into the layout function i can give the chart a title and then i can define attributes of the x-axis this is where i'm going to specify that i would like a range slider i use the argument range slider and this is equal to a list of type equals x so what this does is add a range slider to the x-axis which controls the x-axis so running this code we get this chart so here we have two curves one curve of the sine sine graph and the other cosine in the top right i can click sign to remove the sine curve and just see the cosine and similarly the other way around and on the x-axis we have this range slider say i want to hone in on this part of the graph so here i can use this range slider to hone in on this part like so so now here i can see that this part of the graph has been zoomed in if i wanted to take this x-axis range i can then slide this range along the whole graph like so and i can change it and slide it along so this is one custom control there are many different custom controls and on the website there are examples of buttons and different types of controls so as i mentioned previously um plotly has properly provides a lot of functionality for plotting geospatial data so we can take a um a data set firstly to actually get some geospatial data so this data um is from the plot lead data sets github so this this data set comes along with some other sample data sets which can be which are free to use and specifically supplied from plotly and this data set is about the precipitation levels in the usa in june 2015. so running ahead of the data frame we can see that we have a one column called lat and one column called lon these are the latitude and longitude coordinates of each location and then we also have this glob value this this is the value associated to the amount of precipitation so what we're going to do is plot for each location the precipitation level on a map so plotting geospatial data impliedly follows the same structure as as any other normal plot with a special function called plot underscore gu this works in the same way as the plot plot underscore a y function but it's specific for specific to latitude and longitude coordinates so if we take our data frame and pipe it into plot underscore geo and then we can set the lat to be the latitude column i'll choose to be our longitude column here simply running this we get this plot consume so here we have the world map and the locations plotted on the map when we hover over the points we can see the specific longitude and latitude coordinates we can also zoom in on this map like we could do before notice that this doesn't actually have any information about the precipitation this is purely just a scatter plot of the locations on a map so we can add the precipitation values on this map by using a trace function called add underscore markers if we take this previous fig object so this the map that we just created and then pipe this into the add markers function use the variable text to add some information to the hover text here so what previously when we've hovered we just see the latitude and longitude coordinates say now we want to see the precipitation for each point so i can say about text equals paste and i'm going to paste the word precipitation with the glob value which is the value associated to the precipitation level now running this when i hover over a point not only do i see the latitude and longitude coordinate but i also see the precipitation level for that specific point like so the add markers function also has a lot of other arguments for further customization of the points so if we take our data frame again and pipe it into plot underscore gu with our latitude and longitude variables and then pipe this into the add markers function set our text to be the same as before so we want the to see the precipitation levels for each of the point points we can also change the color of these points using the color argument and here i can actually give it the variable glob value what this will do is create a color bar and assign a color to the the value of the precipitation so then we can see the areas where precipitation was higher precipitation was low use the symbol argument to change the actual shape of the point make the points a square i can also change the size there is an argument called hover info and this specifically refers to not only the text provided in this label but also the coordinate so say now we don't actually care about the specific latitude and longitude coordinate all we want to see is the precipitation level we can set this hover info argument to equal text and this text here refers to the text that we provided so now it will only show the precipitation level on the label instead of the coordinate as well finally i can use the opacity argument so how sort of see-through the point is running this we get this chart so here again we've got the world map with our points hovering over we now only see the precipitation so we don't see the coordinates anymore we have this color bar here that represents that glob value and we can see sort of the areas of higher precipitation and lower precipitation also if we zoom in to the points closer you can see that the points are now square and they're slightly opaque now we might want to give the color bar a title instead of grab value it's not very meaningful and also the chart title at the charter title so we can take our plot so fig and pipe it into the color bar function so this allows us to change any of the attributes of the color bar so give it a title the limits of the bar so here i can set a title using the title argument and call it mission june 2015. i can then pipe this into the layout function again to assign the chart a title so running this we get the same chart as before but now we have a new title for the color bar and a title for the chart we can go even further with the customization the layout function has an argument called geo and that we can supply this with a big number of arguments so in this website specifically navigate to this point this talks about the layout and the layout arguments for different traces and there as you can see there are many different types of arguments so there are many different ways we can customize different functions so for this i'm actually going to copy some customization i wrote earlier and then explain the parts so j is going to be a list and first we can set the scope to be the usa so since the precipitation data is only for the usa we don't need a world map we can just look at the usa we can set the projection so this is the specific map that we want to use to see the points in the usa we can then do other customization to fit them up like so so these arguments here set the color of the land and the colour of the the borders of the states as well as the the width of those borders so now if i take the figure from before and pipe it into the layout function i can use the geo argument and give this this list of different customizations so running this we get this chart so now all the points are just on the map of the usa which it's a bit more sensible and we have our color bar we have the precipitation labeled for each point and our points are square slightly opaque and we have a chart title so this is one type of um plot of geospatial data that we can do with poppy this is a scatter plot but flatly has functionality for chloroplasts heat maps very many different types of plots so does anyone have any are there any questions before i move on i know that haven't been answered there's only one that i wasn't sure about and oh no they keep being in the new and then they get published and i lose them um what about if can you if you created a plot using ggplotly um and then you want to use partly plot underscore l y excel the functionality from that is their way of doing that as in the arguments from plot underscore l y yeah i think they have made um like something interact like an interactive graph using the ggplotly function but um i think specifically they said they really like like the slider thing that you used using plot underscore why um but i i mean i've had a google i can't i don't think you can but i don't want to say you can't if you know i'm not sure because so the next thing that i was going to go and do is that ggplotly function and i can give it a go while i go through and see what happens i think is a good good way forward shall i continue is that yeah is that everything okay cool so yeah so the next so the next thing is um that plotly has really good integration with ggplot2 so we can have a look at a couple of different examples so i can just create a dataframe this contains one variable which is just random samples from the standard normal distribution i can then take this data frame and pipe it into ggplot so we're not looking at plotly right now we're just looking at ggplot2 so i can type this into ggplot and um assign my mappings in ggplot we use the addition instead of the pipe so that's the subtle difference between plotley and ggplot2 i'm then going to plot a histogram of the points so i can specify the clarity that i'm plotting the density and i want my bars to be to have gray outlines so this color here actually talks about the outline of the bars as opposed to the fill argument which specifies the color of the the inside of the bar then over the top of this i can plot the density in gg plot 2 we use the alpha argument for the opacity as opposed to in partly where the argument was called opacity so running this we get a normal jg plot it's appeared in our plots tab as opposed to the viewer tab uh it's not interactive it's just a classic plot we can turn this plot into an interactive plot using the function ggplotly two ways of doing this so by default this function will take the last plot that was produced and make it interactive or we can specify that we want to make the plot p interactive so either we'll work here so running this we now get the same plot that opens up in the viewer i think that changed slightly because of the samples oh no never mind um yeah so this it's taking this object and making it interactive so now it's opened up in the viewer and we can hover over the bars and see the density for the x-coordinate we can also zoom in and select areas so we can select some bars here and we can zoom in just like we could before so it's really nice if you've already got some gg plot codes you've already got a plot and you'd just like it to quickly make it interactive you can just use this function another example so car underscore copy this is going to be the empty cars data set but with the cylinder variable removed we can then make a plot so i'm going to actually start here with the ggplot function and not give it any specify any of the arguments i'm then going to use the g on point function and specifically supply the data here and i'm going to give it this car copy so that so this geometry is going to plot points for data that doesn't involve the cylinder variable so i can set the aesthetics to be the mpg against displacement i can also set the color to be light gray i can then use the g on point function again and instead i'm going to plot the whole of the empty cars data set and specify the same mappings then i'm going to fasten over the cylinder variable so using facet grid so this will split the points into facets depending on the type of cylinder and then i can also give this plot a title as well as some x and y labels so running this we get a normal gg plot this is an interactive here we for each cylinder we've got mpg against displacement and in light grey we have all of the points in the background i can use the ggplotly function to take this object and make it interactive so now it appears in the viewer if we take a closer look at the interactivity so we can see the coordinates for the points moving the y-axis moves the y-axis for all three of the facets but moving the x-axis for one facet only moves it for that facet so the other two stay the same zooming zooms all of the facets at once and we again can select points that we care about so answering that question before where if you you can if you want to supply some layout say a range slider or custom control via the plotly function layout let's see if this works so if i assign the output of gg plotly to an object fig now fig is this interactive plot if i then pipe this into the layout function say i'm going to change the y-axis range so i'm going to use the y-axis argument and i'm going to change the range b from 0 to 500 example let's see didn't specify let's change the range and see make it smaller yes so that worked so before it was going from 0 to like 500 but now i've specified the range in the layout function to be from 100 to 400 and now we can see that initial range so yes i think if you assign the at the output of using the ggplotly function to an object then you can use the layout function and other plotly functionality with it so as as the custom controls the range slider earlier was you was within the layout function i don't see why you couldn't integrate something like that within this as this has worked does anyone have any questions of any questions i think the rule unanswered they're all answered in the chat um the only question that i kind of only half answered was um is there a way to add comments below charts in g d plotly i found it for just plot underscore why probably um but not the gg please function it may not be um a option then inside this function i guess you could do what we've just done here where you assign it to an object and then use the plotly function on it because that would i don't see why that wouldn't work but yeah i don't think for this gg probably function there'll be an up there might not be an option to do that there's one last thing i wanted to show and i mentioned sort of right at the start that uh plotly has integration with shiny and dash and charts studio i think it's called yeah chart studio so i just wanted to show an example of integrating partly with a shiny app so this is some shiny app code which i wrote earlier demonstrate so i have the shiny app and it has some text output here so this is an interactive plot within a shiny app and here we have our plotly plotly chart this is one that i want one of the examples earlier so all of the interactivity is exactly the same as it was before and it's within this shiny app to do this uh plotly provides two functions so the plotly output function and the render plotly function so here in the ui we can give it this plotly output function to specify the the widget and then in the server we can use the render plotly to actually render our plotly chart to that widget so this here is the code used in the example earlier so running this code will simply just produce that plotly chart as before so plot b makes it really easy to integrate these plotly charts within shiny apps are there any other questions or anything no more than put in the q a cool that is that is the end of the talk so if anyone has any questions or anything then ask away the um materials will be made available afterwards later on so you'll have all of the code that was used if that is everything then thank you for um coming is it possible to have subtitles i believe so um let's have a look at the layout function and can you facet the plotly charts yes yes so fast faster than the partly charts there if you go to the um the plotly website there's a section on subplots so here that this sort of looks like similar to jg plot faster than so this will have these have different examples here of different ways to sort of facet plots i i will also be surprised if there isn't a way to do a subtitle have a look and see if there is a go to an earlier example here let's see if i can i wonder if subtitle is an argument and see if this works that did not work so i don't think that's what it's called but i would be surprised if there wasn't an argument it will it will definitely be under the documentation um i found a stack overflow answer there's actually no subtitle um attribute there's any a title one um but someone's got around it using like some annotations so i'll pop that in the chat rather than trying to say out loud the code um there we go oh there's some more um oh a lot of suddenly come through okay um um so where can we find the materials and the script you're gonna are you gonna email them after uh yeah that's um sort of works for you guys bethany um yeah no that's great thanks so much um her elizabeth there's a lot of people attend today and um lots of really good questions i think everyone found it really useful so thank you again um just a reminder um jane posted in the q a there um the little feedback forms we'd really appreciate if you can fill that out and um we have next month's webinar to look forward to with zoe turner so um thanks everyone for coming thank you you
Info
Channel: NHSR Community
Views: 2,258
Rating: undefined out of 5
Keywords:
Id: WmofiOklux8
Channel Id: undefined
Length: 57min 44sec (3464 seconds)
Published: Tue May 25 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.