Open In App

Data Visualization using GoogleVis Package

Last Updated : 31 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

GoogleVis is a package in R that is used to act as an interface between R and the Google API to produce interactive charts which can be easily embedded into web pages. This package helps the user to plot the data without uploading them into google. In this article let’s parse through some charts that can be plotted using googleVis in R Programming Language.

Line Chart using googleVis

In the googleVis package gvisLineChart() function is used to plot the Line charts. This is going to generate output referring to Google API.

Syntax : gvisLineChart(data, xvar, yvar)

Where,

  • data – a data frame to be displayed on the chart
  • xvar – the column name of the data frame which values of it need to be placed on x-axis
  • yvar – the column name of the data frame which values of it need to be placed on y-axis 

R




# load the installed package
library(googleVis)
 
# Creation of data frame which need to be plotted
df=data.frame(subject=c("Maths","Science","Social"),
              section1=c(10,15,25),
              section2=c(15,17,24),
              section3=c(17,23,25))
line<-googleVis::gvisLineChart(df)
 
# Plotting the line chart
plot(line)


Output:

Line Chart using googleVis

Line Chart using googleVis

Bar Chart using googleVis

In the googleVis package gvisBarChart() function is used to plot the Line chart. This is going to generate output referring to Google API.

Syntax : 

gvisLineChart(data, xvar, yvar)

Where,

  • data – a data frame to be displayed on the chart
  • xvar – the column name of the data frame which values of it need to be placed on x-axis
  • yvar – the column name of the data frame which values of it need to be placed on y-axis 
  • chartid – character.[default – a random chart id will be generated based on chart type]

R




# load the installed package
library(googleVis)
 
# Creation of data frame which need to be plotted
df=data.frame(subject=c("Maths","Science","Social"),
              section1=c(10,15,25),
              section2=c(15,17,24),
              section3=c(17,23,25))
bar<-googleVis::gvisBarChart(df)
 
# Plotting the bar chart
plot(bar)


Output:

Bar Chart using googleVis

Bar Chart using googleVis

Area Chart using googleVis

An area chart is used when one wants to plot the change in quantities over time. It can also be termed as a combination of Scatter and Bar plots. In the googleVis library gvisAreaChart() is used to plot the Area Chart.

Syntax : gvisAreaChart(data,xvar,yvar,chartid)

Where,

  • data – a data frame to be displayed on the chart
  • xvar – the column name of the data frame which values of it need to be placed on x-axis
  • yvar – the column name of the data frame which values of it need to be placed on y-axis 
  • chartid – character.[default – a random chart id will be generated based on chart type]

R




# load the installed package
library(googleVis)
 
# Creation of data frame which need to be plotted
df=data.frame(subject=c("Maths","Science","Social"),
              section1=c(10,15,25),
              section2=c(15,17,24),
              section3=c(17,23,25))
 
# Area Chart using googleVis
area_chart<-googleVis::gvisAreaChart(df)
 
# Plotting the Area chart
plot(area_chart)


Output:

Area Chart using googleVis

Area Chart using googleVis

Scatter Chart using googleVis

As the name indicates it will plot the relation between two variables in the form of scattered points. In the googleVis library gvisScatterChart() function, we have used to plan the Scatter Plot.

Syntax : gvisScatterChart(data, xvar, yvar, chartid)

Where,

  • data – a data frame to be displayed on the chart
  • xvar – the column name of the data frame which values of it need to be placed on x-axis
  • yvar – the column name of the data frame which values of it need to be placed on y-axis 
  • chartid – character.[default – a random chart id will be generated based on chart type]

R




# load the installed package
library(googleVis)
 
# Creation of data frame which need to be plotted
df=data.frame(
  Roll_number=c(1,2,3,4,5,6,7,8,9,10),
  Marks_Section1=c(23,30,21,24,27,28,23,30,29,26),
  Marks_Section2=c(31,30,26,45,23,44,32,25,46,33))
 
# Scatter Chart using googleVis
scatter_chart<-googleVis::gvisScatterChart(df,
                options=list(pointSize=5,
                title="Scatter Plot using googleVis"))
 
# Plotting the scatter chart
plot(scatter_chart)


Output:

Scatter Chart using googleVis

Scatter Chart using googleVis

Gauge Chart using googleVis:

Gauge Chart is a chart that is used to represent a single data variable in the form of a colored range bar like a speedometer. The values which need to be displayed under red, yellow, and green colors can be specified in the options parameter of the gvisGuage() function.

Syntax : gvisScatterChart(data, xvar, yvar, chartid)

Where,

  • data – a data frame to be displayed on the chart
  • xvar – the column name of the data frame which values of it need to be placed on x-axis
  • yvar – the column name of the data frame which values of it need to be placed on y-axis 
  • chartid – character.[default – a random chart id will be generated based on chart type]
  • options – used to specify the list of optional parameter that need to be added to chart

R




# Creation of data frame which need to be plotted
df=data.frame(subject=c("Maths","Science","Social"),
              section1=c(10,15,25),
              section2=c(15,17,24),
              section3=c(17,23,25),
              section4=c(12,23,19))
 
# Gauge Chart using googleVis package
Gauge <-  googleVis::gvisGauge(df,
                    options=list(min=0, max=30,
                                 greenFrom=0, greenTo=10,
                                 yellowFrom=10, yellowTo=20,
                                 redFrom=20, redTo=30,
                                 width=400, height=300))
# plot the gauge chart
plot(Gauge)


Output:

Guage Chart using googleVis

Guage Chart using googleVis

GeoChart using googleVis:

GeoChart can is mainly used to represent data that are bound to particular regions like a country, continent, etc, In R Programming this type of plot can be built using the googleVis package as follows:

Syntax: gvisGeoChart(data, locationvar, colorvar, hovervar, chartid)

Where,

  • data – a data frame to be displayed on the geo chart
  • locationvar – column name of data  frame with the geo locations to be analysed.
  • colorvar – column name of data frame with the geo locations to assign color marker
  • hovervar – column name of data frame with the geo locations to assign hover string
  • chartid – character.[default – a random chart id will be generated based on chart type]

R




# load the installed package
library(googleVis)
 
# Creation of sample data frame
# which represents GDP share of some countries
df=data.frame(Country=c("India", "Pakistan",
                        "United States", "South Africa",
                        "Japan"),
              GDP_Percent_Share=c(3.5, 1.1,
                                  24.1, 3.9, 4.8))
 
# plotting the GeoChart using googleVis package
Geo=gvisGeoChart(df, locationvar="Country",
                 colorvar="GDP_Percent_Share")
plot(Geo)


Output:

GeoChart using GoogleVis

GeoChart using GoogleVis



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads