scieee AI-readable full text Open interactive document viewer

Visualizing and Reporting Educational Data with R

López-Pernas, Sonsoles,Misiejuk, Kamila,Tikka, Santtu,Kopra, Juho,Heinäniemi, Merja,Saqr, Mohammed

Full text

This is a self-archived version of an original article. This version may differ from the original in pagination and typographic details. Author(s): Title: Year: Version: Copyright: Rights: Rights url: Please cite the original version: CC BY 4.0 https://creativecommons.org/licenses/by/4.0/ Visualizing and Reporting Educational Data with R © The Author(s) 2024 Published version López-Pernas, Sonsoles; Misiejuk, Kamila; Tikka, Santtu; Kopra, Juho; Heinäniemi, Merja; Saqr, Mohammed López-Pernas, S., Misiejuk, K., Tikka, S., Kopra, J., Heinäniemi, M., & Saqr, M. (2024). Visualizing and Reporting Educational Data with R. In M. Saqr, & S. López-Pernas (Eds.), Learning Analytics Methods and Tutorials : A Practical Guide Using R (pp. 151-194). Springer. https://doi.org/10.1007/978-3-031-54464-4_6 2024 Visualizing and Reporting Educational Data with R Sonsoles López-Pernas, Kamila Misiejuk, Santtu Tikka, Juho Kopra, Merja Heinäniemi, and Mohammed Saqr 1 Introduction Data visualization can be defined as “the representation and presentation of data that exploits our visual perception abilities in order to amplify cognition” [1]. It has the power to transform complex information into stories that inform and inspire action. Data visualization is an effective tool for learning analytics, as it helps to present learners’ data in a way that is easily understandable and intuitive for students, teachers, researchers, and other stakeholders. Through the use of graphs, charts, and other visual aids, it is possible to quickly identify patterns, trends, and relationships within data that may not be immediately apparent through purely numerical data analysis methods. Visualization in learning analytics has two distinct applications. On the one hand, the use of visual dashboards has become the main vehicle for putting learning analytics into practice. Presenting data in visually appealing and intuitive ways can help promote data literacy among students and other stakeholders, encouraging S. López-Pernas () · J. Kopra School of Computing, University of Eastern Finland, Joensuu, Finland e-mail: sonsoles.lopez@uef.fi K. Misiejuk Centre for the Science of Learning & Technology (SLATE), University of Bergen, Bergen, Norway S. Tikka Department of Mathematics and Statistics, University of Jyväskylä, Jyväskylä, Finland M. Heinäniemi Institute of Biomedicine, University of Eastern Finland, Kuopio, Finland M. Saqr School of Computing, University of Eastern Finland, Joensuu, Finland © The Author(s) 2024 M. Saqr, S. López-Pernas (eds.), Learning Analytics Methods and Tutorials, https://doi.org/10.1007/978-3-031-54464-4_6 151 152 S. López-Pernas et al. greater engagement with data and fostering a culture of continuous improvement. On the other hand, learning analytics scientific production heavily relies on data visualization to present research findings in a clear and accessible manner, making it easier for readers from different scholarly backgrounds to understand and act upon research insights. Regardless of the context, the power of visualization in learning analytics lies in its ability to take complex data and turn it into meaningful insights that support better decision-making and drive improvement. In this chapter, the reader will be guided through the process of generating meaningful and aesthetically pleasing visualizations of different types of datasets using well-known R packages. Relevant plots and plot types will be demonstrated with an explanation of their usage and usage cases. Furthermore, learning-related examples will be discussed in detail. For instance, readers will learn how to visualize learners’ logs extracted from learning management systems (LMSs) to show how trace data can be used to track students’ learning activities. Other examples of common research scenarios in which learners’ data are visualized will be illustrated throughout the chapter. In addition to creating compelling plots, readers will also be able to generate professional-looking tables with summary statistics to report descriptive statistics. 2 Visualization in Learning Analytics Developing visualizations is a challenging task of balancing the cognitive load of users while not compromising on conveying specific insights from data [2]. Visualizations for practice in learning analytics are mostly developed for two main stakeholders: learners and instructors. Depending on the target group, a visualization or a dashboard (i.e., a collection of visualizations depicting multiple indicators) have different goals. Learner-facing visualizations are meant to make learners aware of their own learning and to provide them with actionable feedback on their learning. Visualizations display learners’ performance on a specific metric and compare it with a reference frame: other peers, desirable learning achievement, or their own progress over time [3]. Sense-making questions triggering reflection can be added to a visualization [4, 5], or some elements of the visualizations can be highlighted and described in words using layered storytelling [6, 7]. Another option is to gamify a dashboard, for example, by using badges [8]. To provide feedback to learners, visualizations can be augmented with links to recommended resources [9], information about specific topics to review to close the achievement gap [6], or explanations of the meaning of visualizations and their implications for the learner [10]. Current learner-facing dashboards mostly show resource use and assessment data [11], compare learners to their peers [12], display descriptive analytics rather than predictive or prescriptive analytics [10], and use self-regulated learning theory as their framework [12, 13]. Some reviews found a positive effect on student outcomes [10], while others reported mixed results [11, 14]. Showing Visualizing and Reporting Educational Data with R 153 visualizations to learners can change their behavior. For example, social network analysis visualizations have resulted in fewer cross-group commenting [15], while a visualization comparing individual submission patterns with the top 25% of students in a class led to earlier homework submissions [16]. In comparison, the goal of instructor-facing visualizations is to support teachers and their decision-making process by tracking student progress. Two main types can be distinguished. Mirroring or descriptive visualizations provide insights about the learners on an aggregated or an individual level using either descriptive or comparative data. Advising or prescriptive visualizations show not only information about the learners but also alert the instructor to undertake a pedagogical action [17, 18]. Current instructor-facing visualizations mostly display course-wide information about the learners or track group work [14]. These visualizations can support teachers in facilitating student collaboration [19], planning and collecting student feedback on learning activities [20], or obtaining insights into student interactions within an online environment, such as simulations, virtual labs or online games [21, 22]. However, interpreting dashboard information is a challenging task for instructors. Although some teachers use dashboards as complementary sources of information, others act based only on the dashboard information without further investigation [23]. A common point of criticism of learning analytics dashboards is that most of them are not grounded in learning theories [13,14]. Data-driven evaluations of dashboards focused on dashboard acceptance, usefulness, or usability are more prevalent than pedagogically-focused evaluations [24]. Some approaches were developed to mitigate these issues. The model of user-centered learning analytics systems (MULAS) presents a set of recommendations on four interconnected dimensions: theory, design, evaluation, and feedback, and can be used to guide dashboard development [14]. Another approach is an iterative five-stage Learning Awareness Tools—User eXperience (LATUX) workflow, including problem identification, low-fidelity prototyping, high-fidelity prototyping, pilot studies, and classroom use, that can be used to develop visual analytics [25]. Finally, open learner model research could be used as a source of insights while developing learning analytics visualizations, such as dashboards [9]. 3 Generating Plots with ggplot2 In the previous section, we have seen how central visualization is to learning analytics. In the remainder of the chapter, we will learn how to create different types of visualizations that are relevant to different types of data related to teaching and learning. We will mostly rely on ggplot2, a popular data visualization package in R that was developed by Hadley Wickham [26]. It is based on the grammar of graphics [27], which is a systematic way of thinking about and constructing visualizations. The ggplot2 library provides a flexible and intuitive framework for creating a wide range of graphics, from basic scatter plots to complex visualizations with multiple 154 S. López-Pernas et al. layers. It is known for its ability to produce visually appealing and informative graphics with relatively few lines of code. It enables users to define aesthetics, such as color and size, and add layers, such as points and lines, to create customized and interactive plots. In addition, ggplot2 allows for easy customization of plot features, such as titles, axis labels, and legends. Overall, ggplot2 is a powerful and versatile tool for data visualization in R, and is widely used by data scientists, statisticians, and researchers in a variety of fields. In this chapter, we will cover the fundamental concepts and techniques of ggplot2, including how to create basic plots, and customize their appearance. We will start by introducing the building blocks of a ggplot2 plot, including aesthetics, layers, and scales. Then, we will create a plot from scratch step by step, showing how to customize its appearance, including how to change theme, colors, and scales. We will then explore the different types of plots that can be created with ggplot2, such as scatter plots, bar charts, and histograms. Throughout this section, we will use datasets of students’ learning data to demonstrate how to create effective visualizations for learning analytics with ggplot2. Please, refer to Chapter 2 of this book [28] to learn more about the datasets used. By the end of this section, you will have a solid foundation in ggplot2 and be able to create basic, yet compelling visualizations to explore your data. 3.1 The ggplot2 Grammar The ggplot2 library is based on Wilkinson’s grammar of graphics [27]. The main idea is that every plot can be broken down into a set of components, each of which can be customized and combined in a flexible way. These components are: • Data: This is the data we want to visualize. It can be in the form of a dataframe, tibble or any other structured data format. • Aesthetic mapping (aes): It defines how variables in the data are mapped to visual properties of the plot, such as position, color, shape, size, and transparency. • Geometric object (geom): It represents the actual visual elements of the plot, such as points, lines, bars, and polygons. • Statistical transformation (stat): It summarizes or transforms the data in some way, such as by computing means, medians, or proportions, or by smoothing or summarizing data, or grouping them into bins. • Scale (scale): It maps values in the data to visual properties of the plot, such as color, size, or position. • Coordinate system (coord): It defines the spatial or geographic context in which the plot is displayed, such as Cartesian coordinates, polar coordinates, or maps. • Facet (facet): It allows to split the data into subsets and display each subset in a separate panel. It often useful for visualizing data with multiple categories or groups. Visualizing and Reporting Educational Data with R 155 Through the combination and customization of these components, we can create a wide variety of complex and informative visualizations in ggplot2. The idea behind the graphics grammar is to provide a consistent framework for constructing plots, allowing users to focus on the data and the message they want to convey, rather than on the technical details of the visualization. In the following section, we will create a plot from scratch step by step to become familiar with the most relevant components. 3.2 Creating Your First Plot We will now create our first plot using ggplot2. Our example deals with a widely studied matter in learning analytics, which is the relationship between online activity and achievement. We will use a bar chart to represent the number of students that have low, moderate and high activity levels in each achievement group (high achievers vs. low achievers). In order to become familiar with the syntax of ggplot2, we will recreate the plot step by step, explaining each of the elements in the plot. Below is the final result we aim at accomplishing (Fig. 1): 0 10 20 30 40 50 60 Low achiever High achiever Achievement group Number of students Activity level Low activity Moderate activity High activity Activity level by achievement group Fig. 1 First plot with ggplot2 156 S. López-Pernas et al. 3.2.1 Installing ggplot2 Our first step is installing the ggplot2 library. This is usually the first step in any R script that makes use of external libraries. install.packages("ggplot2") To import ggplot2 we just need to use the library command and specify the ggplot2 library: library(ggplot2) 3.2.2 Downloading the Data Next, we need to import the data that we are going to plot. For this chapter, we are using synthetic data from a blended course on learning analytics. For more details about this dataset, refer to Chap. 2 in this book. The data is in Excel format. We can use the library rio since it makes it easy to read data in several formats. We first install the library: install.packages("rio") And import it so we can use its functions: library(rio) Now we can download the data using the import function from rio and assign it to a variable named df (short for dataframe). demo_url = "https://github.com/lamethods/data/raw/main/1_moodleLAcourse/AllCombined.xlsx" df <- import(demo_url) We can use the head command to get an idea of what the dataset looks like. To recreate the plot above we will need the AchievingGroup column —which indicates whether students’ are high achievers (to 50%) or low achievers (bottom 50%), according to their final grade— and the ActivityGroup column —which indicates whether students have a high level of activity (top 33%), moderate activity (middle 33%), or low activity (bottom 33%), according to their total number of events in the LMS. Visualizing and Reporting Educational Data with R 157 head(df) # A tibble: 130 x 37 User Name Gender ActivityGroup AchievingGroup Surname Origin Birthdate <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> 1 00a05cc62 Wan M Low activity Low achiever Tan Malay~ 12.12.19~ 2 042b07ba1 Daniel M High activity Low achiever Tromp Aruba 28.5.1999 3 046c35846 Sarah F Low activity Low achiever Schmit Luxem~ 25.4.1997 4 05b604102 Lian F Low activity Low achiever Abdull~ Yemen 19.11.19~ 5 0604ff3d3 Nina F Low activity Low achiever Borg Malta 13.6.1994 6 077584d71 Moham~ M High activity High achiever Gamal Egypt 13.7.1998 7 081b100cf Maxim~ M Moderate act~ High achiever Gruber Austr~ 20.12.19~ 8 0857b3d8e Hugo M High activity High achiever Pérez Spain 22.12.19~ 9 0af619e4b Aylin F Low activity Low achiever Barat Kazak~ 14.8.1995 10 0ec99ce96 Polina F Moderate act~ Low achiever Novik Belar~ 9.10.1996 # i 120 more rows # i 29 more variables: Location <chr>, Employment <chr>, # Frequency.Applications <dbl>, Frequency.Assignment <dbl>, # Frequency.Course_view <dbl>, Frequency.Feedback <dbl>, # Frequency.General <dbl>, Frequency.Group_work <dbl>, # Frequency.Instructions <dbl>, Frequency.La_types <dbl>, # Frequency.Practicals <dbl>, Frequency.Social <dbl>, ... 3.2.3 Creating the Aesthetic Mapping Now that we have our data, we can pass it on to ggplot2 as follows: ggplot(df) Fig. 2 Empty plot We still do not see anything because we have not selected the type of chart or the variables of the data that we want to plot (Fig. 2). First, let us specify that we want to plot the AchievingGroup column (high vs. low achievers) on the xaxis. Assigning columns of our dataset to different elements of the plot is called constructing an aesthetic mapping. We can do it by calling the aes function from ggplot2, specifying that we want to map the AchievingGroup column to the x- 158 S. López-Pernas et al. axis, and then passing this call to aes to our plot using the second argument of ggplot: 3.2.4 Add the Geometry Component ggplot(df, aes(x= AchievingGroup)) Fig. 3 Empty plot with AchievingGroup in x-axis labels High achiever Low achiever AchievingGroup We now see that the x-axis has the two possible values of AchievingGroup: “High achiever” and “Low achiever” (Fig.3). We still need to tell ggplot2 the type of chart we want to use to plot the number of students of each type. To do that we need to add a geometrical (geom) component to our plot in which we specify that we want a bar chart. We do it by adding a + sign after our call to ggplot and calling geom_bar() (the name of the geometry that represents a bar chart). ggplot(df, aes(x= AchievingGroup)) + geom_bar() 0 20 40 60 High achiever Low achiever AchievingGroup count Fig. 4 Basic bar plot showing students by achievement group Visualizing and Reporting Educational Data with R 165 documentation and even more third-party implementations. To recreate our goal plot, we select the theme_minimal. To avoid having to add the theme to all of our plots from now on, we can set a default theme for our whole project by using theme_set: theme_set(theme_minimal()) Notice how now we get theme_minimal even when we do not specify it in our code (Fig. 14): ggplot(df, aes(x= AchievingGroup, fill = ActivityGroup)) + geom_bar(position = position_stack(reverse = TRUE)) + scale_fill_viridis_d(direction =-1) 0 20 40 60 Low achiever High achiever AchievingGroup count ActivityGroup Low activity Moderate activity High activity Fig. 14 Bar plot with theme minimal by default 3.2.7 Changing the Axis Ticks You may have not noticed that another difference with our goal plot is the ticks in our y-axis. In the goal plot we count 10 by 10, whereas in our last plot we do so 20 by 20. Just like we modified the scale of the fill aesthetic when we changed the color of our bars, we can also modify the y aesthetic to adjust to our needs. We use the scale_y_continuous layer and we try different number of breaks (n.breaks), until we find what we like best (Fig. 15): 166 S. López-Pernas et al. ggplot(df, aes(x= AchievingGroup, fill = ActivityGroup)) + geom_bar(position = position_stack(reverse = TRUE)) + scale_fill_viridis_d(direction =-1) + scale_y_continuous(n.breaks = 15) ggplot(df, aes(x= AchievingGroup, fill = ActivityGroup)) + geom_bar(position = position_stack(reverse = TRUE)) + scale_fill_viridis_d(direction =-1) + scale_y_continuous(n.breaks = 3) ggplot(df, aes(x= AchievingGroup, fill = ActivityGroup)) + geom_bar(position = position_stack(reverse = TRUE)) + scale_fill_viridis_d(direction =-1) + scale_y_continuous(n.breaks = 7) 0 5 10 15 20 25 30 35 40 45 50 55 60 65 Low achiever High achiever AchievingGroup count ActivityGroup Low activity Moderate activity High activity 0 30 60 Low achiever High achiever AchievingGroup count ActivityGroup Low activity Moderate activity High activity 0 10 20 30 40 50 60 Low achiever High achiever AchievingGroup count ActivityGroup Low activity Moderate activity High activity Fig. 15 Bar plot with different numbers of y.axis breaks: 15 (left), 3 (middle), and 7 (right) We choose 7 breaks to obtain our desired result. 3.2.8 Titles and Labels Our plot is still missing some slight modifications to be 100% equal to the original one. For instance, the axes’ titles are not the same. To specify the y-axis label, we add a new layer to our plot named ylab and we pass a string with our desired label “Number of students” (Fig. 16): ggplot(df, aes(x= AchievingGroup, fill = ActivityGroup)) + geom_bar(position = position_stack(reverse = TRUE)) + scale_fill_viridis_d(direction =-1) + scale_y_continuous(n.breaks = 7) + ylab("Number of students") Visualizing and Reporting Educational Data with R 167 0 10 20 30 40 50 60 Low achiever High achiever AchievingGroup Number of students ActivityGroup Low activity Moderate activity High activity Fig. 16 Bar plot with y-axis label We do the same for the x-axis using xlab, and for the legend using labs (Fig. 17): ggplot(df, aes(x= AchievingGroup, fill = ActivityGroup)) + geom_bar(position = position_stack(reverse = TRUE)) + scale_fill_viridis_d(direction =-1) + scale_y_continuous(n.breaks = 7) + ylab("Number of students") + xlab("Achievement group") + labs(fill = "Activity level") 0 10 20 30 40 50 60 Low achiever High achiever Achievement group Number of students Activity level Low activity Moderate activity High activity Fig. 17 Bar plot with all labels 168 S. López-Pernas et al. More importantly, we are missing the overall title of the plot. To add it we use ggtitle and we pass our intended plot title “Activity level by achievement group”. Keep in mind that, whenever possible, it is better to add a caption to the image rather than a title on the plot. A caption is more accessible for visually impaired users since it is compatible with screen readers. In scientific papers, it is also more common to have a Figure caption than a title within the plot. In social media, it is frequent to see the title on the plot as images are often shared without context. However, many social media platforms allow to provide an alternative text which is what screen readers will read as a substitute for the image, and that is also the case in learning analytics dashboards (Fig. 18). ggplot(df, aes(x= AchievingGroup, fill = ActivityGroup)) + geom_bar(position = position_stack(reverse = TRUE)) + scale_fill_viridis_d(direction =-1) + scale_y_continuous(n.breaks = 7) + ylab("Number of students") + xlab("Achievement group") + labs(fill = "Activity level") + ggtitle("Activity level by achievement group") 0 10 20 30 40 50 60 Low achiever High achiever Achievement group Number of students Activity level Low activity Moderate activity High activity Activity level by achievement group Fig. 18 Bar plot with title 3.2.9 Other Cosmetic Modifications Lastly, we need to do some slight modifications to the overall appearance of the plot. We do this through the generic theme function of ggplot2. We first modify the position of the legend by setting legend.position to “bottom”. We then increase the size of the axes titles, by setting axis.title to element_text(size = 12). Finally, we make the plot title bigger as well and put it in bold by setting Visualizing and Reporting Educational Data with R 169 plot.title to element_text(size = 15, face = "bold")). With these last changes, we have an exact replica of our original plot (Fig. 19). ggplot(df, aes(x= AchievingGroup, fill = ActivityGroup)) + geom_bar(position = position_stack(reverse = TRUE)) + scale_fill_viridis_d(direction =-1) + scale_y_continuous(n.breaks = 7) + ylab("Number of students") + xlab("Achievement group") + labs(fill = "Activity level") + ggtitle("Activity level by achievement group") + theme(legend.position = "bottom", axis.title = element_text(size = 12), plot.title = element_text(size = 15, face = "bold")) 0 10 20 30 40 50 60 Low achiever High achiever Achievement group Number of students Activity level Low activity Moderate activity High activity Activity level by achievement group Fig. 19 Bar plot with theme modifications 3.2.10 Saving the Plot Since we have obtained the desired result, we may now save it as an image to be able to use it elsewhere. For that, we first need to assign the plot to a variable (e.g., myplot). 170 S. López-Pernas et al. myplot <- ggplot(df, aes(x= AchievingGroup, fill = ActivityGroup)) + geom_bar(position = position_stack(reverse = TRUE)) + scale_fill_viridis_d(direction =-1) + scale_y_continuous(n.breaks = 7) + ylab("Number of students") + xlab("Achievement group") + labs(fill = "Activity level") + ggtitle("Activity level by achievement group") + theme(legend.position = "bottom", axis.title = element_text(size = 12), plot.title = element_text(size = 15, face = "bold")) We then use ggsave to save the plot to our filesystem. We need to specify the file path (including the extension, such as PNG, JPEG, etc.) where we want to save the plot (e.g., “bar.png”) as the first argument and pass the variable where we saved our plot (myplot) as a second argument. If we do not do this, ggplot2 assumes we want to save the latest plot that we created. Lastly, we may specify the width, height and resolution (dpi) of our plots. If we are submitting our figure to a scientific journal, we probably need a high resolution image. If we are using the figure in social media, we do not want the resolution to be so high as it would take a long time to load. ggsave("bar.png", myplot, width = 10000, height = 5000, units = "px", dpi = 900) Throughout this section, we have learned how we can create a plot from scratch using only the ggplot2 library and a simple dataset. We have seen the many customization possibilities (theme, scales, titles) that we can achieve using the different plot components without needing to rely on external tools for retouching our final graph. In the next section we will learn about new types of plots that might be more suitable for other types of data and their customization possibilities. 3.3 Types of Plots The ggplot2 library offers many types of plots (or geoms) that you can choose from to visualize your data in several ways. In this section, we go over some of the most common types and present examples using students’ learning data. 3.3.1 Bar Plot We have seen how to construct a bar plot in the previous section as an example of how to use ggplot2. But when should we use a bar plot? Bar plots are useful when we want to represent counts or any numerical variable broken down by categories. The y-axis would represent the count (or other continuous numerical variable) and the x-axis would represent the categories. Keep in mind that if the categories follow a natural order, the x-axis should respect it (for example: “Morning”, “Afternoon”, “Evening”; or “Children”, “Adults”, “Elders”). Otherwise, you can just order the x-axis alphabetically or from highest to lowest value in the y-axis (Fig. 20). Visualizing and Reporting Educational Data with R 171 ggplot(df, aes(x= AchievingGroup)) + geom_bar(position = position_stack(reverse = TRUE)) 0 20 40 60 Low achiever High achiever AchievingGroup count Fig. 20 Basic bar plot of students by achievement group Remember that you can add a “third dimension” to the plot by using the fill property. This is known as a ‘stacked’ bar chart and helps highlight the proportion of, in this case, students’ activity level (ActivityGroup) (Fig. 21). ggplot(df, aes(x= AchievingGroup, fill = ActivityGroup)) + scale_fill_viridis_d(direction =-1) + geom_bar(position = position_stack(reverse = TRUE)) 0 20 40 60 Low achiever High achiever AchievingGroup count ActivityGroup Low activity Moderate activity High activity Fig. 21 Basic bar plot of students by achievement group filled by activity level 172 S. López-Pernas et al. If we care more about the actual number rather than the proportion of students with each activity level, instead of a stacked bar chart we can keep each ‘stack’ as a whole bar of their own. This plot is very useful to compare values among categories. We accomplish this by passing the position argument with the value “dodge” to the geom_bar component (Fig. 22): ggplot(df, aes(x= AchievingGroup, fill = ActivityGroup)) + scale_fill_viridis_d(direction =-1) + geom_bar(position = "dodge") 0 10 20 30 Low achiever High achiever AchievingGroup count ActivityGroup Low activity Moderate activity High activity Fig. 22 Basic bar plot of students by achievement group filled by activity level with position dodge instead of stacked We can now see that the highest group is represented by the low achievers with low activity, followed by the high achievers with high activity. 3.3.2 Histogram Histograms allow us to represent the distribution of a single continuous variable. It is inherently a bar chart, but instead of each bar representing the count of a single category, it represents the count of a range of values in the x-axis (what is known as a bin). Let us, for example, create a histogram for students’ online activity. Specifically, let us see the distribution of the number of accesses to the course main page online. If we look at our dataset, we can see that the name of the variable that we are interested in is Frequency.Course_view: Visualizing and Reporting Educational Data with R 173 head(df) # A tibble: 130 x 37 User Name Surname Origin Gender Birthdate Location Employment <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> 1 00a05cc62 Wan Tan Malaysia M 12.12.19~ Remote None 2 042b07ba1 Daniel Tromp Aruba M 28.5.1999 Remote None 3 046c35846 Sarah Schmit Luxembourg F 25.4.1997 On camp~ None 4 05b604102 Lian Abdullah Yemen F 19.11.19~ On camp~ None 5 0604ff3d3 Nina Borg Malta F 13.6.1994 On camp~ None 6 077584d71 Mohamed Gamal Egypt M 13.7.1998 On camp~ Part-time 7 081b100cf Maximilian Gruber Austria M 20.12.19~ On camp~ None 8 0857b3d8e Hugo Pérez Spain M 22.12.19~ On camp~ None 9 0af619e4b Aylin Barat Kazakhstan F 14.8.1995 On camp~ None 10 0ec99ce96 Polina Novik Belarus F 9.10.1996 On camp~ None # i 120 more rows # i 29 more variables: Frequency.Applications <dbl>, # Frequency.Assignment <dbl>, Frequency.Course_view <dbl>, # Frequency.Feedback <dbl>, Frequency.General <dbl>, # Frequency.Group_work <dbl>, Frequency.Instructions <dbl>, # Frequency.La_types <dbl>, Frequency.Practicals <dbl>, # Frequency.Social <dbl>, Frequency.Ethics <dbl>, Frequency.Theory <dbl>, ... To create a histogram for this variable we may use the geom_histogram feature of ggplot2. We just pass our dataset and map the Frequency.Course_view variable to the x axis, and we add the geometry geom_histogram (Fig. 23): ggplot(df, mapping = aes(x= Frequency.Total)) + geom_histogram() 0 5 10 15 500 1000 1500 2000 2500 Frequency.Total count Fig. 23 Histogram of students’ course page views We can provide our own value to the bins argument in geom_histogram to personalize how many bins we want in our plot (Fig. 24): 174 S. López-Pernas et al. ggplot(df, mapping = aes(x= Frequency.Total)) + geom_histogram(bins = 50) 0 5 10 15 500 1000 1500 2000 2500 Frequency.Total count Fig. 24 Histogram of students’ course page view with 50 bins We can also personalize the color scheme using fill for the background of the bars (Fig. 25): ggplot(df, mapping = aes(x= Frequency.Total)) + geom_histogram(bins = 20, fill = "deeppink" ) + scale_x_continuous(n.breaks = 10) 0 10 20 30 300 600 900 1200 1500 1800 2100 2400 Frequency.Total count Fig. 25 Histogram of students’ course page view with color, fill and linewidth Visualizing and Reporting Educational Data with R 181 3.3.7 Scatter Plots The examples we have seen so far have dealt with plotting a single variable alone or divided in categories. Another common scenario is to investigate the direct relationship between two or more variables. Scatter plots are used to visualize how two numerical variables relate to each other. For example, we can use them to see how LMS activity relates to grades (Fig. 34). ggplot(df, aes(x= Frequency.Total, y= Final_grade)) + geom_point() + ylab("Final grade") + xlab("Number of events") Fig. 34 Scatter plot of number of events vs. final grade 0.0 2.5 5.0 7.5 10.0 500 1000 1500 2000 2500 Number of events Final grade In the plot, each point represents a student. Students at the right side of the plot represent students with higher activity, while students closer to the left side of the plot, represent students with lower activity. At the same time, students with low grades are closer to the bottom of the plot, while students with high grades are closer to the top. Overall, se see an upward trend whereby students with higher activity indeed obtain better grades. We can add another dimension by coloring points according to another variable. For example, we can color the points according to high vs. low achievers (Fig. 35), so we can now where the division between the two groups is: ggplot(df, aes(x= Frequency.Total, y= Final_grade, color = AchievingGroup)) + geom_point() + ylab("Final grade") + xlab("Number of events") + labs(color = "Achievement") 182 S. López-Pernas et al. 0.0 2.5 5.0 7.5 10.0 500 1000 1500 2000 2500 Number of events Final grade Achievement Low achiever High achiever Fig. 35 Scatter plot of number of events vs. final grade colored by achievement group We can add yet another dimension by mapping the size aesthetic to another variable, for example Frequency.Group_work which represents the number of events related to group work (Fig. 36). ggplot(df, aes(x= Frequency.Total, y= Final_grade, fill = AchievingGroup, size = Frequency.Group_work)) + geom_point(color = "black", pch = 21) + scale_size_continuous(range = c(1, 7)) + ylab("Final grade") + xlab("Number of events") + labs(size = "Group work", fill = "Achievement") 0.0 2.5 5.0 7.5 10.0 500 1000 1500 2000 2500 Number of events Final grade Group work 200 400 600 Achievement Low achiever High achiever Fig. 36 Scatter plot of number of events vs. final grade colored by achievement group and sized by frequency of group work Visualizing and Reporting Educational Data with R 183 3.4 Advanced Features 3.4.1 Plot Grids Sometimes, adding all the information in a single plot can be overwhelming and hard to interpret. For example, take a look at the following line plot that shows the number of events per day for each of the course online components (Fig. 37): ggplot(events, aes(x= as.Date(timecreated), color = Action )) + scale_fill_viridis_d() + geom_line(stat = "count") + xlab("Date") + ylab("Number of events") 0 500 1000 1500 2000 2500 Sep 15 Oct 01 Oct 15 Date Number of events Action Applications Assignment Course_view Ethics Feedback General Group_work Instructions La_types Practicals Social Theory Fig. 37 Multiple series line plot If we had only a few (2–5) lines, the plot would probably look good, but as the number of categories grow, the plot becomes unintelligible. Instead of showing all the lines together, the plot would be easier to understand if each component had their own plot. To do this, instead of mapping the Action column to the color aesthetic, we add a new component to our plot using facet_wrap and we pass the name of the column as a character string ("Action"). We can change the geom_line to a geom_area to enhance the visualization (Fig. 38). 184 S. López-Pernas et al. ggplot(events, aes(x= as.Date(timecreated))) + geom_area(stat = "count", fill = "turquoise", color = "black") + facet_wrap("Action") + xlab("Date") + ylab("Number of events") La_types Practicals Social Theory Feedback General Group_work Instructions Applications Assignment Course_view Ethics Sep 15 Oct 01 Oct 15 Sep 15 Oct 01 Oct 15 Sep 15 Oct 01 Oct 15 Sep 15 Oct 01 Oct 15 0 500 1000 1500 2000 2500 0 500 1000 1500 2000 2500 0 500 1000 1500 2000 2500 Date Number of events Fig. 38 Grid of multiple plots 3.4.2 Combining Multiple Plots In the previous example, we saw how to split a plot into multiple plots. But what happens if we want to combine multiple independent plots? For that purpose, we can use the library patchwork. Install it first if you do not have it already: install.packages("patchwork") We import the patchwork library: Visualizing and Reporting Educational Data with R 185 library(patchwork) We have to create the plots that we want to combine and assign each of them to a different variable. We can use previous examples from this chapter and assign them to variables named p1, p2, and p3. p1 <- ggplot(df, aes(x= Frequency.Total, y= Final_grade)) + geom_point() + ylab("Grade") + xlab("Total number of events") p2 <- ggplot(df, aes(x= AchievingGroup, fill = ActivityGroup )) + geom_bar(position = position_fill(reverse = T)) + scale_fill_viridis_d(direction =-1) + xlab("Achievement group") + ylab("Number of events") + labs(fill = "Activity level") p3 <- ggplot(events, aes(x= as.Date(timecreated) )) + geom_line(stat = "count", color = "turquoise", linewidth = 1.5) + geom_point(stat = "count", color = "purple", size = 2, stroke = 1) + xlab ("Date") + ylab("Number of events") Now, if we add the three variables together separated by the + sign, the plots will be placed horizontally next to each other (Fig. 39): p1 + p2 + p3 0.0 2.5 5.0 7.5 10.0 500 1000 1500 2000 2500 Total number of events Grade 0.00 0.25 0.50 0.75 1.00 Low achiever High achiever Achievement group Number of events Activity level Low activity Moderate activity High activity 1000 2000 3000 4000 5000 Sep 15 Oct 01 Oct 15 Date Number of events Fig. 39 Multiple plots stacked horizontally 186 S. López-Pernas et al. If we use the / character side instead, we lay them out vertically (Fig. 40): p1 / p2 / p3 Fig. 40 Multiple plots stacked vertically 0.0 2.5 5.0 7.5 10.0 500 1000 1500 2000 2500 Total number of events Grade 0.00 0.25 0.50 0.75 1.00 Low achiever High achiever Achievement group Number of events Activity level Low activity Moderate activity High activity 1000 2000 3000 4000 5000 Sep 15 Oct 01 Oct 15 Date Number of events We can use combinations of both signs and even leave blank spaces as follows (Fig. 41): (p1 + p2) / (p3 + plot_spacer()) Visualizing and Reporting Educational Data with R 187 0.0 2.5 5.0 7.5 10.0 500 1000 1500 2000 2500 Total number of events Grade 0.00 0.25 0.50 0.75 1.00 Low achiever High achiever Achievement group Number of events Activity level Low activity Moderate activity High activity 1000 2000 3000 4000 5000 Sep 15 Oct 01 Oct 15 Date Number of events Fig. 41 Multiple plots in a grid Putting plots side by side can be very useful to compare datasets and discuss the differences. Some publication venues limit the number of figures or pages of their articles, so combining several plots together can be very useful to overcome this limitation. 4 Creating Tables with gt We have seen earlier in this chapter multiple types of visualizations that are suitable for diverse scenarios in learning analytics. However, we must not forget the other main way of reporting results or metrics, i.e., tables. When we display a data frame in Rstudio, it is by default presented as a table, but we need to be able to extract this table and display it in a dashboard, a report or a scientific article. The library gt can help us with this endeavor. First, install it if you do not have it yet: install.packages("gt") We then import it, as usual: library(gt) Let us create a table, for example, to display the descriptive statistics of students’ events in the LMS. Using the events dataset, we first count the number of events of each type (Event.name) per student (user)using group_by and count from 188 S. López-Pernas et al. dplyr. We then group by Event.name only and use the summarize function, also from dplyr, to create the mean, and standard deviation of the number of events of each type per student, as we learned in Chapter 5 [30]. events |> group_by(user, Action) |> count() |> group_by(Action) |> summarize(Mean = mean(n), SD = sd(n)) # A tibble: 12 x 3 Action Mean SD <chr> <dbl> <dbl> 1 Applications 11.1 9.83 2 Assignment 56.7 34.1 3 Course_view 195. 152. 4 Ethics 11.7 10.7 5 Feedback 24.7 16.2 6 General 25.7 21.4 7 Group_work 252. 163. 8 Instructions 49.8 40.3 9 La_types 14.5 7.58 10 Practicals 77.1 33.8 11 Social 18.1 19.0 12 Theory 11.1 6.92 Now that we have a data frame with the shape that we like, we can use gt to create the formatted table by simply adding gt to the pipeline of operations (Table 1): events |> group_by(user, Action) |> count() |> group_by(Action) |> summarize(Mean = mean(n), SD = sd(n)) |> gt() Visualizing and Reporting Educational Data with R 189 Table 1 Table created with gt Action Mean SD Applications 11.07143 9.825022 Assignment 56.68462 34.129492 Course_view 194.56154 151.656947 Ethics 11.68182 10.669050 Feedback 24.71429 16.243082 General 25.73846 21.390991 Group_work 251.90769 162.899810 Instructions 49.80000 40.272213 La_types 14.54615 7.583245 Practicals 77.07692 33.751627 Social 18.10744 19.034093 Theory 11.10484 6.922120 We might add some tweaks by forcing the numerical columns to have two decimals and the first column to be aligned left. You can also apply themes to the table using the library gtExtras (Table 2). events |> group_by(user, Action) |> count() |> group_by(Action) |> summarize(Mean = mean(n), SD = sd(n)) |> gt() |> fmt_number(decimals = 2, columns = where(is.numeric)) |> cols_align(align = "left", columns = 1) Table 2 Table created with gt with formatting Action Mean SD Applications .11.07 . 9.83 Assignment .56.68 . 34.13 Course_view .194.56 . 151.66 Ethics .11.68 . 10.67 Feedback .24.71 . 16.24 General .25.74 . 21.39 Group_work .251.91 . 162.90 Instructions .49.80 . 40.27 La_types .14.55 . 7.58 Practicals .77.08 . 33.75 Social .18.11 . 19.03 Theory .11.10 .6.92 190 S. López-Pernas et al. 5 Discussion The use of data visualization in the context of learning analytics has the potential to greatly enhance our understanding of student behavior and performance. Using tools such as ggplot2, instructors and researchers can create informative and visually appealing plots that highlight important patterns and trends in student activity, providing insights into factors that may be impacting student success and therefore inform instructional decisions and improve student outcomes. As we have already seen throughout the chapter, we often use different plots when dealing with categorical variables or numerical variables; when plotting a single variable or two (or more), etc. Moreover, on some occasions when we need very detailed information, a table might be more informative compared to a figure. As a summary for the possible visualizations, Table 3 gathers the most commonly used visualization types that we have seen throughout this chapter according to the number of variables and the data type. It also points to the ggplot2 geometry that is used to create each visualization. Table 3 Summary of the types of visualization for each data type and number of variables Number of variables Variable types Type of visualization ggplot2 geometry One variable Continuous Histogram geom_hist() Discrete Bar chart geom_bar() Two or more variables Both continuous Scatter plot geom_point() One discrete time and Line chart geom_line() one continuous Area chart geom_area() One discrete and one Bar chart geom_bar() continuous Box plot geom_boxplot() Jitter plot geom_jitter() Violin plot geom_violin() Both discrete Stacked bar chart geom_bar() Another way to decide which visualization to use is to think what kind of story we want to tell or which aspect of our data we want to highlight. Figure 42 shows a flowchart that can help choose the most suitable visualization for our data. There are many other decision charts online made for this purpose. For example, “From Data to Viz”1 leads you to the most appropriate graph for your data and also links to the code to build it and lists common caveats you should avoid. Throughout the rest of the book, we will see other forms of data visualization that are inherent to specific learning analytics methods. For example, in Chapter 15 [31], we will learn how to represent students’ discussions in the form of 1 Data to Viz https://www.data-to-viz.com/.