ggplot
The ggplot2 package is a powerful and widely used package for graphic visualization. It can be used to provide a lot of aesthetic mappings to the plotted graphs. This package is widely available in R. The package can be downloaded and installed into the working space using the following command :
install.packages("ggplot2")
The ggplot method can be used to create a ggplot object. The graphical object is used to create plots by providing the data and its respective points. The data can be plotted using both points as well as lines.
ggplot(data, aes = )
Arguments :
- data – The data to be plotted
- aes – The aesthetic mappings
# installing the reqd library
library("ggplot2")
# creating a data frame
data_frame <- data.frame(
col2 = 1:8,
col3 = LETTERS[1:8]
)
print ("Data Frame")
print(data_frame)
# plotting the col2 and col3 of
# data frame
ggplot(data_frame,aes(col2,col3))+
geom_point()
Output
[1] "Data Frame"
col2 col3
1 1 A
2 2 B
3 3 C
4 4 D
5 5 E
6 6 F
7 7 G
8 8 H
Now we perform some visualization on both the packages using Iris dataset.
ggvis Scatter Plot
# Load necessary libraries
library(ggvis)
# Interactive Scatter Plot using ggvis
scatter_plot <- iris %>%
ggvis(~Sepal.Length, ~Sepal.Width, fill = ~Species) %>%
layer_points(size := 100) %>%
add_tooltip(function(df) df$Species) %>%
hide_legend("fill") %>%
layer_text(x = ~mean(range(Sepal.Length)), y = ~max(Sepal.Width) + 0.5,
text := "Scatter Plot - ggvis", fontSize := 15, baseline := "bottom") %>%
set_options(width = 400, height = 300)
# Display the plot
print(scatter_plot)
Output:
Scatter Plot using ggplot2
# Load necessary libraries
library(ggplot2)
# Scatter plot using ggplot2
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point(size = 3) +
labs(title = "Scatter Plot - ggplot2", x = "Sepal Length", y = "Sepal Width") +
theme_minimal()
Output:
Interactive Bar Chart using ggvis
# Load necessary libraries
library(ggvis)
# Interactive Bar Chart using ggvis
bar_chart <- iris %>%
ggvis(x = ~Species, y = ~Sepal.Length, fill = ~Species) %>%
layer_bars() %>%
add_tooltip(function(df) df$Species) %>%
scale_nominal("fill", range = c("blue", "red", "green"))
# Display the plot
print(bar_chart)
Output:
Interactive Bar Chart using ggplot
# Load necessary libraries
library(ggplot2)
# Bar Plot using ggplot2 on iris dataset
bar_plot <- ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) +
geom_bar(stat = "identity") +
labs(title = "Bar Plot of Sepal Length by Species",
x = "Species",
y = "Sepal Length") +
theme_minimal()
# Display the plot
print(bar_plot)
Output:
Box Plot using ggvis
# Load necessary libraries
library(ggvis)
# Define custom colors for species
colors <- c("darkblue", "darkred", "darkgreen")
# Box Plot using ggvis on iris dataset with color scales
box_plot_ggvis <- iris %>%
ggvis(x = ~Species, y = ~Sepal.Length, fill = ~Species) %>%
layer_boxplots(fillOpacity := 0.7, strokeWidth := 0.5, stroke := "black") %>%
scale_nominal("fill", range = colors) %>%
add_tooltip(function(df) df$Species)
# Display the plot
print(box_plot_ggvis)
Output:
Box Plot using ggplot2
# Load necessary libraries
library(ggplot2)
# Box Plot using ggplot2 on iris dataset
box_plot_ggplot <- ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) +
geom_boxplot() +
labs(title = "Box Plot of Sepal Length by Species",
x = "Species",
y = "Sepal Length") +
theme_minimal()
# Display the plot
print(box_plot_ggplot)
Output:
Difference Between ggvis VS ggplot in R
R programming language provides many tools and packages to visualize the data points using interactive plots. ggpvis vs ggplot2 plots can be annotated or customized with different headings, titles, and color schemes to improve the readability of the data plotted.
Table of Content
- ggvis
- ggplot
- Table of difference between ggvis and ggplot
Contact Us