作图分三类
#1.基础包 略显陈旧 了解一下
plot(iris[,1],iris[,3],col = iris[,5])
#
text(6.5,4, labels = 'hello')
boxplot(iris[,1]~iris[,5])
dev.off()
#关闭画板
#2.ggplot2 中坚力量 学起来有点难
test = iris
if(!require(ggplot2))install.packages('ggplot2')
library(ggplot2)
ggplot(data = test)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species))
#3.ggpubr 江湖救急 ggplot2简化和美化 褒贬不一
if(!require(ggpubr))install.packages('ggpubr')
library(ggpubr)
ggscatter(iris,
x="Sepal.Length",
y="Petal.Length",
color="Species")
# STHDA美图中心:www.sthda.com
重点:ggplot
test = iris
#1.入门级绘图模板
ggplot(data = test)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length))
#2.映射
library(ggplot2)
test = iris
#1.入门级绘图模板
ggplot(data = test)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length))
#2.映射
ggplot(data = test)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species))
ggplot(data = test)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species),
shape = 23,fill = "black")
#3.分面
ggplot(data = test) +
geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) +
facet_wrap(~ Species)
#facet_wrap就是用于分面的函数
#双分面
test$Group = sample(letters[1:5],150,replace = T) #replace = T可放回的抽样的意思
ggplot(data = test) +
geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) +
facet_grid(Group ~ Species)
#练习6-1
# 示例数据:ggplot2中数据集mpg
library(ggplot2)
test1 = mpg
# 1.分别以mpg的displ和hwy两列作为横纵坐标,画点图。
ggplot(data = test1)+geom_point(mapping = aes(x = displ,y = hwy))
# 2.尝试修改颜色或大小,从mpg数据框中任选可以用来分类的列。
ggplot(data = test1)+
geom_point(mapping = aes(x = displ,
y = hwy,color = year))
ggplot(data = test1)+
geom_point(mapping = aes(x = displ,
y = hwy,color = year),color="blue")
# 3.根据class列来分面
ggplot(data = test1) +
geom_point(mapping = aes(x =displ, y = hwy)) +
facet_grid(~ class)
# 4.根据drv和cyl两个变量来分面
ggplot(data = test1) +
geom_point(mapping = aes(x =displ, y = hwy)) +
facet_grid(cyl~ drv)
ggplot(data = test1) +
geom_point(mapping = aes(x =displ, y = hwy)) +
facet_grid(Group ~ cyl)
#4.几何对象
#4.1分组
ggplot(data = test) +
geom_smooth(mapping = aes(x = Sepal.Length,
y = Petal.Length))
ggplot(data = test) +
geom_smooth(mapping = aes(x = Sepal.Length,
y = Petal.Length,
group = Species))
ggplot(data = test) +
geom_smooth(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species))
#4.2图层
#局部映射和全局映射
ggplot(data = test) +
geom_smooth(mapping = aes(x = Sepal.Length,
y = Petal.Length))+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length))
ggplot(data = test,mapping = aes(x = Sepal.Length, y = Petal.Length))+
geom_smooth()+
geom_point()
#练习6-2
# 1.尝试写出下图的代码
# 数据是iris
# X轴是Species
# y轴是Sepal.Width
# 图是箱线图
iris
ggplot(data=iris, mapping =aes(x =Species, y =Sepal.Width)) +
geom_boxplot( ) +
geom_point()+
#绘制箱线图
labs(title = "iris",
x = 'Species', y = 'Sepal.Width')
# 2. 尝试在此图上叠加点图,
# 能发现什么问题?
# 3.用下列代码作图,观察结果
ggplot(data=test,aes(x = Sepal.Length,y = Petal.Length,color = Species)) +
geom_point()+
geom_smooth(color = "black")
ggplot(data=test,aes(x = Sepal.Length,y = Petal.Length,color = Species)) +
geom_point()+
geom_smooth()
# 请问,当局部映射和全局映射冲突,以谁为准?
#局部映射为准
#5.统计变换-直方图
View(diamonds)
table(diamonds$cut)
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut)) #可以发现只有X没有Y
#上下两行的代码运行结果是一模一样的
ggplot(data = diamonds) +
stat_count(mapping = aes(x = cut)) #每个geom函数都有其对应的stat函数
library(ggplot2)
#统计变换使用场景
#5.1.不统计,数据直接做图
fre = as.data.frame(table(diamonds$cut))
fre
ggplot(data = fre) +
geom_bar(mapping = aes(x = Var1, y = Freq), stat = "identity") #stat = "identity"意思是给的数据是多少就写多少
#5.2count改为prop
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1)) #..prop..看起来很别扭
#6.位置关系
# 6.1抖动的点图
ggplot(data = mpg,mapping = aes(x = class,
y = hwy,
group = class)) +
geom_boxplot()+
geom_point()
ggplot(data = mpg,mapping = aes(x = class,
y = hwy,
group = class)) +
geom_boxplot()+
geom_jitter() #将geom_point()换成 geom_jitter()
# 6.2堆叠直方图
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut,fill=clarity))
# 6.3 并列直方图
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
#7.坐标系
#翻转coord_flip()
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot() +
coord_flip() #翻转与coord_flip()
#极坐标系coord_polar()
bar <- ggplot(data = diamonds) +
geom_bar(
mapping = aes(x = cut, fill = cut),
show.legend = FALSE,
width = 1
) +
theme(aspect.ratio = 1) +
labs(x = NULL, y = NULL)
bar + coord_flip()
bar + coord_polar()
library(ggplot2)
iris
ggplot(data = iris,mapping = aes(x = Species,y = Sepal.Width))+
geom_violin(aes(fill = Species))+
geom_boxplot( ) +
geom_jitter(aes(shape = Species))+
coord_flip()
网友评论