- Greetings.
Welcome to a tutorial on how to 
produce graphs in RStudio.
So we're going to go through 
the basics here.
We'll start off in the Console,
and then we'll open up in our 
Markdown file
so that we can actually save 
our work.
And then we'll get 
a little more advanced
after we've entered 
our own data,
and we'll show 
how to import data,
and even more importantly, 
how to export our result,
our graphs.
Let's say you wanted to take 
a graph
and put it into a Word document 
or something.
Okay, so let's go get over 
to RStudio here.
Let's start off real quick 
with the basics.
How do I input data?
Well, you got to give a name, 
a place to put the data in,
so let's make it easy.
We'll call it X.
So we're going to say X =.
Now many people use 
the left assign character here
in place of the equals,
and you'll find as you get 
better at R,
they're going to push you 
in that direction.
But you know what, 
for the mere mortals of us,
equals works just fine.
So we're going to stick 
with equals, okay?
So now I need to use C for--
think about a C for Column.
If you remember some of our 
other work that we've done,
we've worked with data frames 
or data matrices,
and the variables showed up 
in the columns,
and so think about C for Column.
So say you went around 
your campus,
and you randomly asked people 
what their ages were.
And the first person 
was 19 years of age
and then there was a 21 year old
and then there was a 19 year old 
again and then a 22
and then a 34.
And you notice as I'm typing 
these
that some of them I put 
the spaces in
and some of them I don't.
It doesn't care whether you 
put the spaces in or not.
In some places, like when you're 
typing things in a paper,
you want the spaces delineated,
but you don't have to have them 
in there.
And then let's say you saw 
another 21 year old,
and last but not least there was 
an AR student,
so you saw a 16 year old.
Okay? All right, 
so you hit an Enter, and voila.
If you take a look over here 
in values now,
you'll see you have created 
a variable,
and it tells us 
this is a numeric variable
with seven entries in it.
And if you hover over it,
it will give you kind of 
the same thing
that it's seeing there.
Now, what if I wanted to see 
or echo my variable X
back out to the console so I can 
see it in the console?
We just say hey, show me X.
I hit an Enter, 
and it will replay it for me.
What if I realized ooh, 
I made a mistake?
That wasn't supposed to be 
a 16 year old on the end.
It was supposed to be 
a 17 year old.
Well, you just hit the up arrow
until you get back to where you 
actually entered that command
and just go in and change 
that number, hit an Enter,
and now watch.
When I echo X, 
it will be updated.
Okay, so that's a handy feature.
Don't forget the up and down 
arrows
for scrolling through previous 
commands that you've been using.
All right, so now that I've 
entered in my own data,
let's say I want to get 
a summary of those numbers.
Well now, summary(X) will work 
just fine, okay,
because I've stored my numbers 
in X.
If I want to see a histogram 
of his data, I go hist(X).
It's not going to be a very good 
histogram
because there is not very many 
numbers in there,
but you get the idea.
I can go boxplot(X), 
and sure enough--
whoops, I have to put the "B" in 
there, boxplot(X), there we go--
and there is the box plot 
of my data.
Okay, now that's cool,
but notice that working 
in the console
is kind of like working on 
the screen of your calculator.
These numbers aren't really 
very portable.
I mean, it's not like I can take 
these numbers
and give them to somebody else.
So what we want to do 
is go ahead and come in here
and open a R Markdown file,
and when I open a R Markdown 
file,
I'll be able to share my results 
with other folks if I want to,
in particular, your teacher, 
by having a document
that I can turn into them.
Now, as usual, we're going 
to get rid
of just all of this opening 
stuff here,
and remember that when you just 
type in Text,
when you go to knit this,
it's going to give it back 
as just text.
So this is a test file 
for graphing.
And now if I want to actually 
go in and put some R code,
stuff that I would have entered 
down here in the console,
I need to insert a chunk.
So I'm going to go to Chunks, 
I'm going to go to Insert Chunk,
and notice I get the tick marks 
with the R
and then closing tick marks.
This grayed-out area right here 
is all R code,
so I can come in here 
and I can say okay,
X = C and then I can put 
in some numbers.
I'm going to kind of shorthand 
this,
so I can get a bunch of numbers 
real quick.
I'm going to go ahead and go oh, 
3 to 25.
Okay, and if I run that, 
you'll see what that does.
Now, if I come down here 
and I just say, show me X,
notice it gives me numbers 
ranging between 3 and 25.
That's what the colon does,
but you can enter in any 
of your own numbers there.
Play around with it a bit, 
right?
Okay, now if I want to see 
a histogram of those numbers,
I can just type in hist(X), 
okay?
Whoops, my bad.
I didn't want to hit 
a return there.
I wanted to hit an Enter, 
and then I want to run it.
So let's run that current chunk, 
and we get the following code.
Okay.
All right, so as we will see 
as we work our way
through the book,
there's ways of getting 
interesting numbers.
And rnorm is one way of getting 
interesting numbers.
That's random normal.
It's random from the standard 
normal distribution, okay.
So if I run that chunk, I get a 
much more interesting histogram
because what this command told 
me to do right here
was store 100 numbers into X.
Those 100 numbers come from the 
standard normal distribution,
the model where N, a mean of 0, 
and standard deviation of 1.
Okay, and you can ask 
for a box plot of those
by saying boxplot(X).
Let's come back up here 
and run that chunk.
Remember, running a chunk simply 
just pastes the commands
down into the console for you 
and actually runs them.
Now, notice I'm not seeing the 
histogram.
Well, let's notice there's these 
arrows here
that let you scroll through all 
of the previous stuff
that you made.
Now, something I've noticed
is that the graph on the second 
time that I ran it is different
than the graph 
for the first time.
In fact, it will be different 
every time I run this
because it's grabbing a new 
random selection of 100 numbers.
Okay, so let's go in and show a 
couple more interesting things
that we'd like to do 
with these commands.
In particular, maybe I would 
like to get a little more,
you know, descriptive 
than Histogram of X,
and maybe I want to put 
something besides X
on the X-axis
and frequency on the Y-axis.
Okay, and maybe I decide to make 
this into relative frequency
or something like that.
Anyway, back over here 
to the hist command,
in all of the plot commands 
in R,
there are ancillary commands 
that you can add to it
that will allow you to do things 
like change the X label
for the X-axis,
the Y label for the Y-axis,
put a title on there which will 
be your main title up here
where it says Histogram of X 
on our current graph, right,
that's our title up there--and 
also to change the color of it.
Now, it does not matter 
which order you put them in
because R will just interpret 
them
however you give them to them,
so let's start off 
with the X label.
So X label is xlab.
So if you type in xlab, 
that's for X label,
and now I can give it 
a new name there.
Names are categorical, so they 
have to go into quotations.
Anything that's categorical 
goes into quotations.
Anything that's numeric, 
it does not.
That's pretty much hard and fast 
throughout R.
Okay, so xlab, 
let's put in there Z
because we know that that's 
the Z-axis.
Okay, and ylab, that is--
well, it already says frequency,
but let's put it in there anyway 
just so you can see.
We'll call it "Freq."
We'll just abbreviate it
so that you can see it 
actually does something there.
And then the main which is the 
header or the title up there,
and in the main we'll say 
"Random Values."
We'll just put 100,
"100 Random Values 
from the Standard Normal."
Okay.
I'm going to get rid of this 
bloxplot command for the moment.
We'll come back to that 
in just a second.
If I go in and I run that,
you notice that part of it is 
kind of cut off. That's okay.
If I click on Zoom, notice it 
will fill in the entire thing
because that's just a little 
preview window down there,
and I'm a little happier 
with it.
I like that. That's better.
Okay. But what if I wanted my 
bars to have some color to them?
Well, one last thing we can add 
to this command is col,
C-O-L for color,
and let's go ahead and make ours 
"blue."
Okay, so if I run that chunk 
this time--
notice every time I'm going to 
get a slightly different graph
because it's getting a new 100 
numbers--
I end up with the following 
graph.
Now, let's take a look at it one 
more time doing Zoom
to preview it.
Loving it, beautiful.
It's just so gorgeous.
So now that I like that graph, 
I want to export that
so I can put that into my 
Microsoft Word document,
let's say.
So you see where it says Export?
I click on that, click on save 
as image,
and I have a choice of PNG, 
JPEG, TIF, et cetera.
PNG is probably your best choice 
in most cases.
It should go directly 
to your working directory,
wherever you've been saving 
all of your R work
is already going to go there,
but you do need to give it 
a name.
And so this is TestGraph 
is what we'll call it,
and I don't want to put a space 
in my name there so TestGraph.
So I hit an Enter,
and when I go in and I navigate 
to my R folder,
I'm going to find that 
particular file in there.
Okay, so that's some pretty cool 
stuff.
It lets you use R to make some 
pretty amazing graphics
and then go in and actually 
export them.
Okay, so I think that's enough 
to get you started.
I would be happy to take 
questions on any of this.
Or if you'd like additional help
with any of the graphics 
parameters,
please let me know, and I'll be 
happy to help you with those.
And I obviously went really 
quick here,
but you can watch this as many 
times as you want.
