Skip to content
Related Articles
Open in App
Not now

Related Articles

How to make multiple smoothing lines in ggvis?

Improve Article
Save Article
  • Last Updated : 26 Oct, 2022
Improve Article
Save Article

In this article, we are going to see How to make multiple smoothing lines in ggvis using R Programming Language.

Make multiple smoothing lines in ggvis

The ggvis package in R is used for data visualization. It is also used to create interactive web graphics. It is also used to create interactive graphs and plots. It is an addition to the ggplot package. It provides the framework to build HTML graphs in the working space. The package can be downloaded and installed into the working space using the following command :

install.packages("ggvis")

A data frame can be created using the data.frame method which takes as input a number of columns and the values stored in it. This data can be visualized using the ggvis method which computes the visual representation of the y-axis and x-axis data contained in the data frame. The ggvis method has the following syntax.

Syntax: ggvis(~y,~x,stroke)

Arguments:

  • ~y,~x- y-axis and x-axis of the data frame
  • stroke- the different color values to plot different lines in ggvis

This is followed by the application of layers_lines() method using the pipe operator. The line segments are plotted connecting the y and x axis respectively. The points can also be marked significantly representing the data values contained within the data frame. This method is used to also sort the data that is available on the x variable. All the lines are plotted from left to right direction.

This is followed by the application of layer_points() method which is used to mark the data points on the plotted graph. This method as the following syntax.

Syntax: layer_points(size,fill)

Arguments: 

  • size- the size of the data points to be plotted. 
  • fill- the color of the data points to be plotted.

The following code shows two groups that are to be plotted in terms of the ratings given by the people. One line is drawn for the group GeeksForGeeks and another for Geekster using different colors. 

R




# installing the required libraries
library(dplyr)
library(tidyverse)
library(dplyr)
library(ggvis)
 
# creating a data frame
data_frame = data.frame(group = c("Geekster","GeeksforGeeks",
                                  "Geekster","Geekster",
                                      "GeeksforGeeks",
                                  "GeeksforGeeks","Geekster",
                                  "GeeksforGeeks",
                                      "Geekster","Geekster"),
                        people = c(10,12,21,45,23,54,
                                   22,12,32,45),
                        rating = c(4,3,5,7,2,8,1,10,9,6))
# printing the data frame
print("Original Data frame")
print(data_frame)
 
# plotting the data
data_frame %>%
  ggvis(~people, ~rating, stroke = ~group) %>%
  layer_lines() %>%
  layer_points(size = 1, fill = ~group)


Output

[1] "Original Data frame"
> print(data_frame)
           group people rating
1       Geekster     10      4
2  GeeksforGeeks     12      3
3       Geekster     21      5
4       Geekster     45      7
5  GeeksforGeeks     23      2
6  GeeksforGeeks     54      8
7       Geekster     22      1
8  GeeksforGeeks     12     10
9       Geekster     32      9
10      Geekster     45      6

 

The following code snippet shows four groups instead of two that are used for plotting thereby the plot contains four different color lines.

R




# installing the required libraries
library(dplyr)
library(tidyverse)
library(dplyr)
library(ggvis)
 
# creating a data frame
data_frame = data.frame(group = c("Geekster","TCS","Geekster","Geekster",
                                      "GeeksforGeeks",
                                  "Wipro","Geekster","GeeksforGeeks",
                                      "Geekster","Wipro",
                                  "TCS"),
                        people = c(10,12,21,45,23,54,22,12,32,45,65),
                        rating = c(4,3,5,7,2,8,1,10,9,6,5))
 
# printing the data frame
print("Original Data frame")
print(data_frame)
 
# plotting the data
data_frame %>%
  ggvis(~people, ~rating, stroke = ~group) %>%
  layer_lines() %>%
  layer_points(size = 1, fill = ~group)


Output

[1] "Original Data frame"
> print(data_frame)
           group people rating
1       Geekster     10      4
2            TCS     12      3
3       Geekster     21      5
4       Geekster     45      7
5  GeeksforGeeks     23      2
6          Wipro     54      8
7       Geekster     22      1
8  GeeksforGeeks     12     10
9       Geekster     32      9
10         Wipro     45      6
11           TCS     65      5

 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!