### R and RStudio Installation and Setup Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/appendix-a-resources.md Resources for installing R and RStudio, including walkthrough videos and guides. Essential for setting up a reproducible research environment. ```R # Installing R - PsyTeachR # https://psyteachr.github.io/data-skills-v1/installing-r.html # Running R on your own computer (walkthrough videos) - Danielle Navarro # https://www.youtube.com/playlist?list=PLRPB0ZzEYegOZivdelOuEn-R-XUN-DOjd ``` -------------------------------- ### R and RStudio Installation and Usage Guides Source: https://github.com/psyteachr/introdataviz/blob/master/docs/appendix-a-resources.md Resources for installing R and RStudio, including walkthrough videos and guides on writing and debugging code in RStudio. ```R https://psyteachr.github.io/data-skills-v1/installing-r.html https://www.youtube.com/playlist?list=PLRPB0ZzEYegOZivdelOuEn-R-XUN-DOjd https://www.rstudio.com/resources/webinars/programming-part-1-writing-code-in-rstudio/ https://www.rstudio.com/resources/webinars/programming-part-2-debugging-code-in-rstudio/ ``` -------------------------------- ### Load R Packages Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/02-ch2.md Loads the tidyverse and patchwork packages, which are necessary for data manipulation, visualization, and combining plots. Ensure these packages are installed before running. ```r library(tidyverse) library(patchwork) ``` -------------------------------- ### R and RStudio Installation Resources Source: https://github.com/psyteachr/introdataviz/blob/master/docs/additional-resources.html Links to resources for installing R and RStudio. Includes a walkthrough video series by Danielle Navarro and a guide from PsyTeachR on installing R. ```HTML * [Installing R - PsyTeachR](https://psyteachr.github.io/data-skills-v1/installing-r.html) * [Running R on your own computer (walkthrough videos) - Danielle Navarro](https://www.youtube.com/playlist?list=PLRPB0ZzEYegOZivdelOuEn-R-XUN-DOjd) ``` -------------------------------- ### Load R Packages Source: https://github.com/psyteachr/introdataviz/blob/master/docs/02-ch2.md Loads the tidyverse and patchwork packages, which are necessary for data manipulation, visualization, and combining plots. Ensure these packages are installed before running. ```r library(tidyverse) library(patchwork) ``` -------------------------------- ### R and RStudio Installation Resources Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/additional-resources.html Links to resources for installing R and RStudio, including a walkthrough video series and specific installation guides. ```APIDOC Installing R and RStudio: - Installing R - PsyTeachR: https://psyteachr.github.io/data-skills-v1/installing-r.html - Running R on your own computer (walkthrough videos) - Danielle Navarro: https://www.youtube.com/playlist?list=PLRPB0ZzEYegOZivdelOuEn-R-XUN-DOjd ``` -------------------------------- ### Getting Started with R for Visualization Source: https://github.com/psyteachr/introdataviz/blob/master/docs/reference-keys.txt Covers the initial steps of loading necessary R packages, loading data into R, and handling numeric factors and argument names for visualization functions. ```R getting-started loading-packages loading-data handling-numeric-factors argument-names ``` -------------------------------- ### Load R Packages Source: https://github.com/psyteachr/introdataviz/blob/master/docs/getting-started.html Loads the tidyverse and patchwork packages, which are essential for data manipulation and visualization in R. You need to load these packages every time you start a new R session. ```R library(tidyverse) library(patchwork) ``` -------------------------------- ### Load R Packages Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/getting-started.html Loads the tidyverse and patchwork packages, which are essential for data manipulation and visualization in R. You need to load these packages every time you start a new R session. ```R library(tidyverse) library(patchwork) ``` -------------------------------- ### Install introdataviz Package Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/06-ch6.md Installs the introdataviz package from GitHub, which provides custom plotting functions like geom_split_violin() and geom_flat_violin(). This is a prerequisite for using the advanced plotting examples in this tutorial. ```r devtools::install_github("psyteachr/introdataviz") ``` -------------------------------- ### Footer Content Update Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/getting-started.html Updates the footer content with project links (psyTeachR logo) and a Creative Commons license link using jQuery. ```javascript $( document ).ready(function() { var cite = ' '; var psyteachr = ' psyTeachR: Reproducible Research'; var license = ' Creative Commons License'; $("footer div.row div:eq(1) p").html( psyteachr + license + cite ); function move_side ``` -------------------------------- ### Tutorial Components and Setup Source: https://github.com/psyteachr/introdataviz/blob/master/docs/reference-keys.txt Details the components of the tutorial, including setting up R and RStudio, preparing your data, and understanding data formats and variable naming conventions. ```R tutorial-components simulated-dataset setting-up-r-and-rstudio preparing-your-data data-format variable-names data-values ``` -------------------------------- ### Add Y-axis Label to Histogram Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/02-ch2.md This snippet adds a y-axis label 'Number of participants' to a ggplot histogram, building upon the previous example with `theme_minimal()`. ```r ggplot(dat, aes(age)) + geom_histogram(binwidth = 1, fill = "wheat", color = "black") + scale_x_continuous(name = "Participant age (years)") + theme_minimal() + scale_y_continuous(name = "Number of participants") ``` -------------------------------- ### Install introdataviz Package Source: https://github.com/psyteachr/introdataviz/blob/master/README.md Provides instructions on how to install the introdataviz package from GitHub. It also mentions the prerequisite of installing the 'devtools' package if it's not already installed. ```r install.packages("devtools") devtools::install_github("psyteachr/introdataviz") ``` -------------------------------- ### Add Y-axis Label to Histogram Source: https://github.com/psyteachr/introdataviz/blob/master/docs/02-ch2.md This snippet adds a y-axis label 'Number of participants' to a ggplot histogram, building upon the previous example with `theme_minimal()`. ```r ggplot(dat, aes(age)) + geom_histogram(binwidth = 1, fill = "wheat", color = "black") + scale_x_continuous(name = "Participant age (years)") + theme_minimal() + scale_y_continuous(name = "Number of participants") ``` -------------------------------- ### R Data Visualization Setup and Basics Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/chapter-5.html This section covers essential R commands for data visualization, including loading packages, loading data, handling numeric factors, and creating basic plots like bar charts and histograms. It also touches upon customizing plots. ```R # Loading packages library(ggplot2) library(dplyr) # Loading data data <- read.csv("your_data.csv") # Handling numeric factors data$numeric_factor <- as.numeric(data$numeric_factor) # Bar chart of counts ggplot(data, aes(x = category_variable)) + geom_bar() # Histogram ggplot(data, aes(x = numeric_variable)) + geom_histogram(binwidth = 5) # Customization example ggplot(data, aes(x = category_variable, y = numeric_variable)) + geom_point() + labs(title = "Scatter Plot", x = "Category", y = "Value") + theme_minimal() ``` -------------------------------- ### Histogram with Custom Binwidth Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/getting-started.html Creates a histogram with a specified bin width, allowing for more control over the data aggregation. This example sets the binwidth to 5. ```R ggplot(dat, aes(x = age)) + geom_histogram(binwidth = 5) ``` -------------------------------- ### R Setup and Data Handling Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/chapter-4.html This section details the initial steps for setting up R and RStudio, preparing data for visualization, and understanding data formats, variable names, and values. It's crucial for researchers new to R. ```R # Loading packages library(ggplot2) library(dplyr) # Loading data data <- read.csv("your_data.csv") # Handling numeric factors data$numeric_factor <- as.numeric(data$numeric_factor) # Argument names # Example: ggplot(data, aes(x = variable1, y = variable2)) # Demographic information # summary(data$demographic_variable) # Bar chart of counts ggplot(data, aes(x = categorical_variable)) + geom_bar() # Plotting existing aggregates and percent # Example: data %>% count(group) %>% mutate(percent = n / sum(n)) # Histogram ggplot(data, aes(x = continuous_variable)) + geom_histogram(binwidth = 5) # Customisation 1 # ggplot(data, aes(x = variable1, y = variable2)) + # geom_point(color = "blue", size = 3, alpha = 0.7) ``` -------------------------------- ### Histogram with Custom Binwidth Source: https://github.com/psyteachr/introdataviz/blob/master/docs/getting-started.html Creates a histogram with a specified bin width, allowing for more control over the data aggregation. This example sets the binwidth to 5. ```R ggplot(dat, aes(x = age)) + geom_histogram(binwidth = 5) ``` -------------------------------- ### Histogram with Custom Colors Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/getting-started.html Customizes the appearance of a histogram by setting the fill color to white and the outline color to black for all bars. This example also sets a binwidth of 1. ```R ggplot(dat, aes(age)) + geom_histogram(binwidth = 1, fill = "white", colour = "black") ``` -------------------------------- ### Histogram with Custom Colors Source: https://github.com/psyteachr/introdataviz/blob/master/docs/getting-started.html Customizes the appearance of a histogram by setting the fill color to white and the outline color to black for all bars. This example also sets a binwidth of 1. ```R ggplot(dat, aes(age)) + geom_histogram(binwidth = 1, fill = "white", colour = "black") ``` -------------------------------- ### R Data Visualization Setup and Basics Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/chapter-3.html This section covers the fundamental steps for setting up R and RStudio for data visualization. It includes loading necessary packages, loading data into R, and handling different data types like numeric factors. Basic plotting functions such as bar charts and histograms are also introduced, along with initial customization techniques. ```R library(ggplot2) # Example of loading a package # Example of loading data (assuming a CSV file) data <- read.csv("your_data.csv") # Example of creating a bar chart of counts ggplot(data, aes(x = category)) + geom_bar() # Example of creating a histogram ggplot(data, aes(x = value)) + geom_histogram(binwidth = 5) ``` -------------------------------- ### Interactive Web Visualization Setup Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/chapter-3.html Configures GitBook for interactive features, including social sharing, font settings, and table of contents. This script initializes the GitBook environment for the project. ```javascript gitbook.require("gitbook", function(gitbook) { gitbook.start({ "sharing": { "github": false, "facebook": true, "twitter": true, "linkedin": false, "weibo": false, "instapaper": false, "vk": false, "whatsapp": false, "all": ["facebook", "google", "twitter", "linkedin", "weibo", "instapaper"], "google": false }, "fontsettings": { "theme": "white", "family": "sans", "size": 2 }, "edit": { "link": null, "text": null }, "history": { "link": null, "text": null }, "view": { "link": null, "text": null }, "download": {}, "toc": { "collapse": "section", "scroll_highlight": true } }); }); ``` -------------------------------- ### Error Example: Incorrect Scale Mapping Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/02-ch2.md This R code snippet illustrates a common error in `ggplot2` where an incorrect scale function is mapped to a variable. Using `scale_x_continuous` for a discrete variable results in the error 'Discrete value supplied to continuous scale'. ```r # produces an error ggplot(dat, aes(language)) + geom_bar() + scale_x_continuous(name = "Language group", labels = c("Monolingual", "Bilingual")) ``` -------------------------------- ### Error Example: Incorrect Scale Mapping Source: https://github.com/psyteachr/introdataviz/blob/master/docs/02-ch2.md This R code snippet illustrates a common error in `ggplot2` where an incorrect scale function is mapped to a variable. Using `scale_x_continuous` for a discrete variable results in the error 'Discrete value supplied to continuous scale'. ```r # produces an error ggplot(dat, aes(language)) + geom_bar() + scale_x_continuous(name = "Language group", labels = c("Monolingual", "Bilingual")) ``` -------------------------------- ### Load introdataviz Library Source: https://github.com/psyteachr/introdataviz/blob/master/README.md Demonstrates how to load the installed introdataviz package into the current R session, making its functions available for use. ```r library(introdataviz) ``` -------------------------------- ### Gitbook Initialization Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/chapter-5.html Initializes Gitbook with specified sharing, font, and navigation settings. ```javascript gitbook.require(["gitbook"], function(gitbook) { gitbook.start({ "sharing": { "github": false, "facebook": true, "twitter": true, "linkedin": false, "weibo": false, "instapaper": false, "vk": false, "whatsapp": false, "all": ["facebook", "google", "twitter", "linkedin", "weibo", "instapaper"], "google": false }, "fontsettings": { "theme": "white", "family": "sans", "size": 2 }, "edit": { "link": null, "text": null }, "history": { "link": null, "text": null }, "view": { "link": null, "text": null }, "download": {}, "toc": { "collapse": "section", "scroll_highlight": true } }); }); ``` -------------------------------- ### Error Example: Using Continuous Scale for Discrete Data Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/getting-started.html Illustrates a common error in ggplot2 where a continuous scale function is incorrectly applied to a discrete variable, resulting in the 'Discrete value supplied to continuous scale' error. This highlights the importance of matching scale types to data types. ```R # produces an error ggplot(dat, aes(language)) + geom_bar() + scale_x_continuous(name = "Language group", labels = c("Monolingual", "Bilingual")) ``` -------------------------------- ### Data Visualization with R - Basic Example Source: https://github.com/psyteachr/introdataviz/blob/master/NEWS.md A foundational example showing how to visualize data using R, potentially including functions like `head()` to display the first few rows of a data frame. This helps users understand the structure of the data being plotted. ```R # Assuming 'my_data' is a data frame # Display the first few rows of the data: # print(head(my_data)) # Example of a simple plot (e.g., scatter plot): # library(ggplot2) # ggplot(my_data, aes(x = column1, y = column2)) + # geom_point() ``` -------------------------------- ### Error Example: Using Continuous Scale for Discrete Data Source: https://github.com/psyteachr/introdataviz/blob/master/docs/getting-started.html Illustrates a common error in ggplot2 where a continuous scale function is incorrectly applied to a discrete variable, resulting in the 'Discrete value supplied to continuous scale' error. This highlights the importance of matching scale types to data types. ```R # produces an error ggplot(dat, aes(language)) + geom_bar() + scale_x_continuous(name = "Language group", labels = c("Monolingual", "Bilingual")) ``` -------------------------------- ### R Markdown Rendering Script Source: https://github.com/psyteachr/introdataviz/blob/master/NEWS.md Provides an example of an R script (`_render.R`) used for rendering Bookdown projects and managing package placement. This script automates the build process for R Markdown-based books. ```R # _render.R # Example of rendering a bookdown project: # bookdown::render_book('index.Rmd', output_format = 'bookdown::gitbook') # Example of moving rendered output to a specific package location: # file.copy('docs/index.html', '../your_package/inst/doc/index.html', overwrite = TRUE) ``` -------------------------------- ### Themes and Fonts Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/reference-keys.txt Explains how to apply and customize themes using `ggthemes` and how to manage fonts within plots for consistent and professional-looking visualizations. ```R themes-appendix ggthemes fonts setting-a-lab-theme-using-theme ``` -------------------------------- ### GitBook Initialization Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/chapter-2.html Initializes the GitBook environment with specified sharing, font, and navigation settings. ```javascript gitbook.require(["gitbook"], function(gitbook) { gitbook.start({ "sharing": { "github": false, "facebook": true, "twitter": true, "linkedin": false, "weibo": false, "instapaper": false, "vk": false, "whatsapp": false, "all": ["facebook", "google", "twitter", "linkedin", "weibo", "instapaper"], "google": false }, "fontsettings": { "theme": "white", "family": "sans", "size": 2 }, "edit": { "link": null, "text": null }, "history": { "link": null, "text": null }, "view": { "link": null, "text": null }, "download": {}, "toc": { "collapse": "section", "scroll_highlight": true } }); }); ``` -------------------------------- ### R ggplot2 Legend Control with guides() Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/additional-advanced-plots-and-customisation-options.html Illustrates how to control plot legends in ggplot2 using the `guides()` function. This example specifically shows how to remove the fill legend. ```r ggplot(dat_long, aes(x = condition, y= rt, fill = condition)) + geom_violin(alpha = .4) + geom_boxplot(width = .2, fatten = NULL, alpha = .5) + stat_summary(fun = "mean", geom = "point") + stat_summary(fun.data = "mean_se", geom = "errorbar", width = .1) + scale_fill_viridis_d(option = "E") + guides(fill = "none") + theme_minimal() ``` ```r ggplot(dat_long, aes(x = condition, y= rt, fill = condition, color = condition)) + geom_violin(alpha = .4) + geom_boxplot(width = .2, fatten = NULL, alpha = .5) + stat_summary(fun = "mean", geom = "point") + stat_summary(fun.data = "mean_se", geom = "errorbar", width = .1) + scale_fill_viridis_d(option = "E") + guides(fill = "none", color = "none") ``` -------------------------------- ### Open introdataviz Book Source: https://github.com/psyteachr/introdataviz/blob/master/README.md A function to open a local copy of the book associated with the introdataviz package, likely containing tutorials and explanations. ```r introdataviz::book() ``` -------------------------------- ### R ggplot2 Legend Control with guides() Source: https://github.com/psyteachr/introdataviz/blob/master/docs/additional-advanced-plots-and-customisation-options.html Illustrates how to control plot legends in ggplot2 using the `guides()` function. This example specifically shows how to remove the fill legend. ```r ggplot(dat_long, aes(x = condition, y= rt, fill = condition)) + geom_violin(alpha = .4) + geom_boxplot(width = .2, fatten = NULL, alpha = .5) + stat_summary(fun = "mean", geom = "point") + stat_summary(fun.data = "mean_se", geom = "errorbar", width = .1) + scale_fill_viridis_d(option = "E") + guides(fill = "none") + theme_minimal() ``` ```r ggplot(dat_long, aes(x = condition, y= rt, fill = condition, color = condition)) + geom_violin(alpha = .4) + geom_boxplot(width = .2, fatten = NULL, alpha = .5) + stat_summary(fun = "mean", geom = "point") + stat_summary(fun.data = "mean_se", geom = "errorbar", width = .1) + scale_fill_viridis_d(option = "E") + guides(fill = "none", color = "none") ``` -------------------------------- ### Loading Packages in R Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/chapter-2.html Demonstrates how to load necessary R packages for data visualization. This is a fundamental step before using any visualization functions. ```R library(ggplot2) library(dplyr) ``` -------------------------------- ### Calculate Percentages for Bar Chart Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/02-ch2.md Pre-calculates the percentage of participants for each language group. This involves counting occurrences per language and then computing the percentage. ```r dat_percent <- dat %>% count(language) %>% mutate(percent = (n/sum(n)*100)) ``` -------------------------------- ### Setup and Event Handling for Web Elements Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/introduction.html This JavaScript code sets up event listeners for radio group and select elements on the page. It also includes a function to update the total correct count and handles sidebar responsiveness based on window width. ```javascript ups = document.getElementsByClassName("webex-radiogroup"); for (var i = 0; i < radiogroups.length; i++) { radiogroups\[i].onchange = radiogroups_func; } /* set up selects */ var selects = document.getElementsByClassName("webex-select"); for (var i = 0; i < selects.length; i++) { selects[i].onchange = select_func; } update_total_correct(); ``` -------------------------------- ### Gitbook Initialization Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/chapter-6.html Initializes Gitbook with specific configuration options for sharing, font settings, and table of contents. ```gitbook gitbook.require(["gitbook"], function(gitbook) { gitbook.start({ "sharing": { "github": false, "facebook": true, "twitter": true, "linkedin": false, "weibo": false, "instapaper": false, "vk": false, "whatsapp": false, "all": ["facebook", "google", "twitter", "linkedin", "weibo", "instapaper"], "google": false }, "fontsettings": { "theme": "white", "family": "sans", "size": 2 }, "edit": { "link": null, "text": null }, "history": { "link": null, "text": null }, "view": { "link": null, "text": null }, "download": {}, "toc": { "collapse": "section", "scroll_highlight": true } }); }); ``` -------------------------------- ### Histogram with Custom Binwidth Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/02-ch2.md Creates a histogram of ages with a specified bin width. This allows for finer control over the grouping of data points on the x-axis. ```r ggplot(dat, aes(x = age)) + geom_histogram(binwidth = 5) ``` -------------------------------- ### Calculate Percentages for Bar Chart Source: https://github.com/psyteachr/introdataviz/blob/master/docs/02-ch2.md Pre-calculates the percentage of participants for each language group. This involves counting occurrences per language and then computing the percentage. ```r dat_percent <- dat %>% count(language) %>% mutate(percent = (n/sum(n)*100)) ``` -------------------------------- ### Page Initialization and Event Binding Source: https://github.com/psyteachr/introdataviz/blob/master/docs/getting-started.html Initializes interactive elements on page load. This includes setting up click handlers for solution buttons, configuring 'webex-solveme' inputs (disabling autocomplete, setting initial values, attaching event listeners), and setting up change handlers for radio groups and select elements. It also calls `update_total_correct` to set the initial score. ```javascript window.onload = function() { console.log("onload"); /* set up solution buttons */ var buttons = document.getElementsByTagName("button"); for (var i = 0; i < buttons.length; i++) { if (buttons[i].parentElement.classList.contains('webex-solution')) { buttons[i].onclick = b_func; } } /* set up webex-solveme inputs */ var solveme = document.getElementsByClassName("webex-solveme"); for (var i = 0; i < solveme.length; i++) { /* make sure input boxes don't auto-anything */ solveme[i].setAttribute("autocomplete","off"); solveme[i].setAttribute("autocorrect", "off"); solveme[i].setAttribute("autocapitalize", "off"); solveme[i].setAttribute("spellcheck", "false"); solveme[i].value = ""; /* adjust answer for ignorecase or nospaces */ var cl = solveme[i].classList; var real_answer = solveme[i].dataset.answer; if (cl.contains("ignorecase")) { real_answer = real_answer.toLowerCase(); } if (cl.contains("nospaces")) { real_answer = real_answer.replace(/ /g, ""); } solveme[i].dataset.answer = real_answer; /* attach checking function */ solveme[i].onkeyup = solveme_func; solveme[i].onchange = solveme_func; } /* set up radiogroups */ var radiogroups = document.getElementsByClassName("webex-radiogroup"); for (var i = 0; i < radiogroups.length; i++) { radiogroups[i].onchange = radiogroups_func; } /* set up selects */ var selects = document.getElementsByClassName("webex-select"); for (var i = 0; i < selects.length; i++) { selects[i].onchange = select_func; } update_total_correct(); } ``` -------------------------------- ### R Data Visualization Examples Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/chapter-6.html This section contains R code snippets demonstrating various data visualization techniques. It covers loading packages, loading data, handling numeric factors, creating bar charts of counts, plotting existing aggregates and percentages, histograms, and plot customization. ```R library(tidyverse) # Load data data <- read_csv("your_data.csv") # Handle numeric factors data <- data %>% mutate(numeric_factor = as.numeric(factor_variable)) # Bar chart of counts count_plot <- data %>% count(categorical_variable) %>% ggplot(aes(x = categorical_variable, y = n)) + geom_bar(stat = "identity") # Plotting existing aggregates and percent aggregate_plot <- data %>% group_by(grouping_variable) %>% summarise(mean_value = mean(numeric_variable), percent_positive = sum(numeric_variable > 0) / n()) %>% ggplot(aes(x = grouping_variable, y = mean_value)) + geom_bar(stat = "identity") # Histogram histogram_plot <- ggplot(data, aes(x = numeric_variable)) + geom_histogram(binwidth = 5) # Customization example custom_plot <- ggplot(data, aes(x = variable1, y = variable2, color = factor_variable)) + geom_point() + labs(title = "Custom Plot", x = "Variable 1", y = "Variable 2", color = "Factor") + theme_minimal() ``` -------------------------------- ### Histogram with Custom Binwidth Source: https://github.com/psyteachr/introdataviz/blob/master/docs/02-ch2.md Creates a histogram of ages with a specified bin width. This allows for finer control over the grouping of data points on the x-axis. ```r ggplot(dat, aes(x = age)) + geom_histogram(binwidth = 5) ``` -------------------------------- ### Histogram of Ages Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/02-ch2.md Generates a histogram to visualize the distribution of ages. It maps the 'age' variable to the x-axis and uses geom_histogram() to automatically bin and count observations. ```r ggplot(dat, aes(x = age)) + geom_histogram() ``` -------------------------------- ### Initialize Interactive Elements Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/chapter-6.html Sets up event listeners for solution buttons and 'webex-solveme' input fields when the page loads. It configures input fields for optimal user experience and attaches the answer checking function. ```javascript window.onload = function() { /* set up solution buttons */ var buttons = document.getElementsByTagName("button"); for (var i = 0; i < buttons.length; i++) { if (buttons[i].parentElement.classList.contains('webex-solution')) { buttons[i].onclick = b_func; } } /* set up webex-solveme inputs */ var solveme = document.getElementsByClassName("webex-solveme"); for (var i = 0; i < solveme.length; i++) { /* make sure input boxes don't auto-anything */ solveme[i].setAttribute("autocomplete","off"); solveme[i].setAttribute("autocorrect", "off"); solveme[i].setAttribute("autocapitalize", "off"); solveme[i].setAttribute("spellcheck", "false"); solveme[i].value = ""; /* adjust answer for ignorecase or nospaces */ var cl = solveme[i].classList; var real_answer = solveme[i].dataset.answer; if (cl.contains("ignorecase")) { real_answer = real_answer.toLowerCase(); } if (cl.contains("nospaces")) { real_answer = real_answer.replace(/ /g, ""); } solveme[i].dataset.answer = real_answer; /* attach checking function */ solveme[i].onkeyup = solveme_func; solveme[i].onchange = solveme_func; } update_total_correct(); } ``` -------------------------------- ### Setup and Event Handling for Web Elements Source: https://github.com/psyteachr/introdataviz/blob/master/docs/introduction.html This JavaScript code sets up event listeners for radio group and select elements on the page. It also includes a function to update the total correct count and handles sidebar responsiveness based on window width. ```javascript ups = document.getElementsByClassName("webex-radiogroup"); for (var i = 0; i < radiogroups.length; i++) { radiogroups\[i].onchange = radiogroups_func; } /* set up selects */ var selects = document.getElementsByClassName("webex-select"); for (var i = 0; i < selects.length; i++) { selects[i].onchange = select_func; } update_total_correct(); ``` -------------------------------- ### Toggle Solution Visibility Source: https://github.com/psyteachr/introdataviz/blob/master/docs/getting-started.html Toggles the 'open' class on the parent element of the clicked button, which is used to show or hide solution explanations. ```javascript b_func = function() { console.log("webex: toggle hide"); var cl = this.parentElement.classList; if (cl.contains('open')) { cl.remove("open"); } else { cl.add("open"); } } ``` -------------------------------- ### Histogram of Ages Source: https://github.com/psyteachr/introdataviz/blob/master/docs/02-ch2.md Generates a histogram to visualize the distribution of ages. It maps the 'age' variable to the x-axis and uses geom_histogram() to automatically bin and count observations. ```r ggplot(dat, aes(x = age)) + geom_histogram() ``` -------------------------------- ### Webex Initialization and Event Binding (JavaScript) Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/additional-resources.html Handles the initialization of Webex quiz elements when the page loads. It sets up event listeners for solution buttons, 'solveme' inputs, radio groups, and select elements, and then updates the total correct count. ```javascript window.onload = function() { console.log("onload"); /* set up solution buttons */ var buttons = document.getElementsByTagName("button"); for (var i = 0; i < buttons.length; i++) { if (buttons[i].parentElement.classList.contains('webex-solution')) { buttons[i].onclick = b_func; } } /* set up webex-solveme inputs */ var solveme = document.getElementsByClassName("webex-solveme"); for (var i = 0; i < solveme.length; i++) { /* make sure input boxes don't auto-anything */ solveme[i].setAttribute("autocomplete","off"); solveme[i].setAttribute("autocorrect", "off"); solveme[i].setAttribute("autocapitalize", "off"); solveme[i].setAttribute("spellcheck", "false"); solveme[i].value = ""; /* adjust answer for ignorecase or nospaces */ var cl = solveme[i].classList; var real_answer = solveme[i].dataset.answer; if (cl.contains("ignorecase")) { real_answer = real_answer.toLowerCase(); } if (cl.contains("nospaces")) { real_answer = real_answer.replace(/ /g, ""); } solveme[i].dataset.answer = real_answer; /* attach checking function */ solveme[i].onkeyup = solveme_func; solveme[i].onchange = solveme_func; } /* set up radiogroups */ var radiogroups = document.getElementsByClassName("webex-radiogroup"); for (var i = 0; i < radiogroups.length; i++) { radiogroups[i].onchange = radiogroups_func; } /* set up selects */ var selects = document.getElementsByClassName("webex-select"); for (var i = 0; i < selects.length; i++) { selects[i].onchange = select_func; } update_total_correct(); } ``` -------------------------------- ### Initialize Interactive Elements Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/getting-started.html Sets up event listeners for various interactive elements on page load. This includes attaching click handlers to solution buttons, keyup/change handlers to 'solveme' inputs, and change handlers to radio groups and select elements. ```javascript window.onload = function() { console.log("onload"); /* set up solution buttons */ var buttons = document.getElementsByTagName("button"); for (var i = 0; i < buttons.length; i++) { if (buttons[i].parentElement.classList.contains('webex-solution')) { buttons[i].onclick = b_func; } } /* set up webex-solveme inputs */ var solveme = document.getElementsByClassName("webex-solveme"); for (var i = 0; i < solveme.length; i++) { /* make sure input boxes don't auto-anything */ solveme[i].setAttribute("autocomplete","off"); solveme[i].setAttribute("autocorrect", "off"); solveme[i].setAttribute("autocapitalize", "off"); solveme[i].setAttribute("spellcheck", "false"); solveme[i].value = ""; /* adjust answer for ignorecase or nospaces */ var cl = solveme[i].classList; var real_answer = solveme[i].dataset.answer; if (cl.contains("ignorecase")) { real_answer = real_answer.toLowerCase(); } if (cl.contains("nospaces")) { real_answer = real_answer.replace(/ /g, ""); } solveme[i].dataset.answer = real_answer; /* attach checking function */ solveme[i].onkeyup = solveme_func; solveme[i].onchange = solveme_func; } /* set up radiogroups */ var radiogroups = document.getElementsByClassName("webex-radiogroup"); for (var i = 0; i < radiogroups.length; i++) { radiogroups[i].onchange = radiogroups_func; } /* set up selects */ var selects = document.getElementsByClassName("webex-select"); for (var i = 0; i < selects.length; i++) { selects[i].onchange = select_func; } update_total_correct(); } ``` -------------------------------- ### Check Select Answers Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/getting-started.html Checks the selected option in a dropdown menu against the expected answer. It applies 'webex-correct' or 'webex-incorrect' classes based on the selection. ```javascript select_func = function(e) { console.log("webex: check select"); var cl = this.classList /* add style */ cl.remove("webex-incorrect"); cl.remove("webex-correct"); if (this.value == "answer") { cl.add("webex-correct"); } else if (this.value != "blank") { cl.add("webex-incorrect"); } update_total_correct(); } ``` -------------------------------- ### Creating Histograms and Density Plots Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/chapter-3.html Covers the creation of histograms and density plots for visualizing data distributions. Includes examples for grouped density plots to compare distributions across different categories. ```R library(ggplot2) # Histogram example # ggplot(data, aes(x = variable)) + geom_histogram() # Density plot example # ggplot(data, aes(x = variable)) + geom_density() # Grouped density plot example # ggplot(data, aes(x = variable, fill = category)) + geom_density(alpha = 0.5) ``` -------------------------------- ### Total Observation Count in R Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/getting-started.html Calculates the total number of observations in the dataset using dplyr's pipe operator and count function. ```r dat %>% count() ``` -------------------------------- ### Summarise Data in R Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/getting-started.html Calculates descriptive statistics, such as mean age and standard deviation, for a dataset using dplyr's summarise function. ```r dat %>% summarise(mean_age = mean(age), sd_age = sd(age)) ``` -------------------------------- ### R Data Visualization Tutorial - Navigation Source: https://github.com/psyteachr/introdataviz/blob/master/docs/index.html HTML links for navigating the R data visualization tutorial. Includes links to different sections of the tutorial and appendices. ```HTML [Skip to main content](#content) [Data visualisation using R, for researchers who don’t use R](index.html) ========================================================================= Show table of contents Table of contents ----------------- * [Overview](index.html) * [1 Introduction](introduction.html) * [2 Getting Started](getting-started.html) * [3 Transforming Data](transforming-data.html) * [4 Representing Summary Statistics](representing-summary-statistics.html) * [5 Multi-part Plots](multi-part-plots.html) * [6 Advanced Plots](advanced-plots.html) * [7 Conclusion](conclusion.html) * Appendices * [A Additional resources](additional-resources.html) * [B Additional customisation options](additional-customisation-options.html) * [C Styling Plots](plotstyle.html) * [D Advanced Plots](advanced-plots-1.html) * [License](license.html) * [References](references.html) [View book source](https://github.com/psyteachr/introdataviz) Overview[](#overview) ===================== 0.1 Citing[](#citing) --------------------- Please cite both the preprint and interactive online tutorial as: Nordmann, E., McAleer, P., Toivo, W., Paterson, H. & DeBruine, L. (2022). Data visualisation using R, for researchers who don't use R. Advances in Methods and Practices in Psychological Science. [https://doi.org/10.1177/25152459221074654](https://doi.org/10.1177/25152459221074654) ``` -------------------------------- ### Change Bar Chart Bar Color Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/02-ch2.md This code changes the fill color of the bars in a ggplot bar chart to red. It maps the 'language' variable to the x-axis. ```r ggplot(data = dat, mapping = aes(x = language)) + geom_bar(fill = "red") ``` -------------------------------- ### Apply theme_minimal() to Histogram Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/02-ch2.md This code snippet demonstrates how to apply the `theme_minimal()` to a ggplot histogram, updating axis labels for age and count. It requires the `ggplot2` library. ```r ggplot(dat, aes(age)) + geom_histogram(binwidth = 1, fill = "wheat", color = "black") + scale_x_continuous(name = "Participant age (years)") + theme_minimal() ``` -------------------------------- ### Total Observation Count in R Source: https://github.com/psyteachr/introdataviz/blob/master/docs/getting-started.html Calculates the total number of observations in the dataset using dplyr's pipe operator and count function. ```r dat %>% count() ``` -------------------------------- ### jQuery for Footer and Sidebar Setup Source: https://github.com/psyteachr/introdataviz/blob/master/docs/license.html This jQuery code runs when the document is ready. It sets up the footer content with links to the psyTeachR project and a Creative Commons license. It also implements a function to dynamically move the table of contents (TOC) sidebar based on the window width, ensuring better responsiveness. ```javascript $( document ).ready(function() { var cite = ' '; var psyteachr = ' psyTeachR: Reproducible Research'; var license = ' Creative Commons License'; $("footer div.row div:eq(1) p").html( psyteachr + license + cite ); function move_sidebar() { var w = window.innerWidth; if (w < 992) { $("#toc").appendTo($("#main-nav")); } else { $("#toc").appendTo($("div.sidebar-chapter")); } } move_sidebar(); window.onresize = move_sidebar; }); ``` -------------------------------- ### Summarise Data in R Source: https://github.com/psyteachr/introdataviz/blob/master/docs/getting-started.html Calculates descriptive statistics, such as mean age and standard deviation, for a dataset using dplyr's summarise function. ```r dat %>% summarise(mean_age = mean(age), sd_age = sd(age)) ``` -------------------------------- ### Basic Plot Setup Source: https://github.com/psyteachr/introdataviz/blob/master/inst/book/appendix-c-style.md Generates a basic ggplot using the diamonds dataset, demonstrating facets, smooth lines, and labels. This serves as a base for theme customization. ```r g <- ggplot(diamonds, aes(x = carat, y = price, color = cut)) + facet_wrap(~color, nrow = 2) + geom_smooth(method = lm, formula = y~x) + labs(title = "The relationship between carat and price", subtitle = "For each level of color and cut", caption = "Data from ggplot2::diamonds") g ```