patchwork and plotly packages

Sample Dataset

Use patchwork to combine the bar plot and line plot created above into a single row figure.

Solution:

combined_1 <- bar_plot + line_plot
combined_1

Use patchwork to combine the histogram and scatter plot created above into a single columb figure.

Solution:

combined_2 <- hist_plot / scatter_plot
combined_2

Arrange all four plots (bar plot, line plot, histogram, and scatter plot) in a 2x2 grid using patchwork.

Solution:

combined_3 <- (bar_plot + line_plot) / (hist_plot + scatter_plot)
combined_3

Create a combined plot from exercises 1 or 2 and add a title to the entire plot.

Solution:

combined_4 <- combined_1 + plot_annotation(title = "Combined Bar and Line Plot")
combined_4

Combine the plots from exercise 3 and adjust the spacing between plots using plot_layout().

Solution:

combined_5 <- combined_3 + plot_layout(guides = "collect", ncol = 2)
combined_5

Create a combined plot from exercise 3 and add annotations to specific plots in the grid.

Solution:

combined_6 <- combined_3 + plot_annotation(
  title = "Combined Plots with Annotations",
  caption = "Data visualizations generated using ggplot2 and patchwork"
)
combined_6

Convert the bar plot created above into an interactive plot using plotly.

Solution:

interactive_bar <- ggplotly(bar_plot)
interactive_bar

Convert the line plot created above into an interactive plot using plotly.

Solution:

interactive_line <- ggplotly(line_plot)
interactive_line

Convert the scatter plot created above into an interactive plot using plotly.

Solution:

interactive_scatter <- ggplotly(scatter_plot)
interactive_scatter

Convert the histogram created above into an interactive plot using plotly.

Solution:

interactive_hist <- ggplotly(hist_plot)
interactive_hist

Combine the interactive bar plot and line plot created in exercises 1 and 2 using plotly::subplot().

Solution:

interactive_combined <- subplot(interactive_bar, interactive_line, nrows = 1)
interactive_combined

Enhance the interactive scatter plot from exercise 9 by adding tooltips that display additional information (e.g., height and weight).

Solution:

interactive_scatter_tooltip <- ggplotly(scatter_plot, tooltip = c("height_cm", "weight_kg"))
interactive_scatter_tooltip