Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Telling a Data Story: Combining Plots and Drawing Conclusions | Project Tasks
Data Visualization Project with R and ggplot2
Avsnitt 1. Kapitel 6
single

single

bookTelling a Data Story: Combining Plots and Drawing Conclusions

Svep för att visa menyn

When you want to tell a compelling data story, combining different types of plots is essential. Each plot type brings a unique perspective: a histogram can show the distribution of a variable, a boxplot can compare distributions across groups, and a scatter plot can reveal relationships between two continuous variables. By thoughtfully arranging these visualizations, you can guide your audience through a narrative that uncovers patterns and insights not visible in any single plot alone. Let’s consider the well-known iris dataset to illustrate how these plots can be combined for comprehensive analysis.

12345678910111213141516171819202122
# Load required libraries library(ggplot2) library(gridExtra) # For arranging multiple plots # Histogram: Distribution of petal length hist_petal_length <- ggplot(iris, aes(x = Petal.Length)) + geom_histogram(binwidth = 0.5, fill = "skyblue", color = "black") + ggtitle("Distribution of Petal Length") # Boxplot: Petal length by species boxplot_petal_length <- ggplot(iris, aes(x = Species, y = Petal.Length, fill = Species)) + geom_boxplot() + ggtitle("Petal Length by Species") + theme(legend.position = "none") # Scatter plot: Petal length vs petal width scatter_petal <- ggplot(iris, aes(x = Petal.Length, y = Petal.Width, color = Species)) + geom_point(size = 2) + ggtitle("Petal Length vs Petal Width") # Arrange the plots in a grid grid.arrange(hist_petal_length, boxplot_petal_length, scatter_petal, ncol = 2)
copy

Begin your narrative by exploring the distribution of petal length with a histogram. This reveals whether the measurement is normally distributed or skewed, and if there are any unusual peaks. Next, a boxplot breaks down petal length by species, showing not just the median and spread but also highlighting which species tend to have longer or shorter petals. Finally, a scatter plot of petal length versus petal width, colored by species, uncovers relationships between these two features and shows how species cluster in this two-dimensional space. By connecting these plots, you see that while the histogram provides a general overview, the boxplot and scatter plot add depth, allowing you to pinpoint which species drive the patterns observed in the overall distribution and how petal dimensions relate within and across species.

To combine plots in R, you can use the gridExtra package’s grid.arrange() function. Each plot is first created and stored as a variable (for example, hist_petal_length), then passed as arguments to grid.arrange(), which arranges them in a grid layout. The ncol argument specifies the number of columns. If you use the patchwork package, you can combine plots with simple addition or layout operators (for example, plot1 + plot2). Interpreting the combined visualizations means looking at how each plot complements the others: the histogram gives an overview, the boxplot allows for group comparisons, and the scatter plot uncovers relationships. Together, these plots provide a more complete understanding than any could alone.

Uppgift

Svep för att börja koda

Combine at least two types of plots to tell a data story about mammalian sleep using the msleep dataset. Reference the code and narrative from earlier in the chapter, ensuring all functions and arguments are clearly explained.

  • Use the provided histogram and scatter plot objects.
  • Combine the two plots into a single display using a function that arranges multiple plots.
  • Ensure the combined visualization allows for interpretation of both the distribution of sleep time and its relationship with body weight.

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 6
single

single

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

some-alt