生信技能樹2021生信入門線上課筆記航邢,需要結合課程講解服用
常見可視化R包
作圖:base, ggplot2, ggpubr
拼圖:par里的mfrow, grid.arrange, cowplot, customLayout,patchwork
導出:pdf()等三段論, ggsave, eoffice---to pptx
length(unique(mpg$class))#class一列取值的個數(shù)
ggplot2習題
習題.png
ggplot(data = iris,aes(x = Species, y = Sepal.Width)) +
geom_violin(aes(fill = Species)) +#用映射設置多個對象
geom_boxplot()+
geom_jitter(aes(shape = Species))+#點圖酪术,抖動
coord_flip()#翻轉(zhuǎn)
#注意圖層順序啤斗,注意圖的合理性
ggpubr 可以兩兩比較
ggpubr.png
library(ggpubr)
ggscatter(iris,x="Sepal.Length",
y="Petal.Length",
color="Species")
p <- ggboxplot(iris, x = "Species",
y = "Sepal.Length",
color = "Species",
shape = "Species",
add = "jitter")#可以賦值
p
my_comparisons <- list( c("setosa", "versicolor"),
c("setosa", "virginica"),
c("versicolor", "virginica") )#兩兩比較的列表
p + stat_compare_means(comparisons = my_comparisons)+ # Add pairwise comparisons p-value
stat_compare_means(label.y = 9) #P值放的位置對應的縱坐標的值
labs()用于設置圖表標題子標題奈揍、坐標名稱等
p <- ggplot(mtcars, aes(mpg, wt, colour = cyl)) + geom_point()
p + labs(colour = "Cylinders")#把顏色標尺的標題設置為Cylinders
p + labs(x = "New x label")#把橫坐標標題設置為New x label
# The plot title appears at the top-left, with the subtitle
# display in smaller text underneath it
#圖表的標題默認設置在左上角蜡感,副標題以小一點的字號出現(xiàn)在主標題下面
p + labs(title = "New plot title")
p + labs(title = "New plot title", subtitle = "A subtitle")
# The caption appears in the bottom-right, and is often used for
# sources, notes or copyright
#圖片說明默認設置在右下角,用于注釋
p + labs(caption = "(based on data from ...)")
# The plot tag appears at the top-left, and is typically used
# for labelling a subplot with a letter.
#圖片在左上角加tag和標題
p + labs(title = "title", tag = "A")
# If you want to remove a label, set it to NULL.
#去掉某個設置薇组,設置為NULL
p + labs(title = "title") + labs(title = NULL)
ggplot(mtcars, aes(mpg, wt, colour = cyl)) +
geom_point() +
xlab(label = "New x lab") +#設置x軸標題
ylab(label = "New y lab") + #設置y軸標題
ggtitle(label = "This is title", subtitle = "This is subtitle")#設置標題和子標題
代碼可運行卻不出圖---因為畫板被占用
dev.off()關閉畫板外臂,多次運行直到null device,再運行律胀,后者dev.new()
一些教程補充
-畫圖合集
http://www.reibang.com/nb/35523479
-配色RColorBrewer
http://www.reibang.com/p/194765c8c17e
-patchwork拼圖
https://mp.weixin.qq.com/s/p7LLLvzR5LPgHhuRGhYQBQ
-ggplot2作圖theme主題設置
https://blog.csdn.net/u014801157/article/details/24372531