R 图表入门
- 散点图
你有两个向量,你想绘制它们。
x_values <- rnorm(n = 20 , mean = 5 , sd = 8) #20 values generated from Normal(5,8)
y_values <- rbeta(n = 20 , shape1 = 500 , shape2 = 10) #20 values generated from Beta(500,10)
如果要创建垂直轴为 y_values
且水平轴为 x_values
的绘图,可以使用以下命令:
plot(x = x_values, y = y_values, type = "p") #standard scatter-plot
plot(x = x_values, y = y_values, type = "l") # plot with lines
plot(x = x_values, y = y_values, type = "n") # empty plot
你可以在控制台中键入 ?plot()
以阅读更多选项。
- 箱形图
你有一些变量,你想检查他们的分布
#boxplot is an easy way to see if we have some outliers in the data.
z<- rbeta(20 , 500 , 10) #generating values from beta distribution
z[c(19 , 20)] <- c(0.97 , 1.05) # replace the two last values with outliers
boxplot(z) # the two points are the outliers of variable z.
- 直方图
绘制直方图的简便方法
hist(x = x_values) # Histogram for x vector
hist(x = x_values, breaks = 3) #use breaks to set the numbers of bars you want
- 饼状图
如果你想要显示变量的频率,只需绘制饼图
首先,我们必须生成具有频率的数据,例如:
P <- c(rep('A' , 3) , rep('B' , 10) , rep('C' , 7) )
t <- table(P) # this is a frequency matrix of variable P
pie(t) # And this is a visual version of the matrix above