Hi! I am Mike Marin and in this video we'll talk about producing "bar charts" and "pie charts"
using R. bar charts and pie charts
are appropriate for summarizing the distribution of a categorical variable.
we will be working with the lung capacity data
that was introduced earlier in this series of videos. I've already gone ahead
and imported the data into R and attached it. first
let's discuss the "bar chart": this can be produced
using the "barplot" command. you can access the Help menu
by typing "help" and in brackets inserting the name of the command you'd like help for
or by simply throwing a question mark (?) in front of the name of the command
before producing the bar chart
let's remind ourselves that a bar chart is a visual display
of the frequency for each category of a categorical variable
or the relative frequency or percentage for each category
the frequency table
can be produced using the "table" command in R
let's go ahead and make the frequency table
for the variable Gender;
here we can see that we have 358 females
and 367 males in our sample. let's go ahead
and store this in an object called "count" for use later
we can see the table
is now stored in this object "count". we may also wish to express the bar plot
using relative frequencies or percentages (%). so let's go ahead and
calculate these as well
we can see that there are 725
observations in our dataset; we can divide the table
by 725 to have these expressed as percentages (%)
or relative frequencies; we can see
that our sample consists of about 49.4 percent females
50.6 percent males; let's go ahead
and save this in an object called "percent" for use later
now, we can produce the bar chart using the "barplot" command
let's produce it first for our "counts"
now, let's take a look at this same plot but using the percentages (%)
or relative frequencies.
we can add a title to this plot
using the "main" argument within the plot command
we can also label the
x-axis using the "xlab" argument as well as the y-axis
using the "ylab" argument.
I also like to rotate
the values on the y-axis using the "las" argument and setting this
equal to 1
to change the names or labels appearing under each of the bars
we can use the "names.arg" argument
here we can insert the names we'd like to appear
under each of the bars. We'll go ahead and use "female"
and "male" as the labels.
if you would like the bars to appear horizontally rather than vertically
this can be achieved using the "horiz" argument and setting this
equal to TRUE. we should also make sure to change
our x and y label as these are now going to
appear on opposite axes.
to learn more about changing colors (colours)
or other things on this plot explore the Help menu in R.
we can produce a "pie chart"
using the "pie" command.
as before, we can add a title using the "main" argument,
if desired, one can also add a box around this plot
using the "box" command.
to learn more about changing
colors (colours) and other things for this plot explore the Help menu
thanks for watching this video and make sure to check out my other instructional videos