ggplot2包之ggplot命令总览
#basic command
ggplot(
#确定数据,坐标轴内容
data, aes(x = one of colname_data, y = another of colname_data))
# addition commands (layers)
ggplot(data, aes(x = one of colname_data, y = another of colname_data))+
geom_***() + # Geoms
stat_***() + # stats
geom_***() or stat_***() + # position
geom_abline() + # annotations
aes_***() + # Aesthetics
labs/lims() + # Scales
theme() # Themes
#save
#method 1
ggsave(filename,
plot = last_plot(),
device = 'png/pdf/tiff/...', #选一个
path = '/data/plot' ,
width = 10, height = 10,
units = "cm/in/mm/px", #选一个
dpi = 320/300/72, #"retina" (320), "print" (300), or "screen" (72)
limitsize = TRUE, #最大50x50 inches
bg = NULL)
#method 2
png("filename", width = 10, height = 10)
ggplot() #之前绘图的内容
dev.off()
# layers
# Geoms
经常用geom_point()进行绘制散点图,先以geom_point()为例总结~可以绘制三种变量之间的变化
ggplot(data, aes(x = one of colname_data, y = other of colname_data)) +
geom_point(aes(color/shape/size = another of colname_data),
#可以进行颜色、大小、分辨率的调整
color = "red", size = 10, alpha = 0.5
)
geom_bar
geom_bar(
mapping = NULL,
data = NULL,
stat = "count",
# 默认count为计数;
# 'identity':为数值;
position = "stack",
# 默认stack为堆叠;
# position = 'dodge':分散(几列数据分几列),加上position=position_dodge(width=0.9)可设置组间柱子分隔宽度;
# position = 'fill':标准化成每组总量为1,也可以表示各类别所占百分比
# position = 'jitter':一般是用于散点图,因为散点图有时会出现相同值的重叠点,可能造成误导,可以给每个点加上随机噪声变成抖点,使重叠点得以分散
...,
just = 0.5,
width = NULL,
# 设置柱子宽度,使变量之间分开(组间)
col = NULL,
# 设置边框颜色
na.rm = FALSE,
orientation = NA,
show.legend = NA,
inherit.aes = TRUE
)
geom_segment
添加线段(具体还没总结),类似于红框中的线段。。
# axis
- 设置坐标轴的范围(不改变其原有数据)
# 局部放大图形,不改变之前的形状与趋势(用所有数据)
plot + coord_cartesian(ylim = c())
- 设置坐标轴的范围(改变其原有数据)
# mehtod 1
+ ylim()
# legend
#guide
设置图例排列顺序
# 图例变成两列,且由行来填充(默认是列)
plot + guides(fill = guide_legend(ncol = 2), byrow = TRUE)
# 颠倒图例排列顺序
plot + guides(fill = guide_legend(reverse = TRUE))
# 覆盖从每个图层派生的一些图形属性设置,即只更改图例中的图层,不改变图中的结果(俺自己的理解)
# 将图例中的图形透明度调整为100%
plot + guides(colour = guide_legend(override.aes = list(alpha = 1)))
调整图例坐标轴位置及刻度显示
# 水平或垂直
guides(size = guide_bins(direction = "horizontal"/"vertical"))
# 不显示刻度尺
plot + guides(size = guide_bins(axis = FALSE))
# 显示范围、坐标轴颜色、刻线宽度以及刻线箭头
show.limits,axis.colour, axis.linewidth、axis.arrow
调整图例大小以及显示图例范围
plot + guides(colour = guide_colourbar(reverse = TRUE))
# 颠倒图例顺序(由小到大 转为 由大到小)
+ guides(colour = guide_colourbar(barheight = unit(2, "cm")))
# 更改图例长度为2cm
+ guides(colour = guide_coloursteps(show.limits = TRUE))
# 显示图例范围的极值
提取出坐标点通过ggplot包绘制并加圈层、底色
# 添加置信区间 即画椭圆圈层、并填充
stat_ellipse()
#增添坐标轴
theme_dr(xlength = 0.2, #x轴长度
ylength = 0.2, #y轴长度
arrow = grid::arrow(length = unit(0.1, "inches"), #箭头大小/长度
ends = 'last', type = "closed")) + #箭头描述信息
#增加标签信息,即细胞注释图层
取每类细胞类型坐标点的中位数 + geom_text()
持续总结