How to label plot tick marks using ggvis in R
In this article, we will be looking at the approach to label tick marks using ggvis in the R programming language.
The data frame is then subjected to the ggvis operations using the pipe operator. The ggvis method 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_lines() method in the ggvis package is used to arrange the order by the x variable, by default. The method has the following syntax :
layer_lines()
The add_axis method is then added to derive the labeling for the axes of the plotted graph. It can be used to override the default value for the axes.
add_axis( vis, axes , values = )
Arguments :
- vis â The ggvis object to use for plotting
- axes â The axes to be used for plotting
- values â The values to be used for deriving the tick marks for the label
R
# installing the required libraries library (ggplot2) library (ggvis) # creating the data frame by defining # the x and y coordinates respectively data_frame < - data.frame ( x_pos=1: 10, y_pos < - 1: 10 ) # printing the data frame print ( "Data Frame" ) print (data_frame) # plotting the data data_frame % >% ggvis (~x_pos, ~y_pos) % >% layer_lines () % >% add_axis ( "x" , # adding the x axis tick marks value= c (1: 10) ) |
Output
[1] "Data Frame" 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 10 10 10
Customized labels can also be added to the axes by specifying the labels of the axes. The labels of the axes may also be string values which are plotted at the used points.
R
# installing the required libraries library (ggplot2) library (ggvis) # creating the data frame by defining # the x and y coordinates respectively x_pos <- factor ( c (5,10,15), labels= letters [1:3]) # defining the y axis y_pos = c (5,10,15) # creating the data frame data_frame = data.frame (x_pos, y_pos ) print ( "Data Frame" ) print (data_frame) # plotting the tick marks on the axes data_frame %>% ggvis (~x_pos,~y_pos) %>% layer_lines () %>% # adding customised tick marks to # the axes add_axis ( "x" , values=x_pos) |
Output
[1] "Data Frame" x_pos y_pos 1 a d 2 b e 3 c f
Labels can also be added to both the x and y axes, respectively. User-defined labels can be set to plot the tick marks of the axes labels of the ggvis plot.
R
# installing the required libraries library (ggplot2) library (ggvis) # creating the data frame by defining the # x and y coordinates respectively x_pos <- factor ( c (5,10,15), labels= letters [1:3]) # defining the y axis y_pos = factor ( c (5,10,15), labels= letters [4:6]) # creating the data frame data_frame = data.frame (x_pos, y_pos ) print ( "Data Frame" ) print (data_frame) # plotting the tick marks on the axes data_frame %>% ggvis (~x_pos,~y_pos) %>% layer_lines () %>% # adding customised tick marks to the axes add_axis ( "x" , values=x_pos) %>% add_axis ( "y" , values = y_pos) |
Output
[1] "Data Frame" x_pos y_pos 1 a d 2 b e 3 c f