Creating plots with ggplot2 in IB Mathematics Year II

My year II IB Mathematics class is going very well this year. Our hard work last year is starting to pay off in that we know how to use ggplot2, dplyr and now R Markdown. The students will complete their Internal assessment, which is a large research report, by the end of December.

I have encouraged them to write in R Markdown so their research is reproducible. One student now has the general outline of his report completed and is using an Rmd file and knitr to generate a Word file complete with code, output and plots.

I have here a few partial screen grabs to give you a flavor of the type of report that can be generated using R Markdown and knitr. These are not completed plots and the code and report are still in the editing phase.

But I’m glad that this process is Continue Reading

Video How To Find The Acceleration Due To Gravity Using Tracker

trackerg1I will be using Tracker more this year in my science classes so I am getting ready by filming some of our labs. Yesterday, the class completed the drop the bouncing ball lab by graphing the connection between initial height and bounce height.

We can also use this video to calculate the acceleration due to gravity using Tracker software. I’ll extend this lab next week for the students when they get into the computer lab.

In this video, we calculated the mean acceleration due to gravity at at about -9.7m/s^2. This is close to the official 9.8 m/s^2.

trackerg2In this picture, you can see the cursor is pointing to the mean for g is -9.68 m/s^2. When we do this in the lab, I’ll have the students video the ball completely from the top all the way to the floor. This way, we’ll have more data.

Below is the video of how I used Tracker to find g. You can also use the auto tracker in this program, but I have not used it yet. The auto tracker is supposed to be Continue Reading

Using Statistics in Tracker For Our Rocket Project

Tracker software
Tracker software can calculate velocity

I am back to school now for another new year. This year I hope to expand on my use of the Tracker software in my Physical Science classes.

Since many of my students have a smart phone, I want them to take video of their rocket blast off so they can incorporate the video into the  video analysis program.

They will learn how to calculate the velocity and acceleration using Tracker by tracking the their rocket in the video. I’ll be starting the Rocket project next week to be completed by the end of the week.

Stat Analysis of Rocket
Stat Analysis of the Rocket Blast Off

The Tracker software will also do some basic statistical analysis such as finding the mean and standard deviation of a given data. I’m still playing with this and I’m not sure how best to introduce this aspect to my students.

A lot of the analysis depends on the quality and position of the video taken of the rocket blast off. The video should be at exactly 90 degrees for best analysis. I’ll have another post when we are ready to go to Mars.

We have off the first week of October off so we’ll aim for blast-off the week of the 9th. Stay tuned.



Why I Teach R Programming to my AP Statistics Class

AP Statistics and R Programming

Should AP Statistics students learn to program in R? Definitely yes. R is a powerful programming language and an important skill to have entering college. In my class, we use the TI-Nspire CX graphing calculator for most of our course work during the year but there are times when I feel it would be better to use R.

We usually have some time to explore data and I think learning some of the basics of R is worth the time and effort. In my school, the computer lab is just down the hall and we are also wired for WI-Fi throughout the school. I have loaded both R and RStudio on our school computers so we are ready to go from day one.

I think it’s important for students to think deeply about their data and using R with RStudio helps a great deal. Plus there is a ton of good data on the internet that becomes available to analyze using tools like R.

Using data from our textbook

We use The Practice of Statistics 3rd Edition, Starnes, Yates, and Moore during the year and I also have a copy of Stats Modeling the World, 3rd Edition (Bock, Velleman, De Veaux)

In Bock’s book, in the beginning of chapter three, they give three rules for data analysis:

  1. Make a picture
  2.  Make a picture
  3. Make a picture

The reason given for this is:

These days, technology makes drawing pictures of data easy, so there is no reason not to follow the three rules. Bock, p21

At the beginning of the semester, it’s a good time to introduce the students to the R and RStudio environment. This is where they can play with R’s base graphics, and for the more advanced student, work with ggplot2. It takes time to introduce R and some of the basics, but all of my students have a laptop that they can use at home or at school. I encourage them to download both R and RStudio on their laptop.

Once they have done that, I show them how to download different packages. We will work mostly with ggplot2 and the dplyr package as dplyr makes it easier to subset data.

To begin, I would show my students some code in vector form and how to make a simple bar graph. I will try to recreate the graphs on pages 22 and 23 from Stats, Modeling The World.

I usually put the code on the front SmartBoard for the students to copy. I encourage them to read and to look at code examples on the internet and to bring in any reference material they find in addition to the material I hand out.

Learning to code in R for a bar plot

First I make sure they are in the correct working directory, or at least know what directory they are saving their work to. I then try to recreate some of the bar plots in the text so the students can get interested right away. Also, my students feel that they are doing “real” statistics now by actively creating plots instead of just looking at them in the book.

