Solution: Visualize the Data

Compare your answer with the solution in this lesson.

We'll cover the following...

Explore the solution

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

Press + to interact
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 ...