2017-10-09 252 views
-2

我的R中的數據集如下所示:[R GGPLOT2:複雜的堆疊條形圖與多個分類變量

a <- c("M","F","F","F","M","M","F","F","F","M","F","F","M","M","F") 
p <- c("P","P","W","W","P","P","W","W","W","W","P","P","P","W","W") 
y1 <- c("yes","yes","null","no","no","no","yes","null","no","yes","yes","yes","null","no","no") 
y2 <- c("yes","null","no","no","no","yes","yes","yes","null","no","yes","null","no","yes","yes") 
y3 <- c("no","no","no","yes","null","yes","null","no","no","no","yes","yes","null","no","no") 
VE <- data.frame(gender = a, 
      type = p, 
      y1 = y1, 
      y2 = y2, 
      y3 = y3) 

而且我想創建一個柱狀圖,看起來像這樣: ideal bar chart

我只是想出了很長的路要得到圖表:

q<-data.frame(gender=VE$gender, 
      year=rep("y1",15), 
      group=VE$y1) 
p<-data.frame(gender=VE$gender, 
      year=rep("y2",15), 
      group=VE$y2) 
x<-data.frame(gender=VE$gender, 
      year=rep("y3",15), 
      group=VE$y3) 
Table<-rbind(q,p,x) 
ggplot(Table, aes(year)) + geom_bar(aes(fill=group), position = "stack") + facet_grid(gender~.) 

有沒有更好的方式來獲取條形圖? (因爲我原本打算處理3,000,000個具有各自32個變量的迷惑) 請給我一些這種條形圖的幫助。乾杯!

+3

顯示的圖像數據並不能使它容易複製。閱讀[this](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)和幫助並編輯你的問題。或者搜索你的[問題](https://stackoverflow.com/questions/38877580/stacked-bar-plot-with-4-categorical-variables-in-r?rq=1)這是在相關的問題上畢竟,你的問題的一面。 – shea

+0

[R中有4個分類變量的堆積條形圖可能重複](https://stackoverflow.com/questions/38877580/stacked-bar-plot-with-4-categorical-variables-in-r) – shea

回答

0

首先,您可以融化data.frame以獲得'long'格式。爲此,我創建了一個ID變量,將3個變量'y1,'y2'和'y3'放在一起成爲一個變量。 然後,您可以使用ggplot2並使用geom_bar(),如果不提供y審美性,它將計算x美學中的值。

library(ggplot2) 

# create data frame 
df <- data.frame(ID = 1:15, 
      gender = c('M', 'F', 'F', 'F', 'M', 'M', 'F', 'F', 'F', 'M', 'F', 'F', 'M', 'M', 'F'), 
      type = toupper(c('p', 'p', 'w', 'w', 'p', 'p', 'w', 'w', 'w', 'w', 'p', 'p', 'p', 'W', 'W')), 
      y1 = c('yes', 'yes', 'null', 'no', 'no', 'no', 'yes', 'null', 'no', 'yes', 'yes', 'yes', 'null', 'no', 'no'), 
      y2 = c('yes', 'null', 'no', 'no', 'no', 'yes', 'yes', 'yes', 'null', 'no', 'yes', 'null', 'no', 'yes', 'yes'), 
      y3 = c('no', 'no', 'no', 'yes', 'null', 'yes', 'null', 'no', 'no', 'no', 'yes', 'yes', 'null', 'no', 'no'), 
      stringsAsFactors = TRUE) 

# melt data frame to long format 
df_melt <- data.table::melt(df[, c(1, 4:6)], id.vars = "ID") 

# set correct levels for factor (needed for the legend) 
df_melt$value <- factor(df_melt$value, levels = c("yes", "no", "null")) 

# add ggplot 
ggplot(data = df_melt) + 
    geom_bar(aes(x = variable, fill = value, colour = value)) + 
    ylab("count") + 
    xlab("year") 

將返回:

output_ggplot

+0

基本上,它是「重塑」包中的melt()函數自動實現了我的長程變換。謝謝! –