I also tell them at this point they have to take more ownership of their code when they are stuck. I can help with some of the error messages, but they need to Google any error code to try and figure out the problem. I found this to be a very rewarding part of my teaching by not knowing all the answers and figuring out the solution with the “team”.

So here would be a first attempt at plotting a bar graph.

And this is the plot.


bar plot1The teaching part here is to show that this plot is not complete and we need to put more information for the x and y labels;  xlab and ylab.

Here is the new plot.

bar plot2



Another example from the book gives the count for each class.

The plot looks like this.



Bar plot 3

Extension: Working with ggplot2

Some of my students will be able to grasp the base graphics in a short time but may need additional help to continue. I usually give them a PDF document for the base graphics and also one for R commands. Other students will run with this and ask for additional information on how to graph. This is when I introduce ggplot2 for those students that are more self directed and motivated. Last year, all my students wanted to learn more about using ggplor2 when they saw the results of a few other students’ plots using ggpolt2.


I will usually spend the last ten minutes in class giving the basics of ggplot2 and how to make a simple plot. I try to avoid using the qplot2 because by the end of the school year, my students will have enough time to internalize the basics of the grammar of graphics. Some of them also used their knowledge of ggplot2 in their AP Economics class when exploring data.

Some would explore with the color =  and fill = arguments to come up with different plots.

Here is one beginning plot.


ggplot2 bar plot

With a little different code, we get something better.

Which gives this nice plot.


ggplot2 bar plot colorAt this point, it take a while for all this information to sink in and many of my students will tell me they will play with the graphs at home to try to impress the class. We will also have experience using our TI Nspire CX to make bar plots as there are clear instructions in the back of the Starnes book.

Other lessons using R in the beginning of the year include converting vector data to a data.frame and using the dplyr package. I’ll share more about these topics in future posts.



How To Print x Label Vertical In Ggplot2

I was working with some boxplots last month and I needed to plot twelve months of air quality data. The problem was that the twelve months over lapped each other and the plot didn’t look good. If I could only draw the x labels vertical.

For this example, I’ll show you how to plot the x labels vertical. It’s just a matter of using the theme() function.

Here is the R code using ggplot to plot the Iris data of Species and Sepal.Width

Here is the graph of this plot.

X horizontal

To make the x label vertical, add the theme() function:

The new plot will look like this:

X 60 degrees

You can change it to 90 degrees by adjusting the angle.


The new 90 degrees plot now looks like this:

X 90 degrees

I am working on more plots using air quality data that plots month verses AQI2.5 and I’ll have them for my next post.

Working with Rmd files using knitr and WordPress


Author Kevin

Here is a pairs plot.

Here is a regression model with the output below


What Story Telling Can Teach Us About The Learning Process

16 Scientists and Mathematicians
16 Scientists and Mathematicians that my students learned about.

In my Physical Science class this year I taught 16 scientists and mathematicians to my students. Most of these scientists are well known and a few less so .

To engage the student, I try to tell a story about each scientist because as research shows, students can remember a story much better than just a list of facts alone.

For each scientist, I also have them remember the year they were born and one or two key facts about an important aspect of their lives as a scientists.

Now for some of these scientists I am generalizing as some are mathematicians, but there is usually a scientific link from their work to sciences.

One good example is G.H. Hardy. He proudly exclaimed in his book that Continue Reading

The Electroscope Demo

In class this week we built a simple electroscope to demonstrate static electricity. The students built it with things we had in the classroom. In the video, you can see the two pieces of aluminum foil move apart as the static charge is applied.

Here is a nice video showing how to make an electroscope.


Get Your Numbers Ready For Pi Day


Are you ready for Pi day? Only one week to go. I’ve been doing the Major System for a few years now and it’s time for a little mental math and for me to demonstrate what this system can do.

I got pi to 100 digits as of today. My goal for pi day is 200 digits.

In. Front. Of. The. Class. On the Smart board.

Anybody want to ride the number wave with me?


3/7/2016 Update: I have a challenger! One of my students asked me for the 200 digits list. We’re ready to rumble.

3/14/2016: I successfully wrote 200 digits of pi. My challenger did over 100.

Chengdu Food You Just Have To Try

Chengdu1I was on my way to the grocery store yesterday and I stopped for lunch at my favorite place to eat. It’s a small shop that serves the hot sauce wonton’s and the round veggie wraps.

While I was waiting, I realized I never took any video or pictures of this place. So today, I share with you some good food if you happen to be in Chengdu. This is right across for the Carefour store in the Tonzilin section of town.

The video below shows how they make the food. It’s fun to just watch as they can make them pretty fast.