# Check if the package is installed
if (!require("trelliscopejs")) {
# Install the package if it's not already installed
install.packages("trelliscopejs")
}
if (!require("ggplot2")) {
# Install the package if it's not already installed
install.packages("ggplot2")
}
if (!require("tidyverse")) {
# Install the package if it's not already installed
install.packages("tidyverse")
}
if (!require("dslabs")) {
# Install the package if it's not already installed
install.packages("dslabs")
}
if (!require("purrr")) {
# Install the package if it's not already installed
install.packages("purrr")
}
# Load library
library(trelliscopejs)
library(ggplot2)
library(tidyverse)
library(dslabs) #gapminder dataset
library(purrr)
For a data set of your choosing, make a faceted plot using the trelliscopejs package. You may make any type of plot; scatter plot, histogram, etc. but, as mentioned in the discussion below, you must explain why you chose this plot and what you are investigating about the variable you are graphing.
The trelliscope plot must include one cognostic measure of your own. Include a description of what it is and what information this measure gives.
# Using *ggplot()*, create a scatter plot of population(x) vs. life expectancy(y) for the year 2010.
# filter the gapminder data where year is 2010
#looking at life expectancy vs. year for each country in Europe
data <- as_tibble(gapminder)
view(data)
onlyAmericas_data <- subset(data, continent == "Americas")
view(onlyAmericas_data)
#creating scatter plot
splot <- ggplot(onlyAmericas_data, aes(year, life_expectancy)) +
geom_point()+
labs(title = "Year vs Life Expectancy",
x = "Year",
y = "life expectancy") +
facet_wrap(~ country + continent)+
theme_bw()
splot
#There are too many to show on a single plot. This is where we can turn to Trelliscope.
#To create the same plot with Trelliscope, swap facet_wrap() for the trelliscopejs function facet_trelliscope():
# x axis values to set the xlim
min(onlyAmericas_data$year)
## [1] 1960
max(onlyAmericas_data$year)
## [1] 2016
# y axis values to set the ylim
min(onlyAmericas_data$life_expectancy)
## [1] 32.2
max(onlyAmericas_data$life_expectancy)
## [1] 81.7
# Create a function to calculate the desired cognostic
cognostic_pop <- function(data) {
population <- nrow(data)
return(population)
}
# Perform data wrangling and add the cognostic
onlyAmericas_data <- onlyAmericas_data %>%
group_by(country, continent) %>%
mutate(population = cognostic_pop(.))
ggplot(onlyAmericas_data, aes(year, life_expectancy)) +
geom_point(shape = 16,colour="blue")+
xlim(1960,2016)+
ylim(30,90)+
labs(title = "Year vs Life Expectancy ",
x = "Year",
y = "life expectancy") +
#using trelliscope
facet_trelliscope(~ country + continent, nrow = 2, ncol = 3, scales = c("same", "same"),
width = 700,
height = 700,
path = ".",
self_contained = TRUE,
#auto_cog = TRUE
auto_cog = list(Population = function(data) nrow(data))) + # Adding cognostic
#adding theme to the plot
theme(plot.title = element_text(hjust=0.5, size = 20,face="bold"),
axis.text=element_text(size=20,face="bold"),
axis.title =element_text(size=20,face="bold"))
## using data from the first layer
## Warning in min(x): no non-missing arguments to min; returning Inf
## Warning in max(x): no non-missing arguments to max; returning -Inf
## Warning in min(x): no non-missing arguments to min; returning Inf
## Warning in max(x): no non-missing arguments to max; returning -Inf
## Warning in min(x): no non-missing arguments to min; returning Inf
## Warning in max(x): no non-missing arguments to max; returning -Inf
or go to the web https://rpubs.com/txsst/1172089
Description 2-3 paragraphs.
Describe the data set. Explain the variable you are graphing in your plots and the reason you are investigating with it. Discuss the reason/motivation you chose the variable to facet on, and what insight or trend you are attempting to investigate. Discuss any challenges you had in making the graphs and how you dealt with these challenges. Name at least one cognostic measure (this can include the cognostic you created or be different) the reader could investigate, and explain any insight they might gain from it.
knit the file to an html document
publish this to an RPubs page. Note: you can add a url directly to the text and it will be active in the html (and word document if you knit to that)
Answer: The above scatter plot displays the relationship between the years and life expectancy for each country within the Americas continent.It displays the year on the x-axis and life expectancy on the y-axis. Each data point is represented by a blue dot. The x-axis limits are set from 1960 to 2016, and the y-axis limits are set from 30 to 90. The plot is faceted by country and continent using the facet_trelliscope() function, allowing for easy comparison between different countries within the Americas. The plot dimensions are specified to be 700x700 pixels, and the plot is self-contained so that user can easily access all the plots. Additionally, automatic cognostics are enabled to provide additional insights into the data. Finally,the theme settings are applied to enhance the appearance of the plot, including adjustments to the title and axis labels.
The purpose of the above scatter plot is to explore the relationship between the passage of time (years) and the life expectancy of different countries in the Americas continent.
I choose to facet the plot by country and continent so that it allows for a detailed examination of how life expectancy trends vary across different countries within the Americas. By breaking down the data into smaller panels for each country, it becomes more easier to see if any patterns or trends that exists.
One potential challenge I faced while creating this plot was that the panel was not displaying when publishing the R code in the browser because I did not use the self_contained attribute inside the facet_trelliscope function. Once I added that attribute, it finally worked for me.
I have included the “Population” cognostic in the above trelliscope plot.It calculates the total number of observations (rows) within each group, representing the population size of countries within the Americas continent. This cognostic allows for a quantitative comparison of the population sizes across different countries and continents, providing insight into the distribution and representation of data within the plot. by adding that cognostic, it is not only visualizes the relationship between year and life expectancy for countries in the Americas but also provides information about the population sizes of these countries.
In my filterd dataset (onlyAmericas_data), there are a total of 2052 rows. When sorting by population from the left-hand side of the panel in the Trelliscope, the cognostic population with the value of 2052 will be displayed under each facet