#datavisualization #rprogramming #barcharts #ggplot2 #barplot
Hello friends welcome to this session of our programming in this session we are going to learn how to plot bar plots which are similar to those you see in generality here is a plot from a general article i will give you the citation of this paper in description this bar plot is similar to that which i plotted using ggplot2 in r studio this bar plots require much
Customization to make those similar to those in the journal by default we actually don’t get bar plot similar to this and that customization only we are going to learn in this session before starting we’ll know about our data and this data contains a two data sets one is with this antibiotics as one column miller unit as other column and standard deviation as the
Next column and the other data set again contain antibiotic combinations and then relative fluorescence is there as the second column and standard deviation as the third column and this data set is in xlsx format before plotting we need to import our data into r and for that i will set my working directory using set wd function inverted code and here i have to put the
Path i will go to the directory where my data file is stored here and this is the path i will copy that path and bring that to set working directory function windows recognize this single backslash but r cannot therefore i will make that double backslash i will run that working directory has been set and you can see that here bar graphs as you see in publications
And now i have to import my data and my data is in excel sx format as we have seen earlier and to read xlsx file i need to call the library read excel read excel is a special package which provide functions to read xlsx file and this read excel function comes from the library read excel and if you don’t have this red excel package go to install package type read
Excel here and click install package will get installed i will run this library and now i will import the data in data frame df1 that function is read underscore excel sx the name of the data file required that is data dot excel sx and we’ll run that and we’ll see how the data has got imported i don’t want all that data to get imported at once i just want the
First data set only and for that i have to specify the range we’ll see the range in excel file at a1 to c6 is the range for the first data set i will specify that range here in red xl function using range argument in inverted code a1 to c6 is the range i will run that i will confirm whether data has got imported correctly yeah data has got imported correctly now
We are ready to plot the bar plot for that we will require to call library ggplot2 and this digi plot 2 library provide functions to plot our data and run that if you have not installed g plot 2 again you can go to tools package install packages type ggplot to here and click install a library with a package will get installed i’ll run i will run that i will call
The function ggplot and this plot function takes argument data first that is df1 then aesthetic mapping aes and there x and y axis we have to specify x axis will be a b and y axis will be miller’s unit i will copy these two and bring that here i copy because i don’t want to make spelling mistakes otherwise errors will get introduced now with this i will run that
We will not get the plot because we have not specified the jio i’ll specify the geom here plus geo call and we’ll run that now we have the plot but these bars are not arranged as we want those to be this arrangement which shall be like this a chloroform first polymax in the second ncr335 second but gg plot 2 has arranged those alphabetically this is because we
Have not specified in what level those should be displayed and that will do by converting that vector df scalar a b to factor vector using that factor function factor and df scalar a b we want to convert and levels we want to specify levels is equal to and this level shall be similar to the values which are there in dx scalar a b we’ll run that and these values
Are there we’ll copy those ctrl c i’ll bring that here will remove the unwanted things removed put this inverted code shall be there put comma between those values that c function is also required here c and here that bracket i have to complete and we’ll run that again we’ll run this gg plot and here the bars are in the order in which we want those to be now
We’ll add error bars here using geom error bar function zoom error bar and this function takes the first argument aesthetics mapping data is here this data will be used and in aesthetic mapping we require y min for lower limit of the error bar and that y mean will be this miller unit minus sd and y max will be again miller unit plus st will run that and error
Bars are there but this width is quite large we’ll customize those error bars a little and extra other customization will do later on and here that width is equal to 0.2 and size i want those to be little thicker where precise will be one okay this is the basic plot now we are going to customize this to make it as that we have seen in the journal and for that first
Thing those bars were in grayscale we’ll specify the color here and that field is equal to a b we’ll run that and here different colors are there and this is not grayscale for that will require to use scale underscore fill underscore gray when we run that you can see the grayscale uh now labels are also required for labels will again put here argument label is
Equal to now this time that argument will be miller unit and those labels will be miller unit i will put the plus again and we’ll put the geom text so that labels will get printed on the plot we’ll run that now labels are there on the block now these labels are overlapping the plots because the height is the miller unit now we’ll go little customize those using
V just is equal to minus 1.5 will go for and i will run that those uh data values are there and here also data value is there but visible here we’ll customize it little more by changing this theme this gray theme is not suitable the theme which was there in the journal was theme class c theme classic a very popular theme it is and we’ll learn that bar plots came
Quite better but here it is not starting from zero we’ll force it to start from zero plus using scale y underscore continuous this one and expand will be the argument is equal to and this c 0 0 so that those bars will start from the zero and here those bars have started from zero now another thing this label has got beyond the plot limit and to make that we’ll
Again add plus and their expand limits expand limits is the argument y is equal to c 0 to 150 we will make that limit so that those labels will be within the range and now further customization we have to customize that legion shall not be there because it’s a redundant one that value is there on the x axis this axis level shall not be there miller unit shall be
There on the y-axis will customize plus theme legend dot position is equal to none religion will not be there now legend is not there these labels are quite near to each other we’ll make those at an angle that is axis dot takes and only x axis label we want to have an angle element underscore text we want to modify angle is equal to 90 we’ll run that those labels
Are there we want to change the size of those text elements on the text elements which are there in the plot therefore i will use the argument text is equal to again element underscore takes size is equal to 14 phase is equal to bold i want all to be bold color here on x-axis color is gray i want that color to be black one for all the text elements we’ll run that
Now all the text elements are like as we want those to be then this x axis text is quite away from the x axis that will change using this axis dot text dot x argument after the angle we’ll put v just is equal to 0.2 and it just is equal to 0.9 and we’ll run that those labels are near to the axis x this is a fair amount of customization we will just go there and
Change the labs now label labs x is equal to people don’t want anything and y is equal to miller units percentage and we will run that this plot is sufficient to be published we’ll use this entire code to get the another plot and see how rapidly we will get the another plot now because we have the code in our hand the another data i will label as the f2 this i
Am using from the range that range will be a8 to ac c14 that range is a8 to c14 i’ll put that range here a8 to c14 will run that and we’ll see what is there in the data frame and a b the second axis numeric axis is a relative fluorescence i will copy this because instead of millers unit we require this relative fluorescence in the place of miller unit we’ll paste
That relative fluorescence here also we press that here also we’ll paste that and our label is also complete we’ll remove the stick marks from the label this df1 will change with df2 everywhere here also i will make that df2 here also i will make that df2 and um another change where required this factor levels we need to change and we’ll see what those factor
Levels we need to enter these many different factors are there we will just copy those factors those variables and i will put that here i’ll remove the things which i don’t want and put comma here and we’ll bring this value these brackets above run that here the limits is issue because maximum value is 0.53 therefore the in this expand limits i will change this
150 to 0.8 which is near to 0.53 and we’ll run that now our plots are there but in the second plot we don’t require these labels therefore i will remove this geom text and we’ll remove that label argument also from the digiplot function we’ll run that labels are not there now in the second plot if you see the graph and in journal you you can see this horizontal
Line there that horizontal line i will plot here using the geom h line function 0 h line all caps i have to remove geom h line and in the bracket i’ll put there y intercept and that y intercept will be 2.5 then line type i want that line to be dotted and this 3 is the dotted line type with size of the line not width size is equal to 2 i will prefer then color
Of the line color is equal to a little gray gray 40 i’ll prefer that which is similar to that we see in the uh plot one the line is there 2.5 it is not 2.5 it is 0.25 we’ll run that again and the line is there where we want it to be okay this is the plot which is similar to the plot we see in the journal but if you see the error bars those error bars are half
Error bars and to get half error bars we have to find out the half error bar code from google i’ll go there and i will again i will go to google i will type yeah it’s getting plotted half error bars in ggplot2 and i will run that half error bar is there and geom error but this is coming from joe and gg annotate package i will go to the package index to get the
Code for its installation i’ll come here i’ll just copy the installation lines these two bring that to r at the end put that installation code we’ll run this geom so that package gets installed install dot packages remotes and package is getting installed then i will go for the install github as well dj annotate package it is getting installed like the package is
Getting compiled package has got installed now i will call library gg annotate and will run that line now here in geom error bar i will make it half this y mean i will remove because y mean is not required in the from first plot also we will remove the will make it half error bar of error bar and this y mean will remove will run the first plot error bars are red
Change the color by specifying color there color is equal to black next code also in geom error bar i will make color is equal to black will run this error bars are black here also in first plot error bars are blank half error bars are there now these two plots we want to arrange therefore we first save these plots this is p2 with a name run that the first
Plot is p1 with name p1 we will run that we arrange those vertically and for that arrangement we’ll require library patchwork we’ll run the library batch work we’ll arrange the plots now as p plot p1 upon p2 1 below other run that and we’ll see how those plots are getting arranged now those plots are plots are getting arrange vertically above each other we want
Those plots to be labeled with alphabets a and b and for that with this p i will put the layer plot annotate plot annotation and their tag levels is the argument and i will put a in inverted code there in this plots a and b is here and this plot if we compare with the plot in german you can see that both are similar practice this to make your data appealing to
Your audience thanks for watching this video if you like my video please click like button share this video with your friends and if you are not subscribed to my channel please subscribe it please press bell icon to get notifications of my new videos thanks thanks for watching
Transcribed from video
How to plot barplots similar to those in journal articles using R and ggplot2 and other packages By Rajendra Choure