R语言 如何用ggvis标注绘图的刻度线
在这篇文章中,我们将研究在R编程语言中使用ggvis标记刻度线的方法。
然后,使用管道操作符对数据框进行ggvis操作。ggvis方法是用来启动ggvis图形窗口的。ggvis方法的语法如下。
ggvis( data , mp1, mp2.,)
参数 :
- data – 要绘制的数据集
- mp1, mp2,… – 要绘制的地图变量。
ggvis包中的layer_lines()方法是用来按x变量排列顺序的,默认情况下。该方法的语法如下。
layer_lines()
然后添加add_axis方法来为绘制的图形的坐标轴进行标注。它可以用来覆盖坐标轴的默认值。
add_axis( vis, axes , values = )
参数
- vis – 用于绘图的ggvis对象
- axes – 用于绘图的坐标轴
- values – 用于绘制标签的刻度线的值。
# 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)
)
输出
[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

也可以通过指定轴的标签来为轴添加自定义的标签。轴的标签也可以是字符串值,在使用的点上绘制出来。
# 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)
输出
[1] "Data Frame"
x_pos y_pos
1 a d
2 b e
3 c f

也可以在x轴和y轴上分别添加标签。用户定义的标签可以被设置为绘制ggvis图的轴标签的刻度线。
# 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)
输出
[1] "Data Frame"
x_pos y_pos
1 a d
2 b e
3 c f

极客教程