一、散点图
一) 基本
1. plot (x,y)
plot(mtcars$wt,mtcars$mpg)
2. 使用qplot
library (ggplot2)
qplot(mtcars$wt,mtcars$mpg)
qplot(wt, mpg, data = mtcars) ###X,Y来自同一个数据框
3. 使用ggplot2
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
##指定x,y ##指定图的类型,point=散点图
二) 美化
ggplot(data, aes(names , Activated.B.cell)) +
geom_point(aes(color = group1)) +
scale_color_manual(values = c(COLON = "#8ac53e" , LIVER = "#ff00ff"))+ ##改颜色
theme_classic()+ ##经典背景
theme(axis.text.x=element_blank(), ##删除X轴标签
axis.ticks.x=element_blank())+ ##删除X轴刻度
xlab("names") + ##x轴命名
ylab("Activated B cell") + ##y轴命名
scale_y_continuous(breaks = seq(0.2,1.2,0.1)) ##改y轴刻度值
二、折线图、曲线图
1. plot函数: type参数可以指定绘图的类型
plot(pressure$temperature, pressure$pressure, type = "l")
points (pressure$temperature, pressure$pressure) ##points:将点在线上表示出来
2. lines函数
lines(pressure$temperature, pressure$pressure/2, col ="red")
points(pressure$temperature, pressure$pressure/2, col = "red")
3. qplot
library (ggplot2)
qplot (pressure$temperature, pressure$pressure, geom = "line")
qplot (pressure$temperature, pressure$pressure, geom = c("line","point")) ###同时画出线和点
qplot(temperature, pressure,data = pressure, geom = "line") #变量x和y来自于同一个数据框
4. ggplot2
ggplot(pressure, aes(x = temperature, y = pressure)) + geom_line() + geom_point()
三、柱状图
一) 基本画图
1. 使用barplot
barplot(BOD$demand,names.arg = BOD$Time)
##Y轴 ##x轴
2. 使用qplot()
library(ggplot2)
qplot(mtcars$cyl) #连续变量
qplot(factor(mtcars$cyl)) #分类变量
3. ggplot
##单一个X
ggplot(mtcars,aes(cyl))+geom_bar() #连续变量
ggplot(mtcars,aes(factor(cyl)))+geom_bar() #分类变量
##指定X和Y时
stat = "count"(默认):表示一个x对应落到该x的样本数
stat = "identity": 表示一个x对应一个y,
即identity提取横坐标x对应的y值,count提取横坐标的频数
ggplot(BOD,aes(Time,demand)) + geom_bar(stat = "identity") #连续变量
ggplot(BOD,aes(factor(Time),demand)) + geom_bar(stat = "identity")#分类变量
二) 美化
1. 柱状图调色补充和分组
library(ggplot2)
library(gcookbook) ##加载教程表格的R包
ggplot(pg_mean,aes(group, weight)) + geom_bar(stat = "identity") #基本图
1.1 修改柱状图的填充(fill)和描边(color):
ggplot(pg_mean,aes(group, weight)) +
geom_bar(stat = "identity", fill = "lightblue", color = "black")
1.2 通过分组变量设置颜色:
ggplot(cabbage_exp, aes(Date, fill = Cultivar)) +
geom_bar(position = "dodge")
postition参数: 主要是指对图像的微调,最常见的应用是在分组的柱形图(bar)中,因为分组的柱形图会产生组内堆积和不堆积两种主要的效果
position常用参数值:其中stack和dodge最为常用。
"identity"
: 不调整,组内前后重叠;
"stack"
: 堆积, 默认;
"fill"
: 按比例堆积;
"dodge"
: 分散
1.3 通过scale_fill_brewer()
scale_fill_brewer()
修改颜色模式:
ggplot(cabbage_exp, aes(Date, Weight, fill = Cultivar)) +
geom_bar(position = "dodge", stat = "identity", color = "black") +
scale_fill_brewer(palette = "Pastel1")
ColorBrewer 配色: 使用的是scale_colour_brewer()
可以使用RColorBrewer::display.brewer.all()
查看所有的调色板
通过scale_fill_manual
修改颜色
自定义颜色:输入的变量长度与分组变量的长度一致
scale_fill_manual(..., values, aesthetics = "fill", breaks = waiver(), na.value = "grey50")
1.4除了fill可以指定分组变量,color, linestyle也可以指定;
ggplot(cabbage_exp, aes(Date, Weight, color = Cultivar)) +
geom_bar(position = "dodge", stat = "identity")
1.5如果类别变量的组合有任何缺失,则该栏将缺失,相邻的栏将扩展以填充该空间。
ce = cabbage_exp[1:5,]
ggplot(ce, aes(Date, Weight, fill = Cultivar)) +
geom_bar(position = "dodge", stat = "identity", color = "black") +
scale_fill_brewer(palette = "Pastel1")
实际情况下确实存在有一种类别没有对应的y值,此时可以使用NA或者0代替
ce_NA <- cabbage_exp
ce_NA$Weight[6] <- 0
ggplot(ce_NA, aes(Date, Weight, fill = Cultivar)) +
geom_bar(position = "dodge", stat = "identity", color = "black") +
scale_fill_brewer(palette = "Pastel1")
1.6 修改颜色的技巧
upc <- subset(uspopchange, rank(Change) > 40)
ggplot(upc, aes(x = Abb, y = Change, fill = Region)) +
geom_bar(stat = "identity")
1.6.1使用reorder()
函数,将条形按其高度进行排序:
ggplot(upc, aes(x = reorder(Abb, change), y = Change, fill = Region)) +
geom_bar(stat = "identity", color = "black") +
scale_fill_manual(values = c("#669933", "#FFCC66")) +
xlab("State")
reorder()函数:
reorder(x, X, FUN = mean, ..., order = is.ordered(x), decreasing = FALSE)
x: x是要排序的数据,排序后的结果作用于x上
X: 要根据X进行排序
1.6.2 正负两极不同的颜色
新建一个字段;该字段描述来正和负
csub <- subset(climate, Source == "Berkeley" & Year >= 1900)
csub$pos <- csub$Anomaly10y >= 0 #指定fill为该字段
ggplot(csub, aes(x = Year, y = Anomaly10y, fill = pos)) +
geom_bar(stat = "identity", position = "identity")
2. 调整条形的宽度和间距
2.1 width
调整间距
library(gcookbook)
# width默认是0.9 , 最大值只能设置为1:
ggplot(pg_mean, aes(x = group, y = weight)) +
geom_bar(stat = "identity", width = 0.5)
2.2 position_dodge
调节分组条形图之间的间距:
默认的同一分组之间的条形是没有间距的
ggplot(cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar)) +
geom_bar(stat = "identity", width = 0.5, position =
position_dodge(0.7))
##当position_dodge=0的时候,两个柱子会重合
2.3 堆积柱状图:
position的默认值为stack;
即如果不设置position,并且设置了分组变量,就是画堆积图;
2.3.1 'guides()'修改图例堆积的顺序
ggplot(cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar)) +
geom_bar(stat = "identity") +
guides(fill = guide_legend(reverse = T))
2.3.2 修改图形堆积顺序:修改因子水平
cabbage_exp$Cultivar <- factor(cabbage_exp$Cultivar, levels = c("c52", "c39"))
ggplot(cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar)) +
geom_bar(stat = "identity", color = "black") +
guides(fill = guide_legend(reverse = T))
3. 修改标签 geom_text:
3.1 vjust参数
设定标签位置的设定
ggplot(cabbage_exp, aes(x = interaction(Date, Cultivar), y = Weight)) +
geom_bar(stat = "identity") +
geom_text(aes(label = Weight), vjust = 1.5, colour = "white")
###vjust正值在水平线下,负值在水平线上
3.2 为了防止标签跑出图形,调整y轴的范围:
##1. ylim()函数
ggplot(cabbage_exp, aes(x = interaction(Date, Cultivar), y = Weight)) +
geom_bar(stat = "identity") +
geom_text(aes(label = Weight), vjust = -0.2) +
ylim(0, max(cabbage_exp$Weight) * 1.05)
##2. 以weight为基准,调节y值,图形高度会自动适配:
ggplot(cabbage_exp, aes(x = interaction(Date, Cultivar), y = Weight)) +
geom_bar(stat = "identity") +
geom_text(aes(y = Weight + 0.1, label = Weight))
3.3分组柱状图加标签
需要设定position_dodge(),以调整字体适合位置
ggplot(cabbage_exp, aes(x = interaction(Date, Cultivar), y = Weight, fill = Cultivar)) +
geom_bar(stat = "identity", position = "dodge") +
geom_text(aes(label = Weight), vjust = 1.5, color = "white", position = position_dodge(0.9), size = 3)
# width参数默认是0.9,position_dodge(0.9),让标签位于柱子中间
3.4 堆积柱状图添加label
library(plyr)
ce <- arrange(cabbage_exp, Date, Cultivar)
ce <- ddply(ce, "Date", transform, label_y = cumsum(Weight))
ce$Cultivar <- factor(ce$Cultivar, levels = c("c52", "c39"))
ggplot(ce, aes(x = Date, y = Weight, fill = Cultivar)) +
geom_bar(stat = "identity") +
geom_text(aes(y = label_y, label = Weight), vjust = 1.5, colour = "white")
3.5 position_stack
修改标签至中央
ggplot(ce, aes(x = Date, y = Weight, fill = Cultivar)) +
geom_bar(stat = "identity") +
geom_text(aes(label = Weight), position = position_stack(vjust = 0.5),colour = "white")
3.6 添加标签单位,并修改颜色模式
ggplot(ce, aes(x = Date, y = Weight, fill = Cultivar)) +
geom_bar(stat = "identity", colour = "black") +
geom_text(aes(y = label_y, label = paste(format(Weight,nsmall = 2), "kg")), size = 4)
4. 柱状图的拓展:饼图
###1.1 扭曲y轴方向,柱子变弯
ggplot(mpg , aes(class)) + geom_bar() + coord_polar(theta = "y")
###1.2 扭曲X轴 , 柱子从一个点出发
ggplot(mpg , aes(class)) + geom_bar() + coord_polar(theta = "x")
###1.3 加颜色分组
ggplot(mpg , aes(class)) + geom_bar(aes(fill = drv)) +
coord_polar(theta = "y")
###2.1 正常饼图
ggplot(mpg , aes(1 , fill = class)) + ##只有一个柱子的堆积柱状图,按class填充
geom_bar() +
coord_polar(theta = "y") ##扭曲Y轴
###2.2 加标签
ggplot(mpg , aes(1 , fill = class)) +
geom_bar(width = 0.5) +
coord_polar(theta = "y") +
geom_text(stat = "count" , aes(label = scales::percent(..count../100)) ,
size =3 , position = position_stack(vjust = 0.5) )
5. 柱状图拓展: 克里夫兰点图绘制
library(gcookbook)
tophit <- tophitters2001[1:25,]
# 从最基本的散点图出发
ggplot(tophit, aes(x = avg, y = name)) + geom_point()
# reorder排序
ggplot(tophit, aes(x = avg, y = reorder(name, avg))) +
geom_point(size = 3) + ##修改点的大小
theme_bw() + ##去掉灰色背景
theme(panel.grid.major.x = element_blank(), #设置纵向网格线为空
panel.grid.minor.x = element_blank(), #设置纵向网格线为空
panel.grid.major.y = element_line(colour = "grey60", linetype = "dashed"))
#设置横向网格线为虚线
# 若颠倒图形的x轴和y轴,网格线设置x,y对调
ggplot(tophit, aes(x = reorder(name, avg), y = avg))+
geom_point(size = 3) + #修改点的大小
theme_bw() + #修改背景
theme(panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major.x = element_line(colour = "grey60", linetype = "dashed"))
## 绘制彩色克利夫兰点图:
ggplot(tophit, aes(x = avg, y = name)) +
geom_segment(aes(yend = name), xend = 0, colour = "grey50") +
geom_point(size = 3, aes(color = lg)) + # 设置分组变量
scale_colour_brewer(palette = "Set1",limits = c("NL", "AL"))+ #limits限定颜色先后顺序:
theme_bw() +
theme(panel.grid.major.y = element_blank(), ##去除横线网格线
legend.position = c(1, 0.55), ##设置图例的位置:这里的1指的是与x轴的比例;
legend.justification = c(1, 0.5)) ##表示图例右边缘中点;(1,0)表示右下角,(0, 1) 表示左上角,以此类推;
#按照lg和avg对name进行排序,先按lg排,再按avg排
nameorder <- tophit$name[order(tophit$lg, tophit$avg)]
#将name变量转化为因子,因子水平设定位nameorder:
tophit$name <- factor(tophit$name, levels = nameorder)
分面绘制:facet_grid()
函数:
ggplot(tophit, aes(x = avg, y = name)) +
geom_segment(aes(yend = name), xend = 0, colour = "grey50") +
geom_point(size = 3, aes(color = lg)) + # 设置分组变量
scale_colour_brewer(palette = "Set1", limits = c("NL", "AL"),
guide = F) + # guides去除图例:
theme_bw() +
theme(panel.grid.major.y = element_blank()) +
# 一列多行:lg~ 行数等于lg的种类数目;
# scales设置每个分块的单位宽度;space设置每个分块的宽度;
facet_grid(lg~.,scales = "free_y", space = "free_y")
四、直方图
x是连续变量
1. hist函数
hist(mtcars$mpg)
hist(mtcars$mpg, breaks = 10) #break参数指定组数
2. qplot()
qplot(mpg,data = mtcars, binwidth = 1 ) #binwidth参数指定组距
3. ggplot2
ggplot(data = mtcars, aes(x = mpg)) + geom_histogram( binwidth = 1)
五、箱线图
1. 使用plot()函数
plot(ToothGrowth$supp, ToothGrowth$len)
##当x为因子变量(与数值变量对应时),默认绘制箱线图
2. boxplot()函数
##1.
boxplot(len ~ supp, data = ToothGrowth)
#y轴值 #x轴值
#2. 或者
boxplot(ToothGrowth$len~ToothGrowth$supp)
#3. 在x轴引入两变量的交互,可以绘制分组箱线图
boxplot(len ~ supp + dose, data = ToothGrowth)
##分组变量
3. qplot()函数
##1.
qplot(supp , len,data = ToothGrowth, geom = "boxplot")
##x轴 ##y轴
##2.
qplot(interaction(supp,dose),len, data = ToothGrowth,geom = "boxplot")
##使用interaction连接x与分组变量
4. ggplot2
ggplot(data = ToothGrowth, aes(x = interaction(supp,dose), y = len)) + geom_boxplot()
六、绘制函数图像
1. 使用curve()函数绘制
##传入一个关于变量x的表达式
curve(x^3 - 5*x, from = -4, to =4)
2. 自定义函数图像
my_fun <- function(xvar){
1/(1 + exp(-xvar + 10))
}
curve(my_fun(x), from = 0, to =20)
3. 原有的基础上添加一条线
curve(1 - my_fun(x),add = T, col = "red")
4. ggplot2
##1.
ggplot(data.frame(x= c(0,20)),aes(x=x)) + stat_function(fun = my_fun, geom ="line")
##线图
##2. 再加一条线
my_fun2 <- function(xvar){
1- 1/(1 + exp(-xvar + 10))
}
ggplot(data.frame(x = c(0,20)),aes(x =x)) + stat_function(fun = my_fun, geom ="line") + stat_function(fun = my_fun2, geom ="line",color="red")