簡體   English   中英

如何制作比例面積圖?

[英]How do I make a proportional area chart?

我目前正在嘗試獲得與該頁面上相似的比例數據圖表( http://www.improving-visualisation.org/vis/id=148 )。

我正在使用的數據是從CSV中提取的,該CSV是從1993年至2003年(包括首尾兩天)按性別和課程類型分層的任何年份的大學畢業生人數。 我已將csv拆分為“ df_list”,可以在此處看到1993。

df_list [1] $ 1993

   year     sex                            type_of_course no_of_graduates
1  1993   Males                                 Education              na
2  1993   Males                              Applied Arts              na
3  1993   Males              Humanities & Social Sciences             481
4  1993   Males                        Mass Communication              na
5  1993   Males                               Accountancy             295
6  1993   Males                 Business & Administration             282
7  1993   Males                                       Law              92
8  1993   Males Natural, Physical & Mathematical Sciences             404
9  1993   Males                                  Medicine              95
10 1993   Males                                 Dentistry              14
11 1993   Males                           Health Sciences              10
12 1993   Males                    Information Technology             264
13 1993   Males                   Architecture & Building             132
14 1993   Males                      Engineering Sciences            1496
15 1993   Males                                  Services              na
16 1993 Females                                 Education              na
17 1993 Females                              Applied Arts              na
18 1993 Females              Humanities & Social Sciences            1173
19 1993 Females                        Mass Communication              na
20 1993 Females                               Accountancy             396
21 1993 Females                 Business & Administration             708
22 1993 Females                                       Law              93
23 1993 Females Natural, Physical & Mathematical Sciences             588
24 1993 Females                                  Medicine              61
25 1993 Females                                 Dentistry              11
26 1993 Females                           Health Sciences              40
27 1993 Females                    Information Technology             215
28 1993 Females                   Architecture & Building             144
29 1993 Females                      Engineering Sciences             254
30 1993 Females                                  Services              na

我知道下一步將針對每一門課程制作每年的比例條形圖,我該如何精確地做到這一點? 我目前正試圖將男性和女性合並為一排。

由於數據不可復制,因此未經測試。 但是我認為類似的事情會起作用:

library(ggplot2)

ggplot(dflist, aes(x=sex, y=no_of_graduates, fill=type_of_course)) +
   geom_bar(stat="identity")

或一個簡單的虹膜示例:

library(dplyr)

iris %>%
  mutate(DummyXVar="DummyX") %>%
  ggplot(aes(y=Petal.Width, x=DummyXVar,fill=Species)) +
  geom_bar(stat="identity")

高溫超導

盡管最后一個問題是獲取圖形,但似乎更多的是處理data.frame的問題。

玩具數據集的可重現示例:

year <- c(rep("1993",6), rep("1994",6))
sex <- c(rep("males",3), rep("females", 3))
course <- c(rep(letters[1:3],4))
number <- 1:12*10
data <- data.frame(cbind(year, sex, course, number))
data$number <- as.numeric(data$number)
data$number[1] <- NA

並統一number變量,而不考慮sex變量。

library(dplyr)
df <- data %>% group_by(year, course) %>% summarise(total=sum(number, na.rm=TRUE))     
library(plyr)
df_2 <- ddply(df, .(year), transform, label_y=cumsum(total))

腳注(1)

獲取所需的圖表

library(ggplot2)
ggplot(df_2, aes(x=year, y=total, fill=course)) + geom_bar(stat="identity") + 
      geom_text(aes(y=label_y ,label=total), vjust=3, colour="white")  

在此處輸入圖片說明

(1)加載包和play dplyr非常敏感。 有些功能已修改,必須再次離開R才能再次重復該過程。我還沒有找到更好的方法。

沒有可用的數據,我生成了一個可供其他人使用並改進的小樣本:

library(dplyr)
library(ggplot2)
set.seed(1)
year <- rep("1993", 20)
sex <- rep(c("M","F"), each = 10)
course <- rep(letters[1:10], 2)
num <- round(runif(20, min = 49, max = 120))
df <- data.frame(year, sex, course, num)
df <- tbl_df(df)
df
Source: local data frame [20 x 4]

     year    sex course   num
   (fctr) (fctr) (fctr) (dbl)
1    1993      M      a    68
2    1993      M      b    75
3    1993      M      c    90
4    1993      M      d   113
5    1993      M      e    63
6    1993      M      f   113
7    1993      M      g   116
8    1993      M      h    96
9    1993      M      i    94
10   1993      M      j    53
11   1993      F      a    64
12   1993      F      b    62
13   1993      F      c    98
14   1993      F      d    76
15   1993      F      e   104
16   1993      F      f    84
17   1993      F      g   100
18   1993      F      h   119
19   1993      F      i    76
20   1993      F      j   104

請注意,我使用字母作為課程名稱。 現在,我將創建一個標簽數據集:

mylab <- data.frame(x = factor(df$course), 
         y = rep(c(0.3, 0.8), each = 10), l = df$num)

最后到劇情:

ggplot(data = df, aes(x = factor(course), y = num, fill=factor(sex))) +
     geom_bar(stat = "identity", position = "fill") + 
     geom_text(data = mylab, aes(x = x, y = y, label = l))

輸出為: 在此處輸入圖片說明

當然,現在可以使用標簽和標題了。
希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM