馬上放假了,今天我們來(lái)點(diǎn)基礎(chǔ)的東西,畫(huà)圖的操作。
不想要這么丑的圖。
圖片
要優(yōu)雅的輸出結(jié)果
圖片
「ggplot2中柱狀圖基本繪制函數(shù)常用geom_bar()」
參數(shù)介紹:
「data和mapping是ggplot的基本參數(shù),數(shù)據(jù)和映射?!?/strong> mapping:使用aes函數(shù)指定,為aesthetic attributes的縮寫(xiě)。但字符串映射使用aes_string。aes:顏色(color顏色或邊框顏色、fill填充顏色和 alpha透明度) 形狀(linetype線型、size點(diǎn)的大小或線的寬度和 shape形狀) 位置 (x, y, xmin, xmax, ymin, ymax, xend, yend) 指定數(shù)據(jù)分組和順序的映射group和order,另一類是字符串映射。
關(guān)于映射的詳細(xì)介紹->
一張統(tǒng)計(jì)圖就是從數(shù)據(jù)到幾何對(duì)象(點(diǎn)、線、條形等)的圖形屬性(顏色、形狀、大小等)的一個(gè)映射。
- ? 數(shù)據(jù)(Data),最基礎(chǔ)的是可視化的數(shù)據(jù)和一系列圖形映射(aesthetic mappings),該映射描述了數(shù)據(jù)中的變量如何映射到可見(jiàn)的圖形屬性。
- ? 幾何對(duì)象(Geometric objects, geoms)代表在圖中實(shí)際看到的點(diǎn)、線、多邊形等。
- ? 統(tǒng)計(jì)轉(zhuǎn)換(Statistical trassformations, stats)是對(duì)數(shù)據(jù)進(jìn)行某種匯總,例如將數(shù)據(jù)分組創(chuàng)建直方圖,或?qū)⒁粋€(gè)二維的關(guān)系用線性模型進(jìn)行解釋。
- ? 標(biāo)度(Scales)是將數(shù)據(jù)的取值映射到圖形空間,例如用顏色、大小或形狀來(lái)表示不同的取值,展現(xiàn)標(biāo)度的常見(jiàn)做法是繪制圖例和坐標(biāo)軸。
- ? 坐標(biāo)系(Coordinate system, coord)描述數(shù)據(jù)是如何映射到圖形所在的平面,同時(shí)提供看圖所需的坐標(biāo)軸和網(wǎng)格線。
- ? 分面(faceting)如何將數(shù)據(jù)分解為子集,以及如何對(duì)子集作圖并展示。
- ? 主題(theme)控制細(xì)節(jié)顯示,例如字體大小和圖形的背景色。
「stat:」 設(shè)置統(tǒng)計(jì)方法,有效值是count(默認(rèn)值) 和 identity,其中,count表示條形的高度是變量的數(shù)量,不能設(shè)定y值。identity表示條形的高度是變量的值;對(duì)于連續(xù)性變量使用bin,轉(zhuǎn)換的結(jié)果使用變量density來(lái)表示。
「position:」 位置調(diào)整,有效值是stack、dodge和fill,默認(rèn)值是stack(堆疊),是指兩個(gè)條形圖堆疊擺放,dodge是指兩個(gè)條形圖并行擺放,fill是指按照比例來(lái)堆疊條形圖,每個(gè)條形圖的高度都相等,但是高度表示的數(shù)量是不盡相同的。
「width:」 條形圖的寬度,是個(gè)比值,默認(rèn)值是0.9
「color:」條形圖的線條顏色
「fill:」 條形圖的填充色
基本演示
讀取ImagJ數(shù)據(jù)及轉(zhuǎn)換
#讀取ImageJ
dat=read.csv("Results.csv")
class(dat)
#加入分組信息
Group=rep(c("NC","A","B","A+B"),each=3)
dat$Group=Group
colnames(dat)
dat=dat[,c("IntDen","Group")]
a=dat[dat$Group=="NC",]
a=mean(a$IntDen)
dat$Relative=dat$IntDen/a
柱狀圖的順序是由因子水平?jīng)Q定的,可以手動(dòng)設(shè)置。
dat$Group=factor(dat$Group,levels = c("NC","A","B","A+B"))
基本畫(huà)圖操作
配色方案->最優(yōu)質(zhì)配色包 注意先按照某一列數(shù)據(jù)分色,然后手動(dòng)填充顏色
library(ggplot2)
library(ggpubr)
ggplot(dat,aes(x=Group,y=Relative,fill=Group))+
geom_bar(stat="identity",width = 0.5,color="black")+
scale_fill_manual(values=c("#017A4AFF", "#FFCE4EFF", "#3D98D3FF","#FF363CFF"))+
theme_minimal()#主題皮膚
數(shù)據(jù)調(diào)整及誤差線增加
在ggplot2中可以直接結(jié)合stat_summary函數(shù)快速進(jìn)行數(shù)據(jù)統(tǒng)計(jì)
所以stat可以設(shè)置為summary,將柱狀圖的高度設(shè)置為各組的均值并聯(lián)合stat_summary函數(shù)增加誤差線。
ggplot(dat,aes(x=Group,y=Relative,fill=Group))+
geom_bar(stat="summary",fun=mean,width = 0.5,color="black")+
scale_fill_manual(values=c("#017A4AFF", "#FFCE4EFF", "#3D98D3FF","#FF363CFF"))+
stat_summary(fun.data = 'mean_sd', geom = "errorbar", colour = "black",
width = 0.25,position = position_dodge( .9))+
theme_minimal()
增加抖動(dòng)的點(diǎn)圖
ggplot(dat,aes(x=Group,y=Relative,fill=Group))+
geom_bar(stat="summary",fun=mean,width = 0.5,color="black")+
scale_fill_manual(values=c("#017A4AFF", "#FFCE4EFF", "#3D98D3FF","#FF363CFF"))+
stat_summary(fun.data = 'mean_sd', geom = "errorbar", colour = "black",
width = 0.25,position = position_dodge( .9))+
geom_jitter( size =5,
alpha = 0.5,
shape = 21) +
theme_minimal()
調(diào)整下字體大小和線條
p <- ggplot(dat,aes(x=Group,y=Relative,fill=Group))+
geom_bar(stat="summary",fun=mean,width = 0.5)+
scale_fill_manual(values=c("#017A4AFF", "#FFCE4EFF", "#3D98D3FF","#FF363CFF"))+
stat_summary(fun.data = 'mean_sd', geom = "errorbar", colour = "black",
width = 0.25,size=1,position = position_dodge( .9))+
geom_jitter( size =5,
alpha = 0.5,
shape = 21,stroke = 1) +
theme_minimal()+
theme(axis.text.x = element_text(size=20),
axis.text.y = element_text(size=20),
axis.title.x = element_text(size=20),
axis.title.y = element_text(size=20),
legend.title= element_text(size=20),
legend.text= element_text(size=15))
組間差異性分析
使用stat_compare_means()函數(shù)。
my_comparisons <- list(c("NC", "A"), c("NC", "B"), c("B", "A+B"))
p <- ggplot(dat,aes(x=Group,y=Relative,fill=Group))+
geom_bar(stat="summary",fun=mean,width = 0.5)+
scale_fill_manual(values=c("#017A4AFF", "#FFCE4EFF", "#3D98D3FF","#FF363CFF"))+
stat_summary(fun.data = 'mean_sd', geom = "errorbar", colour = "black",
width = 0.25,size=1,position = position_dodge( .9))+
geom_jitter( size =5,
alpha = 0.5,
shape = 21,stroke = 1) +
theme_minimal()+
theme(axis.text.x = element_text(size=20),
axis.text.y = element_text(size=20),
axis.title.x = element_text(size=20),
axis.title.y = element_text(size=20),
legend.title= element_text(size=20),
legend.text= element_text(size=15))+
stat_compare_means(comparisons=my_comparisons,method = "t.test",
label = "p.forma",bracket.size = 1,size=5)
p
ggsave(p,filename = "westernblot.png")
要放假了,簡(jiǎn)單學(xué)習(xí)一下就好,大家好好休息,生活很好,等你超越~~~