R学习-hist与ggplot练习题-2018-12-04

调查所在班级同学身高情况,并使用excel表记录具体如下:


image.png

1,将excel数据表转换成csv格式,并将文件数据读取到R软件中;
2,将excel表中的数据复制到txt中,并将文件数据读取到R软件中;
3,将2中复制的数据直接读取到R软件中;
4,使用函数hist()画出班级身高的次数分布图和频率直方图;
5,安装ggplot2软件包,使用函数ggplot()换出班级身高的次数分布图和频率直方图;
6,hist()和ggplot()两种函数画出的图有什么区别?

hist的语法:

hist(x, breaks = "Sturges",
     freq = NULL, probability = !freq,
     include.lowest = TRUE, right = TRUE,
     density = NULL, angle = 45, col = NULL, border = NULL,
     main = paste("Histogram of" , xname),
     xlim = range(breaks), ylim = NULL,
     xlab = xname, ylab,
     axes = TRUE, plot = TRUE, labels = FALSE,
     nclass = NULL, warn.unused = TRUE, ...)

数据如上图,分别存在csv中,和txt中,下面介绍几种读取和画图的方法:

# NO.1
data = read.csv("C:\\Users\\cy006\\Desktop\\01.R_test.csv", header=T)

# NO.2
data = read.table("C:\\Users\\cy006\\Desktop\\01.R_test.txt", header=T)

# NO.3
ID = c(0,1,2,3,4,5)
Gender = c('male','female','male','female','male','female')
Height = c(178,156,170,158,168,155)

# NO.4
# 次数直方图或频数直方图,
hist(Height,freq = T)
# 频率直方图
hist(Height,freq = F)
#NO.5
#install.packages("ggplot2")
library(ggplot2)

ggplot(data, aes(x=Height, y=..count..  )) + geom_histogram(stat="bin", binwidth = 4, colour="black", fill="white")
ggplot(data, aes(x=Height, y=..density..)) + geom_histogram(stat="bin", binwidth = 4, colour="black", fill="white")

第NO.4题图:
次数分布图,纵坐标名字为frequency,


image.png

频率直方图,纵坐标名称为density,


image.png

第NO.5题图:
image.png

density:
image.png

你可能感兴趣的:(R学习-hist与ggplot练习题-2018-12-04)