繪制簡(jiǎn)單條形圖及簇狀條形圖

? ? ? ? ? ? ? ? ? ? ? ? ? ? ?R 語言繪制簡(jiǎn)單條形圖

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?參考書籍:R數(shù)據(jù)可視化手冊(cè)

1、使用barplot()函數(shù)繪制簡(jiǎn)單條形圖

library(datasets)#數(shù)據(jù)來源

library(ggplot2)

barplot(BOD$demand,names.arg= BOD$Time)


? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?數(shù)據(jù)展示


? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?繪圖結(jié)果

2、使用ggplot()函數(shù)繪制簡(jiǎn)單條形圖

library(datasets)

library(ggplot2)

ggplot(BOD,aes(x=Time,y=demand))+geom_bar(stat="identity")

ggplot(BOD,aes(x=factor(Time),y=demand))+geom_bar(stat="identity")

ggplot(BOD,aes(x=Time,y=demand))+geom_bar(stat="identity")+geom_bar(stat="identity",fill="lightblue",colour="black")

#fill修改填充色顏色,colour修改邊框顏色

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Time是數(shù)值型(連續(xù)型)變量

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?factor()函數(shù) 將Time轉(zhuǎn)化為離散型變量(分類變量)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 美化條形圖


3、繪制簇狀條形圖

?

library(gcookbook)

#library(RColorBrewer)

#其中cultivar,data是分類變量,weight是數(shù)值。

ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+geom_bar(position= "dodge",stat ="identity" )

?#添加colour修改為黑色邊框,使用scale_fill_brewer()或scale_fill_manual()函數(shù)

P<-ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+geom_bar(position= "dodge",stat ="identity",colour="black" )

P+scale_fill_brewer(palette="Greens")

P+scale_fill_brewer(palette=1)

P+scale_fill_brewer(palette="Set2")


P+scale_fill_manual(values=c("lightpink1","lightsalmon"))

P+scale_fill_manual(values=c("419","424"))

P+scale_fill_manual(values=c("#FFAEB9","#FFA07A"))


? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? palette="Greens

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "palette=2

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?palette="Set2"

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"lightpink1","lightsalmon"

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "419","424"

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "#FFAEB9","#FFA07A"

自我總結(jié):

繪制簡(jiǎn)單條形圖,主要有三個(gè)重要的函數(shù):

barplot(y,names.arg=x)

# y描述條形的高度,x描述每個(gè)條形對(duì)應(yīng)的標(biāo)簽

ggplot(data,aes(x,y))+geom_bar(stat="identity")

注意:qplot()函數(shù)也可以實(shí)現(xiàn),但是我ggplot無法出圖。

qplot(x,y,data,geom="bar",stat= "identity")

qplot(data$x,data$y,geom="bar",stat= "identity")

其中,如果BOD不知,可以使用help ,操作如下,

??BOD右邊窗口有詳細(xì)的示例

鏈接示例數(shù)據(jù)集:共收集了3種方式:分別是library(datasets)、attach(mtcars)、datasets::mtcars。以上是我對(duì)簡(jiǎn)單條形圖的總結(jié),望大家批評(píng)和指正,我們一起學(xué)習(xí)。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容