如何计算ggplot百分比

时间:2019-10-26 01:33:52

标签: r ggplot2

我有以下代码:

dat:
         'data.frame':  261 obs. of  22 variables:
         $ name            : Factor w/ 261 levels "Some Person",..: 28 32 38 171 216 257 37 43 48  63 ...
         $ title           : Factor w/ 257 levels ""," Some title of Text",..: 181 11 212 60 221   100 105 85 189 56 ...
         $ s          : Factor w/ 85 levels "Alberta",..: 56 51 32 77 15 15 56 43 6 67 ...
         $ year            : int  1992 1992 1992 1992 1992 1992 1993 1993 1993 1993 ...
         $ N               : int  0 0 0 0 0 0 0 0 0 0 ...
         $ Z         : Factor w/ 202 levels "","Some Person",..: 35 8 95 4 117 NA NA 68 115 46 ...
         $ Z2        : Factor w/ 150 levels "","Some; Other; People ",..: NA NA NA NA NA NA NA 95 NA 138 ...
         $ P        : Factor w/ 5 levels "Academic","Business",..: 1 1 1 1 2 1 1 1 1 1 ...
         $ E      : int  1 1 1 1 0 1 1 1 1 1 ...
         $ P1             : Factor w/ 7 levels "AD","AP","B",..: 5 5 5 6 NA 5 5 5 5 5 ...
         $ H               : int  11 8 1 18 NA 13 1 14 NA 33 ...
         $ RF              : int  4 4 2 4 NA 4 1 1 NA 2 ...
         $ Em          : Factor w/ 231 levels " Austin","head of dept",..: 92 187 51 84 21 163 26 149 47 24 ...
        $ M1          : int  NA 1 NA NA NA NA NA 0 NA NA ...
        $ M2            : Factor w/ 217 levels ""," some methods used in writing": NA 208 NA NA NA 1 NA 40 NA NA ...
        $ M3     : Factor w/ 3 levels "M","Ql", "Qt",..: NA NA NA NA NA NA NA NA NA NA ...
        $ F  : int  0 0 0 1 1 1 1 1 1 1 ...

我正在尝试创建一个图,其中x轴为“年”,y轴为3个单独的网格/构面中的M3(3级)的百分比,其中F为分组。

我在ggplot和sjplot中尝试了多个代码,但是没有运气。

例如:


dat <- dat1 %>%
   count(year, M3, F)

ggplot(data = dat, mapping = aes(x = year, y = n, color = F)) +
  geom_line() +
  facet_wrap(facets =  vars(M3))

..哪个是下面创建图表的第一组代码,但是它不起作用。

我希望它看起来像这样:

enter image description here

**它有效,但是我试图将“计数”更改为百分比。我可以使用2个变量来完成此操作,这是使第3个变量感到困惑的原因。谢谢。

https://datacarpentry.org/R-ecology-lesson/04-visualization-ggplot2.html

0 个答案:

没有答案