## Data Visualization #13—Roulette and Temperature with R code

In the most recent post in my data visualization series I made an analogy between climate, weather and the spins of a roulette wheel that demonstrated that short-term randomness does not mean we can’t make accurate long-term predictions.

Towards the end of the post I appended an animation of 1000 random spins of a roulette wheel. In that post, I plotted the 1000 individual outcomes of these random spins of the roulette wheel. I chose to show only one outcome at a time as the animation cycled through all 1000 spins. In this post, I wanted to show you how to keep all of the outcomes from disappearing. Rather than having the value of each spin appear, and then disappear, I will change the code slightly to have every spin’s outcome stay on the plot, but faded so that the focus remains on the next spin value. Here’s what I mean.:

Here is the R code for the image above:

```## These are the packages needed to draw, and animate, the plots.
library(ggplot2)
library(gganimate)
library(dplyr)  # needed for cummean function

## Set up a data frame for the 1000 random spins of the roulette wheel

mywheel <-c(rep(0,2),1:36)  # a vector with the 38 wheel values
wheel.df<-data.frame("x"=1:1000,"y"=sample(mywheel,1000,rep=T))

## Plot, then animate the result of 1000 random spins of the wheel

## the code to plot
gg.roul.1000.point<- ggplot(wheel.df,aes(x, y, colour = "firebrick4")) +
geom_point(show.legend = FALSE, size=2) +
theme_gray() +
labs(title = "1000 Random Spins of a Roulette Wheel",
x = expression("the"~n^th~"roll of the wheel"),
y = 'Value of a single spin') +
theme(plot.title = element_text(hjust = 0.5, size = 14, color = "black")) +
scale_y_continuous(expand = c(0, 0)) +
transition_time(wheel.df\$x) +
shadow_mark(past = T, future=F, alpha=0.2)

## the code to animate
gg.roul.anim.point <- animate(gg.roul.1000.point, nframes=500, fps=25, width=500, height=280, renderer=gifski_renderer("gg_roulette_1000.gif"))

## No plot and animate a line chart that depicts the cumulative mean from spin 1 to spin 1000.

gg.roul.1000.line <- ggplot(wheel.df, aes(x, y = cummean(y))) +
geom_line(show.legend = FALSE, size=1, colour="firebrick4") +
theme_gray() +
ggtitle("Cumulative Mean of Roulette Wheel Spins is Stable over Time") +
theme(plot.title = element_text(hjust = 0.5, size = 14, color = "black")) +
labs(x = expression("the"~n^th~"roll of the wheel"),
y = 'Running (i.e., cumulative) Mean of all Rolls at Roll n') +
scale_y_continuous(expand=c(0,0), limits=c(0,36)) +
transition_reveal(wheel.df\$x) +
ease_aes('linear')

gg.roul.anim.line <- animate(gg.roul.1000.line, nframes=500, fps=25, width=500, height=280, renderer=gifski_renderer("cummean_roulette_1000.gif"))

## Now combine the plots into one figure, using the magick library

library(magick)

roul_gif <- image_append(c(a_mgif[1], b_mgif[1]),stack=TRUE)
for(i in 2:500){
combined <- image_append(c(a_mgif[i], b_mgif[i]),stack=TRUE)
roul_gif <- c(roul_gif, combined)
}

## Save the final file as a .gif file

image_write(roul_gif, "roulette_stacked_point_line_500.gif")
```

Stay tuned for a Python version of this chart.

## Global Warming ‘Hiatus’ Expected to end by 2030

For this week’s seminar, we read and discussed (amongst other things) a general (i.e., non-academic) article–in The Guardian newspaper-regarding the recent so-called hiatus in global warming. (Here’s another look at the same issue from The Economist.) The issue arises from recent global surface temperature data. To wit:

BETWEEN 1998 and 2013, the Earth’s surface temperature rose at a rate of 0.04°C a decade, far slower than the 0.18°C increase in the 1990s. Meanwhile, emissions of carbon dioxide (which would be expected to push temperatures up) rose uninterruptedly. This pause in warming has raised doubts in the public mind about climate change. A few sceptics say flatly that global warming has stopped. Others argue that scientists’ understanding of the climate is so flawed that their judgments about it cannot be accepted with any confidence. (From The Economist)

As the article quoted above goes on to note, there are many compelling scientific accounts for why global surface temperatures have not risen as quickly as in the past, though the author argues that they, in combination, explain too much. To understand what that means, please read the article yourself.

We viewed a video by climate scientist Matt England, in which he explained one plausible reason for this ‘hiatus’–the changing trade winds in the Pacific Ocean.

After having viewed Professor England’s explanation–more heat than normal was being trapped in deeper layers of the western Pacific Ocean–some students wondered when that extra trapped heat might once again rise to the surface. Not being a climate scientist, I did not know the answer. I now know, however, that some scientists predict this to occur by about 2030.

The Atlantic Ocean has masked global warming this century by soaking up vast amounts of heat from the atmosphere in a shift likely to reverse from around 2030 and spur fast temperature rises, scientists said.

The theory is the latest explanation for a slowdown in the pace of warming at the Earth’s surface since about 1998 that has puzzled experts because it conflicts with rising greenhouse gas emissions, especially from emerging economies led by China.

But, if you read the linked article carefully, you’ll notice that these study and explanation cited has nothing to do with the Pacific Ocean. Indeed, the study is by a group of scientists based at the University of Washington:

“We’re pointing to the Atlantic as the driver of the hiatus,” Ka-Kit Tung, of the University of Washington in Seattle and a co-author of Thursday’s study in the journal Science, told Reuters

The study said an Atlantic current carrying water north from the tropics sped up this century and sucked more warm surface waters down to 1,500 metres (5,000 feet), part of a natural shift for the ocean that typically lasts about three decades.

It said a return to a warmer period, releasing more heat stored in the ocean, was likely to start around 2030. When it does, “another episode of accelerated global warming should ensue”, the authors wrote.

So, what do we take from these two different studies. Is the article in The Economist correct that the current warming hiatus is ‘over-explained’? Is this just another example of scientists blindly whacking away at a pinata, hoping to hit upon an explanation? Or, is this another episode of how science is done in the real world. Theory and data combine to make predictions, which may be more or less true. When anomalies occur (that is, predictions are not quite accurate), scientists go about finding new data, and developing new theories to improve upon existing theories and knowledge. Or, is this just a loosely-linked cabal of money-seeking scientists trying to make off like bandits with our tax (i.e., research) money and blithely destroying our freedom while they’re at it?