Hi, I'm David Christensen, and in these lessons, I'll show you how to do data visualization in R. In this lesson, I'll show you how to use the plot function. The plot function comes in Base R and allows us to quickly visualize our data. We'll be visualizing the mtcarves dataset. To visualize data using the plot function, all we need to do is type plot, our parentheses, and then the data we want to plot. If we want to plot the mtcarves dataset, we can type mtcarves. This will plot the entire dataset. Notice that a plot has populated in the Plots tab in the bottom right -hand corner of RStudio. There are several columns in this dataset, and so it's somewhat tricky to see what has been plotted. I'm going to click on Zoom so we can have a closer look at the data visualization. That's a little bit better. We can see the columns named in the diagonal, MPG, CYL, and so on. These column names serve as our guide on the grid in order to find out which two columns are being visualized in any of the visualizations. This is a lot to look at in one visualization, and so it makes sense to reduce this to just, say, a few columns of data. Let's go ahead and do that now. So we'll go ahead and create a second plot. And this time, let's plot the first six columns of data instead of all of the columns of data. This should make it a little bit easier to manage and view. So I'll type empty cars again, and we're going to limit the data to the first six columns of empty cars. So I'll add some square brackets and put 1 colon 6, and then we'll visualize this data. Okay, we have a new plot in the Plots tab, and you'll see that this one's a bit easier to read. I'm going to zoom this one to full size so we can see it a bit closer. Here we have the full size visualization showing us the first six columns of data from the empty cars data set. This makes it a bit easier to start to explore the data and see some connections between the columns. One column that looks very interesting to me is the relationship between MPG, miles per gallon, and the weight of the vehicle. It looks like there is a relationship between the weight of the vehicle and the miles per gallon that vehicle achieves. Let's go ahead and switch back over to RStudio where we can visualize just two columns of data together. Okay, here we are back in the Script tab, and I'm going to now create another plot. And this time we're going to plot those two columns of interest, the empty cars MPG column and the empty cars weight column. We can do this by typing plot once again, and then naming the columns within the plot that we want to visualize. So within the plot function, we have empty cars, dollar sign MPG for the MPG column, comma, and then the other column we want to visualize, empty cars, dollar sign weight. Let's go ahead and run this line of code. Now we have this plot in our plot tab. It's a bit easier to see that relationship between weight and miles per gallon. We can style the plot to be more interesting or self explanatory, or to derive additional insights from the visualization. Let's go ahead and try some basic styling. We can add color to the dots that are visualized. Let's go ahead and return to the function where we plotted MPG and the weight column. And I'm going to add a comma and declare a color. So I'll use COL and I'll set this equal to a color. This could be either a single color. So in quotation marks, I'll put red. And we see that the color appears highlighted. And this lets us know what color the visualization will be when we run it. So let's go ahead and run this just to see what we get. All of the circles within the visualization have been colored red. Instead of providing a specific color, we could provide a vector. For example, we could put red blue as a vector. So I'll add C to combine red. I'll add a comma. And after red, I'm going to add blue. Let's go ahead and run this line of code again. This is interesting. Now our visualization has red and blue dots. The vector red blue has been recycled through all of the dots on the visualization. Instead of coloring this visualization randomly, it would make more sense to encode another column with color. Let's go ahead and give this a try. We know that there is a column for Cylinder. Let me go ahead and type mtCarsCylinder so we can see this column. That's mtCarsCyl. There we go. We get the column. And we see the values are it looks like we have sixes and fours and eights. Let's just make sure that's the columns we have in this data set. Let's go ahead and find the unique values from this column. I'm going to apply the unique function to mtCarsCylinder. And we see when we run the cylinder column within unique, yes, six, four, and eight are the only values that appear in that column. So what we're going to do is create a new column of mtCars. We'll call this new column Column Color. We could assign this column an empty value if we wanted just to start things off. Let's go ahead and see what this column looks like. So we now have a column called Column Color, which is blank. So let's go ahead and assign this column the colors based on the value of the cylinder column. So we'll start with our mtCarsColumnColor. And we're going to limit this to mtCars where cylinder equals four. And anywhere where mtCarsCylinder equals four, we'll set this equal to the value of our first color. And we'll use the color Coral 2. You might be wondering what colors are available and how to find their names. We can run the colors function to find all of the available color names. This prints all of the color names to the console. We can then copy and paste the color names to the script area to view them. Here I've pasted a few dozen of the available color names to the console. There are plenty of colors to choose from. Okay, I'm going to remove the colors from our script and then we can continue encoding our column color. Now let's encode color with the six cylinder cars. Okay, so we have column color once again. And we will filter this to cylinder equals six. And we will set this equal to our second color. Okay, we only have one color left to go. Let's go ahead and add a color value for mtCars where cylinder equals eight. I'll paste this in. We'll assign the eight cylinder cars the color of gray. And now if we look at mtCars once again, we can see that our column color has been populated with these three colors. Okay, we're ready to visualize the mtCars mpg and weight columns once again. Let's go up and grab the previous plot and I'll paste it below. So we have a plot of mtCars mpg by the column weight. The one change we're going to make is color is now going to take the value column color. So I'll type mtCars dollar sign column color. And we can visualize our data. With cylinder encoded by color, we can derive insights not just about the relationship between weight and miles per gallon. At a glance, we can see that the eight cylinder engines have lower fuel economy than the four cylinder engines. We can continue to stylize our visualization until it's to our liking, adjusting the size and shape of the dots, as well as changing the labeling of our visualization. Thanks for watching. Stay tuned for the next video where I'll show you visualization using the ggplot2 package.