今天,小編想跟大家分享一下如何用我們自己的數據繪製帶有誤差線的柱狀圖。首先,放一張圖吧!

這是小編自己編造的一組數據繪製而成的,主要想介紹給小夥伴們方法。

準備:第一次需要安裝ggplot2和相關包,我們打開Rstudio,在交互界面輸入install.packages("ggplot2"),選擇相應的國內鏡像站點。安裝好後,以後使用就不需要再安裝了。

接下來,我們鍵入代碼library(ggplot2),這一步的操作是載入ggplot2這個包(也可以理解為調用), 因為小編想要演示用自己的excel數據來繪圖,所以這裡我們還要載入一個讀取excel的包,在交互界面輸入library(readxl)

接著我們就導入數據,小編準備了一個excel的數據,放在了桌面,內容如下

現在我們就通過代碼導入R中,sample <- read_excel("C:\Users\Administrator\Desktop\示例.xlsx") ,其中sample是小編創建的變數,後面是一個導入函數,括弧裏是文件路徑。

接下來我們就開始準備數據了,在交互界面輸入以下代碼sample_long = with(sample,

rbind( data.frame( lesion=group, stimulus= "test1", lat=test1, sem=sd ),

data.frame( lesion=group, stimulus= "test2", lat=test2, sem=sd) )

)

sample_long$lesion=factor(sample_long$lesion, levels=unique(sample_long$lesion))

這裡小編再次命名了一個變數sample_long,並為其賦值。這裡的with可以理解為sample_long的取值是在變數sample中, rbind可以理解為將兩組取值連接起來賦予sample_long。在括弧中又引入了四個變數,分別是lesion、stimulus、lat、sem,除了stimulus取值為字元串,其餘都是excel表格中的相應的列,代表取那一列的值。sample_long$lesion可以理解為這個變數名代表著sample_long中的lesion,後面的factor可以簡單理解為限制取值範圍,level也是類似作用,限定取值水平。

數據準備好後,我們開始繪圖。代碼如下

figure <- ggplot(data = sample_long, aes(x=lesion, y=lat, fill=stimulus) ) +

geom_errorbar(aes(ymin=lat, ymax=lat+sem, width = 0.2),position=position_dodge(width_=0.90)) +

geom_bar(stat="identity", position="dodge") +

geom_bar(stat="identity", position="dodge", colour="black") +

scale_fill_manual(values=c("grey80", "blue")) +

xlab("實驗分組") +

scale_y_continuous("數據測量值", expand=c(0,0), limits = c(0, 5.5)) +

theme_bw() +

labs(title="示例:帶誤差柱狀圖")+

theme(

panel.grid.major = element_blank(),

panel.grid.minor = element_blank(),

panel.border = element_blank(),

axis.line=element_line(colour="black"),

axis.text.y=element_text(angle=90, hjust=0.5),

legend.position = "left",

plot.title = element_text(hjust=0.5),

legend.key = element_rect()

)

print(figure)

這裡簡單說說每一行代碼的含義,figure是一個變數,看過前幾期文章的小夥伴們應該都知道了。

ggplot(data = sample_long, aes(x=lesion, y=lat, fill=stimulus) )是對ggplot繪圖函數相關參數設置,data是指使用變數sample_long中的數據,aes(x=lesion, y=lat, fill=stimulus)中aes全稱是aesthetics,怎麼來理解呢?小編認為可以簡單理解為佈局,通過對元素的設置來達到讓圖形更好看,其中x和y指x軸和y軸,fill可以理解為字面意思「填充」,主要體現在圖例的標識上。

+號可以理解為一個連接符。geom_errorbar(aes(ymin=lat, ymax=lat+sem, width = 0.2),position=position_dodge(width_=0.90))這行代碼是對誤差線的設置,ymin指y軸上最小值,ymax指y軸上最大值。Width指橫向寬度,position就是指位置了。geom_bar(stat="identity", position="dodge") +

geom_bar(stat="identity", position="dodge", colour="black")這裡兩行代碼合在一起講,指圖中的條形圖參數。stat="identity"可以簡單理解為圖形具有同一性,position="dodge"可以理解為圖形不重疊

scale_fill_manual(values=c("grey80", "blue"))這是對兩種圖顏色填充,我們可以按需求更改。xlab("實驗分組")是x軸的標題,scale_y_continuous("數據測量值", expand=c(0,0), limits = c(0, 5.5))是y軸題目和刻度設置。theme_bw()是一類主題風格,labs(title="示例:帶誤差柱狀圖")是最上面的標題。theme(

panel.grid.major = element_blank(),

panel.grid.minor = element_blank(),

panel.border = element_blank(),

axis.line=element_line(colour="black"),

axis.text.y=element_text(angle=90, hjust=0.5),

legend.position = "left",

plot.title = element_text(hjust=0.5),

legend.key = element_rect()

)是相關參數的設置,panel.grid.major = element_blank(),

panel.grid.minor = element_blank()是網格線,我們去掉兩行代碼繪製的圖如下

panel.border = element_blank()是指邊框。axis.line=element_line(colour="black")指軸線顏色。legend.position = "left"圖例位置,plot.title = element_text(hjust=0.5)標題居中。legend.key = element_rect()是什麼意思呢?哈哈,小編不說。小夥伴們感興趣自行研究,提個醒,和圖例相關。

好了,今天分享就到這裡.

推薦閱讀:

相關文章