How to Use Data Visualization in R

We’re using ggplot because it’s simple and it’s based on the “Grammar of Graphics” principles. These principles offer modern data visualization thanks to a structured approach that allows you to systematically plot out the points in your visualization. ggplot is versatile. You can use it to create a coordinate system out of your data on which you can layer other visualizations. We’re going to keep things simple and use ggplot to map out a coordinate bar chart using the “displ” and “hwy” fields from the “mpg” data frame.

·        displ – Vehicle engine size

·        hwy – Fuel efficiency on the highway

What we should see when we run the following piece of code is a set of coordinates proving that vehicles with larger engines use more fuel on the highway. A logical conclusion. To map out the data and prove the hypothesis, type these lines:

ggplot(data = mpg) +

geom_point(mapping = aes(x = displ, y = hwy))

What have you done here?

You’ve used the “ggplot(data = mpg)” function to create your basic coordinate system “mpg” dataset. The second line of code adds a layer of points to your coordinate system, leaving you with the first of many scatter plots you can create with the “mpg” data frame.

You should see a scatter plot showing that the cars with the smallest engines achieve the best fuel efficiency. So, you’ve proven the hypothesis.

Read more here: Source link