Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
488 views
in Technique[技术] by (71.8m points)

r - 多列频率表是否存在循环以生成条形图?(Is there a loop for frequency table of multiple columns to generate bar plots?)

I have the following data:

(我有以下数据:)

 df1<-read.table(text="Class1 Class2 Class3 M1 M2 M3 Z1 Z2 Z3 C E D Apple Apple Apple Orange Orange Orange A B B Orange Orange Orange Orange Orange Orange A C A Orange Orange Orange Apple Orange Apple B A D Orange Apple Apple Apple Orange Apple C B B Orange Orange Apple Apple Apple Apple A D C Orange Orange Apple Orange Apple Orange E C B Apple Orange Apple Apple Orange Apple D A B Apple Orange Apple Apple Orange Orange E D E Orange Orange Orange Apple Apple Apple C C A Apple Orange Apple Apple Apple Apple E C A Orange Apple Orange Orange Orange Orange C B D Apple Apple Apple Orange Apple Apple E E D Apple Orange Apple Orange Apple Orange E A B Orange Apple Apple Orange Orange Apple A D E Apple Orange Apple Orange Orange Apple E B D Apple Apple Orange Apple Apple Apple D C B Orange Orange Apple Apple Orange Orange A C C Apple Apple Apple Apple Orange Orange E B A Apple Orange Apple Orange Apple Apple E C A Orange Apple Apple Apple Apple Apple ",header=TRUE) 

I want to identify frequency tables using a loop.

(我想使用循环识别频率表。)

For example, M1 and Z1 with class 1, M2 and Z2 with class2 and M3 and Z3 with class3 and so on.

(例如,M1和Z1的类为1,M2和Z2的类为2,M3和Z3的类为3,依此类推。)

The prototype table for each table is :

(每个表的原型表是:)

 Class M1-Apple M1_Orange Z1_Apple Z1_Orange A x x x x B x x x x C x x x x D x x x x E x x x x 

Next, I want to generate bar plots for each table.

(接下来,我想为每个表格生成条形图。)

I have tried this:

(我已经试过了:)

 class<-df1[1:3] MZ<-df1[4:9] df2<-lapply(1:3, function(x) table(class[,x] ~ MZ[,x])) 

So I need to have the graphs as shown in the frequency table .

(因此,我需要具有频率表中所示的图表。)

I think we need to Map variaiables first and then using lappy run these graphs.Can any one help?

(我认为我们需要先映射变量,然后使用lappy运行这些图。)

  ask by User20100 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If I understand correctly what you want, then this is largely an exercise in reshaping your data from wide to long format , so we start by doing that using pivot_longer from tidyr 1.0.0 (you may need to upgrade).

(如果我正确理解了您想要的内容,那么这很大程度上是将数据从宽格式 pivot_longer 为长格式的练习,因此我们首先使用tidyr 1.0.0 pivot_longer进行tidyr 1.0.0 (您可能需要升级)。)

I pivot twice since I find that easier to think about, but the pivot can probably be done in one go:

(我发现了更容易思考的部分,所以我进行了两次透视,但是透视可能可以一口气完成:)

library(tidyverse)

long_data <- df1 %>% 
  pivot_longer(everything(),
               names_to = c(".value", "set"),
               names_pattern = "([A-Za-z]+)(.)") %>%
  pivot_longer(-c(set, Class), 
               names_to = "type",
               values_to = "fruit") %>%
  select(set, class = Class, type, fruit)

head(long_data)
#> # A tibble: 6 x 4
#>   set   class type  fruit 
#>   <chr> <fct> <chr> <fct> 
#> 1 1     C     M     Apple 
#> 2 1     C     Z     Orange
#> 3 2     E     M     Apple 
#> 4 2     E     Z     Orange
#> 5 3     D     M     Apple 
#> 6 3     D     Z     Orange

The set variable here corresponds to the integer suffix you had in your columns before.

(这里的set变量对应于您以前在各列中使用的整数后缀。)

Now we summarise this data:

(现在我们总结一下这些数据:)

counts <- long_data %>% group_by_all %>% count

head(counts)
#> # A tibble: 6 x 5
#> # Groups:   set, class, type, fruit [6]
#>   set   class type  fruit      n
#>   <chr> <fct> <chr> <fct>  <int>
#> 1 1     A     M     Apple      2
#> 2 1     A     M     Orange     3
#> 3 1     A     Z     Apple      2
#> 4 1     A     Z     Orange     3
#> 5 1     B     M     Orange     1
#> 6 1     B     Z     Apple      1

You could reshape this back to a wider format to get this into a matrix shape, if you wanted to, but I leave that aside here, since I gather you're interested in the plotting part, for which we need the long format anyway.

(如果愿意,您可以将其重塑为更宽的格式,以使其变为矩阵形状,但是我将其放在此处,因为我认为您对绘图部分感兴趣,因此我们仍然需要长格式。)

Hence, finally, the plot.

(因此,最后是情节。)

It's not entirely clear plot you're after, but going by the data, the following makes the most sense to me:

(您所追求的情节并不完全清楚,但是根据数据来看,以下对我而言最有意义:)

ggplot(counts, aes(x = class, y = n, fill = fruit)) +
  geom_histogram(position = "dodge",
                 stat = "identity") +
  facet_wrap(~set)
#> Warning: Ignoring unknown parameters: binwidth, bins, pad

Created on 2019-12-01 by the reprex package (v0.3.0)

(reprex软件包 (v0.3.0)创建于2019-12-01)

Play around with the aes and facet_wrap variables if you were after something else.

(如果您不喜欢其他方法,请facet_wrap使用aesfacet_wrap变量。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...