R 如何制作比例面积图?

R 如何制作比例面积图?,r,R,我目前正试图得到一个与本页()中找到的相似的比例数据图 我所使用的数据是从1993年至2003年(含)任何一年按性别和课程类型分层的大学毕业生人数CSV中提取的。我已经将csv分为“df_列表”——1993年的列表可以在这里看到 df_列表[1]$1993 year sex type_of_course no_of_graduates 1 1993 Males

我目前正试图得到一个与本页()中找到的相似的比例数据图

我所使用的数据是从1993年至2003年(含)任何一年按性别和课程类型分层的大学毕业生人数CSV中提取的。我已经将csv分为“df_列表”——1993年的列表可以在这里看到

df_列表[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")
或者一个简单的iris示例:

library(dplyr)

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

HTH

虽然最后一个问题是获取一个图形,但似乎更多的是处理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
脚注(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) 加载包和播放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
输出为:

当然,现在可以使用标签和标题了。

希望能有所帮助。

此处提供的数据目前在R会话中无法使用,这使得这个问题几乎无法回答。请编辑问题。您的问题到底是什么?如何制作图表?如何以您想要的格式获取数据?能否向我们展示
dput(df_列表)
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))