The graph below shows world population data from 1960 to 2020 (at 5 year interval). I am interested in knowing in what fashion the world population increased from 1960 to 2020.
To investigate, I created dual axis scatterplot to clearly visualize the growth trend. It shows that the world population increased by little over 2.5 times in the duration of 60 years from 1960 to 2020. The world population grew steadily from 1960 to 2020. High R2 value (i.e. 98.9%) of linear regression line (or trendline) strongly suggests the population growth is linear.
This dual axis scatterplot clearly reflects the above findings.
library("readxl")
table1 <- read_excel("table1.xlsx")
par(mar=par()$mar+c(4,0,0,4))
plot.new()
plot.window(xlim=c(1,13), ylim=c(1.5,3))
axis(1, seq(1,13, by=1), labels=table1$Year, cex.axis=0.75)
axis(1, seq(1,13, by=1), labels=F, tck = 1, lty = 2, col = "gray")
axis(2, seq(1.5,3, by=0.5), las = 1)
axis(2, seq(1.75,2.75, by=0.5), tck = -.028, las = 1)
axis(3, seq(1,13, by=1), labels=F)
axis(4, at=seq(1.5,3, by=0.5), labels=as.character(round(2^c(1.5,2,2.5,3),1)), las = 1)
axis(4, at=seq(1.5,3, by=0.5), labels=F, tck = 1, lty = 2, col = "gray")
axis(4, seq(1.75,2.75, by=0.5), tck = -.028, labels=as.character(round(2^c(1.75,2.25,2.75),1)), las = 1)
axis(4, seq(1.75,2.75, by=0.5), labels=F, tck = 1, lty = 2, col = "gray")
mtext("log2(Population)", side=2, line=2.9)
mtext("Population (in Billions)", side=4, line=2.5)
title("World Population Data from 1960-2020")
box()
points(1:13, log2(table1$`Population (in BN)`), pch=19, col="blue")
lines(predict(lm(log2(table1$`Population (in BN)`) ~ table1$Year)),col='green')
legend("topleft",legend=paste("R2 is", format(summary(lm(log2(table1$`Population (in BN)`) ~ table1$Year))$r.squared,digits=3)))
mtext("Year", side=1, line=2.5)
mtext("Data points are given at 5 year intervals. They are
plotted on a log base 2 scale. Each unit increase on the left
vertical axis corresponds to the data increasing by a power of 2,
or a doubling in the data values.",
side=1, line=7, cex=.8)
The below beeswarm boxplot uses iris dataset to compare sepal lengths in the three flower species. I chose this plot type because beeswarm plots give clearer picture of how data points are spread for each categorical variable in addition to the regular benefits of boxplots.
The plot below shows that the median sepal lengths follows an increasing trend among the three species. The distribution pattern of data points for each categorical variable and the observance of very few outliers suggests that the data is uniformaly distributed and the statistical measures shown by the boxplots have high confidence.
This beeswarm boxplot strongly suggests that the sepal lengths follows increasing trend when going from sertosa to versicolor, and from versicolor to virginica.
library(ggplot2)
library(ggbeeswarm)
table2 <- iris
ggplot(table2, aes(x = Species, y = Sepal.Length)) +
geom_boxplot(outlier.shape = 1, outlier.color = "black", outlier.size = 2)+ geom_quasirandom(aes(colour = Species), width = 0.4) +
scale_y_continuous(breaks = seq(4, 9, 0.5),
limits = c(4, 9)) +
xlab("Species of Iris Flowers") + ylab("Sepal Length (in cm)")+
labs(title = "Comparison of Sepal Lengths on Different Species of Iris Flowers")
For this graph, I used life expectancy data for 50 US states in 1960 and 2019. I want to study i) how life expectancy in each US state compares from 1960 to 2019, and ii) any trend seen among states on geographical basis. I chose dumbbell dot plot because it works well for illustrating change and comparing the distance between two groups of data points.
The plot shows that the life expectancy increased by at least 5 years for all US states in the duration of almost 60 years. Also, the deep South states seem to have lower life expectancy compared to other geographical regions.
table3 <- read_excel("table3.xlsx")
dumbbell <- function(v1, v2, group = rep(1, length(v1)), labels = NULL,
segments = FALSE, text = FALSE, pch = 19,
colv1 = 1, colv2 = 1, pt.cex = 1, segcol = 1,
lwd = 1, main = NULL, xlab = NULL, ...) {
o <- sort.list(as.numeric(group), decreasing = TRUE)
group <- group[o]
offset <- cumsum(c(0, diff(as.numeric(group)) != 0))
y <- 1L:length(v1) + 2 * offset
dotchart(v1, labels = labels, color = colv1, xlim = range(v1, v2) + c(-2, 2),
groups = group, pch = pch, pt.cex = pt.cex, main = main, xlab = xlab)
if(segments == TRUE) {
for(i in 1:length(v1)) {
segments(min(v2[i], v1[i]), y[i],
max(v2[i], v1[i]), y[i],
lwd = lwd, col = segcol)
}
}
for(i in 1:length(v1)){
points(v2[i], y[i], pch = pch, cex = pt.cex, col = colv2)
points(v1[i], y[i], pch = pch, cex = pt.cex, col = colv1)
}
if(text == TRUE) {
for(i in 1:length(v1)) {
text(min(v2[i ], v1[i]) - 1.5, y[i],
labels = min(v2[i], v1[i]))
text(max(v2[i], v1[i]) + 1.5, y[i],
labels = max(v2[i], v1[i]))
}
}
}
dumbbell(v1 = table3$'1960', v2 = table3$'2019', text = TRUE,
labels = table3$State, segments = TRUE, colv1 = "black", colv2 = "red",
main = "Life Expentancy of 50 US States in 1960 and 2019", xlab = "Life Expentancy (in years)")
legend("topright", legend = c("1960", "2019"), pch = 21, pt.bg = c("black","red"), col = "black")
For this plot, I used mtcars dataset. This dataset comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles. The variables of interest for my plot are MPG (mile per gallon), gross horsepower (HP), number of forward gears, and number of cylinders in engine. I am interested in knowing (i) if there is proportional relationship between MPG and HP, & (ii) how does car MPG vary by # of gears and # of engine cylinders.
To investigate, an animated scatter plotly graph of MPG vs HP is created. The frame is set to vary by # of engine cylinders and data points are colored based on # of gears.
The animated plot strongly suggests that MPG and HP are inversely proportional (i.e., higher HP car has lower MPG and vice-versa). It can be seen that higher # of cylinders in engine corresponds to higher HP and lower MPG. Also, the plot suggests there is no definite relation between # of gears and MPG or HP based on the provided dataset.
library(plotly)
table4 <- mtcars
t1 <- list(
family = "Times New Roman",
size = 20,
color = "red")
t2 <- list(
family = "Times New Roman",
size = 18,
color = "green")
t3 <- list(
family = "Times New Roman",
size = 18,
color = "blue")
table4 %>%
plot_ly(x = table4$mpg, y = table4$hp) %>%
layout(title = list(text = "<b>Fuel Consumption & Horsepower of Cars Based on # of Gears</b>", font=t1),
xaxis = list(title=list(text="<b>MPG</b>",font = t2, standoff = 5), showline=TRUE, ticks="outside"),
yaxis = list(title = list(text ="<b>HP</b>", font = t2), showline=TRUE, ticks="outside"),
legend = list(title=list(text="<b> # of Gear </b>", font = t3))) %>% add_markers(frame = table4$cyl,
color = as.character(table4$gear)) %>%
animation_slider(
currentvalue = list(prefix = "<i># of Cylinders: </i>")
)