Author: morganlefey1119@gmail.com

  • Assignment #11: Debugging and Defensive Programming in R

    For this assignment, we are given the following code:

    It is supposed to return outliers of the columns in x, however, when we run this, there us no output. We can find the error by creating a matrix to test the function:

    In R, && only checks the first element of a logical vector, while & compares all elements individually. So, replacing && with & makes sure the operation works element by element.

    The assignment did not provide a definition for tukey.outlier(), so I had to define this function before testing my corrected code. The main bug in the original function was the use of && instead of &. The && operator only evaluates the first element, whereas & performs element-wise logical comparisons. Replacing it with & fixes the intended operation. The return shows there are no outliers in x. 

    Github link here.

  • Module # 11 assignment

    For this assignment, I will be recreating a dot-dash plot from Dr. Piwek using ggplot2 in RStudio. To start, I have to install and run packages ggplot2 and ggthemes:

    Then, using R’s built in dataset mtcars, I can create a dot – dash plot to visualize how cars MPG is effected by the cars weight. 

    From this graph, we can see a general trend: as cars weight increases, their MPG decreases. The dot-dash plot helps to see patterns and the inverse relationship between miles per gallon and car weight. Additionally, using ggplot2 made the coding process very simple, and creates an easy to understand, presentable visualization. 

  • Module # 10 Building your own R package

    For this assignment, we will be creating our own R package. I will be creating package visuals, which will host important and useful functions for creating data visualizations in R. 

    The package name is visuals  and it is made to make visualizations easy. I am the sole author and creator of the package. The package will include graphing functions, and has ggplot2 as an import of outside code. 

    The purpose of my visuals package is to make creating data visualizations in R easier and more beginner friendly. I want this package to be useful for students and people who are still learning R and may not be comfortable creating graphs on their own. The goal is to provide simple functions that make it easier to turn data into clear and understandable visuals without having to write a lot of complicated code. I plan to include several functions, such as create_barplot() for making bar charts to compare categories, create_lineplot() for showing trends over time, create_scatterplot() for showing relationships between two variables, create_histogram() for displaying data distributions, and create_boxplot() for comparing distributions and identifying outliers.  I want visuals to be a helpful starting point for anyone who wants to create basic graphs in R without feeling overwhelmed by the process.

    GitHub link here.

  • Module # 10 assignment

    For this assignment, we will be using ggplot2 and creating time series visualizations to analyze data.

    To start, we will make a barplot. Using hotdogs data and ggplot2, we can create a visualization that shows the contest results over 30 years. We can see that the number of hotdogs and buns seems to mostly steadily increase over time, going from an average of 10 to well over 50.

    Next, using ggplot, we can create a similar graph, but one that is more refined, easier to understand, and more presentable. 

    In the first graph, there is no indicator of what the different colored bars mean. In the new visualization, the key makes it very clear that they represent new record amounts of HBDs eaten. Additionally, the first graph’s y axis cuts off at 60, while we have some data bars that exceed that, but it’s unclear just what number they are. In the new graph, we can see that they are in between 60 and 70. 

    Using base R graphics, we can also create a stacked bar plot to compare the hotdog eating contest results from 2000 to 2010. Unlike a regular bar plot, which displays each category separately, a stacked bar plot places the values on top of one another within each bar. This allows us to see the combined total as well as how each category contributes to that total.

    Next, using ggplots built in dataset, economics, we can can create a time series to plot unemployment population over time. This graph gives us a visual look at how unemployment has changed over time. We can easily see periods where unemployment increased or decreased, showing how economic conditions have shifted throughout the years.

    These graphs show the relationship between the unemployment-to-population ratio and the median number of weeks people remain unemployed. The first graph shows the overall relationship, while the second uses different colors to represent each year, making it easier to see how the pattern changes over time. We can see how unemployment levels and the length of time people remain unemployed are connected, as well as how this relationship shifts across different years. 

  • Assignment #9: Visualization in R – Base Graphics, Lattice, and ggplot2

    For this assignment, we will be comparing three visualization systems in R: base graphics, lattice, and ggplot2. I’ll be using the built in dataset iris. 

    To start, using R’s base functions and graphics, I created a scatter plot comparing flower sepal length vs width. Then, a histogram showing the distribution of petal length.

    Then, using the lattice package, we can compare sepal length and width by species using a conditional scatter plot. We can also look closer at the petal length distribution by grouping the data into species.

    Finally, using ggplot2, we can look even further at the relationship and patterns in the data between sepal length and width by species, and at petal length by species. 

    Between the base, lattice, and ggplot2, I think base has the most straightforward syntax, but the most basic results. I really liked both of the lattice visualizations and the ggplot2 scatter plot, and think they produced the most publication-quality output. Grouping by species allows for more insigts into patterns, and the visuals are clear and easy to understand. Both the lattice and ggplot2 syntax are more complicated than the base R, but overall they are still easy to understand, intuitive,  and not needlessly complex. However in terms of minimalism, the lattice code was the simplest with best results. I didn’t face any challenges when switching between any of the 3. 

     

    GitHub link here.

  • Module # 9 assignment: Multivariate Visualization

    For this assignment, we are asked to create a multivariate graph, which shows relationships between more than 2 variables. I decided to use RStudios iris dataset, which contains observations of 3 species of iris flowers. It contains 150 observations, and each records four physical features measured in centimeters: sepal length, sepal width, petal length, and petal width.

    Using ggplot, I created a scatter plot visualization comparing petal length vs. petal width. The points are color coded by species, and the point side correlated with sepal length. 

    For my multivariate graph, I used the iris dataset because it comes built into RStudio, and I have been using the mtcars dataset a lot lately, so I wanted to switch it up and try something different. My graph compares petal length, petal width, sepal length, and the 3 different species of flowers. One thing I noticed was that Setosa flowers have the smallest petals on average, Versicolor flowers are a little larger, and Virginica flowers have the largest petals on average. The graph also shows that flowers with longer petals tend to have wider petals as well. I think using a multivariate graph was effective for this dataset because it allowed me to compare multiple variables at once instead of having to make separate graphs for each one. Using different colors for the species and different point sizes for sepal length made it easier to see patterns and differences between the flowers without making the graph too confusing.

    Using different colors for the species creates a contrast that makes it very easy to tell which data points belong to which species of flower. For instance, we can clearly see that the Setosa flowers have a much smaller petal size on average, while Versicolor is larger, but Virginica has the largest petal size on average out of all 3. Proximity was used by how the data points are grouped together based on their petal length and width. Flowers with similar measurements are closer together, which makes it easier to see the similarities and differences between the species. The colors also help connect the points that belong to the same species even if they aren’t right next to each other.  The different colors and point sizes help keep the graph visually balanced, while the labels and legend make everything look more organized and easier to follow.

  • Assignment #8: Input/Output, String Manipulation, and the plyr Package

    To begin this assignment, I download and then upload the provided dataset into RStudio. Then, I separate the rows and convert rows Age and Grade into numeric data. 

    Then, using the plyr package and dpply, we can compute the mean of Grade by Sex.

    Then, we write the grouped means to a text file:

    Finally, we will filter our data, to create a subset of data with students whose name contains an “i”. Then, save as a csv with just the students names, and then one with their full data (including Age, Sex, and Grade).

    GitHub link here.

  • Module # 8 Correlation Analysis and ggplot2

    I will be using Rs built in dataset mtcars in order to explore relationships between variables using correlation or regression analysis and visualize them using ggplot2.

    I will use cor() to compute correlation matrices,  lm() for linear regression models, and ggplot2 for visualizations with geom_point() for scatter plots, stat_smooth(method = "lm") to add regression lines, and  facet_wrap() or gridExtra to arrange multiple plots.

    Correlation values range from -1 to +1. A value of +1 indicates a positive correlation, meaning that when one variable increases, the other tends to increase as well. A value of -1 indicates a negative correlation, meaning that when one variable increases, the other tends to decrease. A value of 0 indicates little or no linear relationship between the variables.

    The cor(mtcars) function calculates a correlation matrix to show the strength and direction of relationships between variables in the dataset. So for instance, the results show a strong negative correlation between  mpg and wt, meaning heavier cars tend to have lower fuel efficiency. There is also a strong negative correlation between mpg and hp, suggesting that cars with higher horsepower generally have lower fuel efficiency. 

    In R, lm() is used to create a linear regression model, which helps us understand relationships between variables and predict an outcome based on one or more predictors.

    We can look at the relationship between mpg and wt further:

    The linear regression model shows a negative relationship between car weight and fuel efficiency. The weight coefficient is approximately -5.344, meaning that for every increase of 1,000 pounds in car weight, fuel efficiency is predicted to decrease by approximately 5.34 miles per gallon. The intercept is approximately 37.285, meaning the predicted MPG when car weight is zero. The model has an R squared value of approximately 0.753, meaning that car weight explains around 75.3% of the variation in fuel efficiency in this dataset. It seems heavier cars generally have lower fuel efficiency.

    I created three scatter plots with regression lines to show the relationships between MPG and weight, displacement, and horsepower. In all three , there is a negative relationship, so as these values increase, MPG generally decreases. Heavier cars and cars with higher horsepower or displacement tend to have lower fuel efficiency. Using a grid layout with facets made it easy to compare these characteristics because all three charts are displayed next to each other, making the patterns easier to identify. I also used a relatively neutral color scheme to keep the visuals simple and easy to read, although I kept the regression line blue because I felt it looked better than a gray or more neutral line. I agree with Few’s recommendations for keeping visualizations clear and avoiding unnecessary clutter. However, I think some flexibility is important because a color choice can improve the appearance of a graph while still making the information easy to understand.

  • Assignment #7: Exploring R’s Object Oriented Systems 

    I will be using Rs built in dataset mtcars for this assignment.

    To start, we can show the first few rows with head() and describe its structure with str(). Then, using generic functions print() and summary()we can further inspect the data.

    Then, we will create 2 objects and inspect:

    You can tell whether an object uses S3 or S4 by using functions  class() and isS4(). The class() function tells us an object’s class, and isS4() checks whether it belongs to the S4 system. You can tell an object’s type by using typeof(), which returns its storage type. We can also use str() to examine its structure. Generic functions in R like these select a specific method based on an object’s class. The main difference between S3 and S4 is that S3 is simpler and more flexible, while S4 is more structured. S3 classes and methods are easier to create, but S4 requires you to define classes and methods using functions like setClass() and setMethod(). S4 also allows you to set specific data types for an object’s slots, which helps prevent errors.

    GitHub link

  • Module #7 Assignment: Visualizing Distributions in R

    Using RStudios built in dataset mtcars and ggplot2 , we will be creating  visualizations for distribution analysis. I decided to first create a density plot to see the distribution of miles per gallon across the number of cylinders (4, 6 or 8):

    This visualization shows the spread and distribution of cars MPG by number of cylinders. According to Few, distribution visualizations have 3 key characteristics, spread, center, and shape.

    4 cylinder:

    • Spread between 15-35 MPG
    • Center is a little less than 25 MPG
    • Shape is leaning towards the right and mid to higher values

    6 cylinder:

    • Spread between 15-25 MPG
    • Center is a little above 20
    • Bell shaped, with values close together

    8 cylinder:

    • Spread between 10-20 MPG
    • Center at 15 MPG
    • Has multi modal shape, with value concentrated mostly at 15 MPG, and then 10 and 20 MPG

    Looking at the graphs and these characteristics, we can notice patterns of:

    • cars with 4 cylinders tend to have mid to high MPG of around 20 and above
    • those with 6 cylinders stay in the mid range of 15-25 MPG
    • cars with 8 cylinders tend to have a lower MPG, of 20 and below

    It could be inferred that the more cylinders a car has, the more fuel is consumed. 

    For a bar plot for categorical distributions, we can look at 

    The graph is skewed to the left, showing a higher distribution of cars having 3 gears, and the number progressively lowering from 4 to 5. 

    Finally, for scatter plots, we can compare automatic vs manual transmission MPG by the cars weight:

    Just from looking at this visualization, we can see that manual cars in the dataset have higher MPG the lighter the car is. Automatic cars tend to have a lower MPG, but some automatic and manual cars have similar MPG values, so transmission type alone does not explain fuel efficiency.

    I agree with Few’s critique of how distributions are commonly presented in visual analytics. Using averages can hide important details and fail to show the full picture of the data. I also agree that box plots are not always the best way to visualize distributions because they can hide individual data points and other important patterns. Additionally, our eyes tend to focus on the box, which may lead us to assume that this is where the data is most concentrated, even when that may not be the case.