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
library (ggplot2)
library (ggvis)
data_frame < - data.frame (
x_pos=1: 10,
y_pos < - 1: 10
)
print ( "Data Frame" )
print (data_frame)
data_frame % >%
ggvis (~x_pos, ~y_pos) % >%
layer_lines () % >%
add_axis ( "x" ,
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
library (ggplot2)
library (ggvis)
x_pos <- factor ( c (5,10,15), labels= letters [1:3])
y_pos = c (5,10,15)
data_frame = data.frame (x_pos, y_pos )
print ( "Data Frame" )
print (data_frame)
data_frame %>%
ggvis (~x_pos,~y_pos) %>%
layer_lines () %>%
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
library (ggplot2)
library (ggvis)
x_pos <- factor ( c (5,10,15), labels= letters [1:3])
y_pos = factor ( c (5,10,15), labels= letters [4:6])
data_frame = data.frame (x_pos, y_pos )
print ( "Data Frame" )
print (data_frame)
data_frame %>%
ggvis (~x_pos,~y_pos) %>%
layer_lines () %>%
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
Last Updated :
30 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...