How to add a specific point to legend in ggvis in R?
Last Updated :
02 Aug, 2022
In this article, we will be going through the approach to add a specific point to a legend in ggvis package in the R programming language.
The ggvis package in R is used to provide the data visualization. It is used to create visual interactive graphics tools for data plotting and representation. The package can be installed into the working space using the following command :
install.packages("ggvis")
The ggvis method in the ggvis package is used to start ggvis graphical window. The ggvis method has the following syntax :
ggvis( data , mp1, mp2.,)
Arguments :
- data - The dataset to plot
- mp1, mp2,.. - The map variables to plot
The layer_points() method in the ggvis package is used to mark the coordinates of the points in the form of dots. Visual points can be added to this method, like the fill, stroke, and shape. The layer_points() method has the following syntax in R :
layer_points(vis, data, ...)
Arguments :
- vis - The ggvis object
- data - The data frame to be used for plotting the points
- ... - The additional attributes like the fill
The scale_nominal() method is used to add a nominal scale to the ggvis plot. The scale_nominal() method is supplied with external visual attributes, like, fill and range. The range attribute is supplied with a vector of colours to label the data points.
scale_nominal (fill , range )
This is further supplied with the add_legend method which is used to add a legend to the ggvis plot. The title attribute can be used to add a title to the made plot.
add_legend ( fill , title)
Arguments :
- fill - The visual fill attribute
- title - The title to be given to the legend
Initially, a data frame is created using the specified data points in R. Two subsets of the data frame are created using the subset() methods. The data frame is then subjected to the ggvis method using the pipe operator. The points are then plotted using dots and then the points are represented using the visual fill attribute. The legend is added with a title.
R
# installing the required libraries
library(ggplot2)
library(ggvis)
# creating the data frame by defining
# the x and y coordinates respectively
x_pos <- 1:10
# defining the y axis
y_pos = 5:14
# creating the data frame
data_frame = data.frame(x_pos, y_pos )
print("Data Frame")
print(data_frame)
# creating a subset of the dataset
# where x_pos value is equivalent to 2
df1 <- subset(data_frame, x_pos == 2)
# creating a subset of the dataset
# where x_pos value is equivalent to 2
df2 <- subset(data_frame, x_pos == 7)
# plotting the tick marks on the axes
data_frame %>%
ggvis(~x_pos,~y_pos) %>%
# marking the point labels for
# the coordinates
layer_points() %>%
# marking x = 2 with green color
layer_points(data = df1, fill = ~"x = 2") %>%
# marking x = 5 with green color
layer_points(data = df2, fill = ~"x = 5") %>%
scale_nominal("fill", range = c("green", "red") ) %>%
# creating solid color dots
add_legend("fill", title = "x coordinates" )
Output
[1] "Data Frame"
> print(data_frame)
x_pos y_pos
1 1 5
2 2 6
3 3 7
4 4 8
5 5 9
6 6 10
7 7 11
8 8 12
9 9 13
10 10 14
The below code snippet illustrates the plotting of the data frame y coordinates indicated by the y_pos data points. The points are labelled using different colors and fill attribute values respectively.
R
# installing the required libraries
library(ggplot2)
library(ggvis)
# creating the data frame by defining
# the x and y coordinates respectively
x_pos < - 1: 10
# defining the y axis
y_pos = 5: 14
# creating the data frame
data_frame = data.frame(x_pos, y_pos)
print("Data Frame")
print(data_frame)
# creating a subset of the dataset where
# y_pos value is equivalent to 9
df1 < - subset(data_frame, y_pos == 9)
# creating a subset of the dataset where
# y_pos value is equivalent to 11
df2 < - subset(data_frame, y_pos == 11)
# creating a subset of the dataset where
# y_pos value is equivalent to 13
df3 < - subset(data_frame, y_pos == 13)
# plotting the tick marks on the axes
data_frame % >%
ggvis(~x_pos, ~y_pos) % >%
# marking the point labels for the
# coordinates
layer_points() % >%
# marking x = 2 with green color
layer_points(data=df1, fill=~"y = 9") % >%
layer_points(data=df2, fill=~"y = 11") % >%
layer_points(data=df3, fill=~"y = 13") % >%
scale_nominal("fill", range=c("green", "blue", "red")) % >%
# creating solid color dots
add_legend("fill", title="y coordinates")
Output
[1] "Data Frame"
> print(data_frame)
x_pos y_pos
1 1 5
2 2 6
3 3 7
4 4 8
5 5 9
6 6 10
7 7 11
8 8 12
9 9 13
10 10 14
Similar Reads
How to Add a plot title to ggvis in R
In this article, we will be looking at the approach to adding a plot title to ggvis package in the R programming language. The ggvis package in R is used to provide the data visualization. It is used to create visual interactive graphics tools for data plotting and representation. The package can be
3 min read
How To Add Circles Around Specific Data Points in R
In this article, we will discuss how to add circles around specific data points in R  Programming Language. Method 1: Using geom_point method The ggplot2 package in R is used to perform data visualization. To install this package type the below command in the terminal. Syntax: install.packages("gg
3 min read
How To Add Mean Line to Ridgeline Plot in R with ggridges?
In this article, we will discuss how to add a mean line to the Ridgeline plot in R Programming Language with the ggridges package. Ridgeline plot helps us to visualize multiple distributions or a distribution that changes a quantifiable variable. The name âridgelineâ comes from its appearance as an
3 min read
How to move or position a legend in ggplot2?
In this article, we will discuss how to control a legend position in ggplot using an R programming language. To draw a legend within ggplot the parameter col is used, it basically adds colors to the plot and these colors are used to differentiate between different plots. To depict what each color re
3 min read
How to show legend in heatmap in R?
A heat map is a graphical representation of data where each data value is represented in terms of color value. Heatmap is created using heatmap() function in R. Legend associated with histogram makes it easy to understand what the color values mean. Legend is shown with histogram using legend() func
2 min read
How To Fold Legend into Two Rows in ggplot2 in R
In this article, we are going to see how to draw a ggplot2 legend with two Rows in R Programming Language. If we want to draw ggplot2 Legend with two rows, we have to add guides and guide_legend functions to the theme() function. Inside guides() function, we take parameter named color, which has cal
2 min read
How to position legends inside a plot in Plotly-Python?
In this article, we will learn How to hide legend with Plotly Express and Plotly. A legend is an area describing the elements of the graph. In the plotly legend is used to Place a legend on the axes. Example 1: In this example, we are positioning legends inside a plot with the help of method fig.upd
2 min read
How to change legend title in ggplot2 in R?
In this article, we will see how to change the legend title using ggplot2 in R Programming. We will use ScatterPlot. For the Data of Scatter Plot, we will pick some 20 random values for the X and Y axis both using rnorm() function which can generate random normal values, and here we have one more p
3 min read
How to set axis limits in ggplot2 in R?
In this article, we are going to see how to set the axis limit in ggplot2 in R Programming Language. Method 1: Using coord_cartesian() This method is most helpful as it displays the plot within the limits of the axis without clipping the actual data. It just zooms in and shows the area under limits.
2 min read
How to Draw a Legend Outside of a Plot in R?
In this article, we will discuss how to draw a legend outside of a Plot in the R Programming Language. We first create a basic plot without legend using the plot() function and add a margin around it for legend by using the par() function of the R Language. We will create the required margin and mak
2 min read