STEP1:了解數(shù)據(jù)特征
rm(list = ls())
data_test = datasets::attitude
# 因?yàn)楹竺嫦胍霭俜直鹊亩询B柱狀圖,先查看這個(gè)數(shù)據(jù)適不適合
statistics = apply(data_test, 1, sum) # 得到每個(gè)樣本的觀測(cè)值總和
plot(statistics)
# 每個(gè)樣本的累加值不相等秀撇,不能直接用來(lái)做百分比柱狀圖划乖,需要轉(zhuǎn)換下
# 不過(guò)這段僅僅是為了作圖好看杀狡,已經(jīng)準(zhǔn)備好數(shù)據(jù)的可以不看下面的處理
data_percent = data.frame() # 建立空數(shù)據(jù)框
for (n in 1:30) {
data_percent = rbind( data_percent, data_test[n,] / statistics[n] )
}
# 再來(lái)看下肃廓,每個(gè)樣本總和都等于1,現(xiàn)在符合要求了
statistics = apply(data_percent, 1, sum)
plot(statistics)
# 再加上樣本的命名信息虚茶,方便看圖捅暴,已有命名的請(qǐng)忽略
data_percent$names = c(LETTERS[seq( from = 1, to = 15 )],
letters[seq( from = 1, to = 15 )])
STEP2:整理數(shù)據(jù)(變成長(zhǎng)數(shù)據(jù))
#作圖前有個(gè)很重要的前置動(dòng)作,要把寬矩陣轉(zhuǎn)換為長(zhǎng)矩陣
#(具體名詞解釋可以百度,關(guān)鍵原因是計(jì)算機(jī)和人的識(shí)別習(xí)性是不同的)
library(reshape2)
data_plot = melt(data_percent)
colnames(data_plot) = c('name','attitude','percent')
group = c( rep('Upper',15), rep('Lower',15))
data_plot$group = rep(group,7)
STEP3:畫圖
STEP3.1:基本圖形
library(ggplot2)
p = ggplot( data_plot, aes( x = name, weight = percent, fill = attitude))+
geom_bar( position = "stack");p
# 如果把 "stack" 改成 "dodge"官研,可以變成分組柱狀圖
p = ggplot( data_plot, aes( x = name, weight = percent, fill = attitude))+
geom_bar( position = "dodge");p
STEP3.2:改顏色
#改顏色
library(ggsci)
p + scale_fill_nejm()
p + scale_fill_manual( values = rainbow(7))
# 還可以自定義
p + scale_fill_manual( values = c('yellow','green','red','blue','brown','black','blue'))
STEP3.3:改標(biāo)簽
# 順帶秽澳,可以把標(biāo)簽給改了
p + xlab('people') + ylab('percent') + scale_fill_nejm()
STEP3.4:數(shù)值排序
#排序的問(wèn)題,
#如果我想調(diào)整不同類型柱子的順序戏羽,讓他們按大小排序担神,可以用factor 函數(shù)
order_x = apply( data_percent[,1:7], 2, sum) # 查看各種 attitude 的總和
order_x = order_x[order(order_x, decreasing = T)] # decreasing = T 代表是倒序
order_x # 看一下,是從大到小排著的
# 此時(shí) data_plot數(shù)據(jù)框里面的 attitude 就按照給定的 levels 排序了
data_plot$attitude = factor(data_plot$attitude,
levels = names(order_x) ,
ordered = T )
p2 = ggplot( data_plot,aes(x = name, weight = percent, fill = attitude))+
geom_bar(position = "stack")
p2
STEP3.5:樣本排序
# 還可以給樣本排序始花,同樣的道理
names = c(LETTERS[seq( from = 1, to = 15 )],
letters[seq( from = 1, to = 15 )])#擬定的樣本順序
data_plot$name = factor(data_plot$name,
levels = names ,
ordered = T )
p3 = ggplot(data_plot,aes(x = name, weight = percent, fill = attitude))+
geom_bar(position = "stack")
p3
STEP3.6:分組展示
# 可以考慮分組展示
p3 + facet_wrap(~group, scales = 'free', nrow = 2)
STEP3.7:用箱圖查看整體分布情況
# 可以查看各種attitude的整體情況
ggplot(data_plot, aes(x = attitude, y = percent,
fill = attitude))+
geom_boxplot()
STEP3.8:線圖
# 另外還可以做成線圖妄讯,不過(guò)這個(gè)數(shù)據(jù)做出來(lái)不好看
ggplot(data_plot, aes(x =name, y = percent,
group =attitude, color = attitude))+
geom_line()+
scale_color_nejm()
STEP3.8:在堆疊圖上標(biāo)數(shù)字(大概就是這個(gè)意思,圖太丑了)
p = ggplot( data_plot, aes( x = name, weight = percent, fill = attitude))+
geom_bar( position = "stack")+
geom_text(aes(label = percent,y=percent),
position = position_stack(vjust = 0.5), size = 1.5);p
整理自
https://cloud.tencent.com/developer/article/1819219
https://www.javaroad.cn/questions/124111
https://cloud.tencent.com/developer/ask/109270
http://www.reibang.com/p/ac615ba65ab6