Search⌘ K

Solution: Visualize the Data

Explore how to build comprehensive visualizations in R by adding multiple layers like barplots, line graphs, and scatter plots with ggplot2. Understand how to read datasets, set variables, and use dual axes for clear data representation.

We'll cover the following...

Explore the solution

The detailed solution for the challenge is given below. The layers are added one by one.

R
library(ggplot2)
traffic <- read.csv('Documents/blog_traffic.csv')
ggplot(data = traffic, aes(x = days)) +
geom_bar(aes(y = pageviews), stat = 'identity', color = 'darkblue', fill = 'cyan')+
geom_line(aes( y = time_spent*1000), group = 1, color = 'black', linewidth = 3) +
geom_point(aes(y = time_spent*1000), color = 'purple', size = 6) +
scale_y_continuous(name = "Pageviews", sec.axis = sec_axis(~ . / 1000, name = "Time Spent"))
  • Line 2: We read the dataset from the given location and name it traffic.

  • Line 3: We define the common variables in the ggplot() function so that we do not repeat ...