Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 按降序排列堆叠的钢筋_R_Ggplot2_Bar Chart - Fatal编程技术网

R 按降序排列堆叠的钢筋

R 按降序排列堆叠的钢筋,r,ggplot2,bar-chart,R,Ggplot2,Bar Chart,我已经成功地在R中制作了一个堆叠条形图,其中几个不同类别的百分比加起来为100%。数据帧如下所示: sujeito epentese vozeamento teste posicao palavra tipo ortografia cseguinte <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <

我已经成功地在R中制作了一个堆叠条形图,其中几个不同类别的百分比加起来为100%。数据帧如下所示:

 sujeito epentese vozeamento teste posicao palavra tipo  ortografia cseguinte
   <chr>   <chr>    <chr>      <chr> <chr>   <chr>   <chr> <chr>      <chr>    
 1 a       1        1          P     L       alpes   ps    ces        d_v      
 2 a       0        1          P     L       crepes  ps    ces        d_v      
 3 a       0        0          P     L       chopes  ps    ces        d_v      
 4 a       1        0          P     L       jipes   ps    ces        d_d      
 5 a       1        0          P     L       naipes  ps    ces        d_d      
 6 a       0        0          P     L       xaropes ps    ces        d_d      
 7 a       0        0          P     L       artes   ts    ces        d_v      
 8 a       0        0          P     L       botes   ts    ces        d_v      
 9 a       0        0          P     L       dentes  ts    ces        d_v      
10 a       0        0          P     L       potes   ts    ces        d_d      
# ... with 421 more rows
不过,我的意图是将其作为这张图片右侧的图表,并按降序排列列:


我尝试了不同的软件包,也操纵了小组,但仍然没有运气。我希望这不是多余的。我在网上看到的教程涉及到操纵Tidyverse,这是我的基本知识。提前谢谢

帮助您翻译链接的问题和手边问题的答案-

``` r
library(tidyverse)
# devtools::install_github("alistaire47/read.so")
dadospb <- read.so::read_so("sujeito epentese vozeamento teste posicao palavra tipo  ortografia cseguinte
   <chr>   <chr>    <chr>      <chr> <chr>   <chr>   <chr> <chr>      <chr>    
 1 a       1        1          P     L       alpes   ps    ces        d_v      
 2 a       0        1          P     L       crepes  ps    ces        d_v      
 3 a       0        0          P     L       chopes  ps    ces        d_v      
 4 a       1        0          P     L       jipes   ps    ces        d_d      
 5 a       1        0          P     L       naipes  ps    ces        d_d      
 6 a       0        0          P     L       xaropes ps    ces        d_d      
 7 a       0        0          P     L       artes   ts    ces        d_v      
 8 a       0        0          P     L       botes   ts    ces        d_v      
 9 a       1       0          P     L       dentes  ts    ces        d_v      
10 a       0        0          P     L       potes   ts    ces        d_d ")

df1 <- 
  dadospb%>%
  group_by(tipo, epentese)%>%
  summarise(quantidade = n())%>%
  mutate(frequencia = quantidade/sum(quantidade))
#> `summarise()` has grouped output by 'tipo'. You can override using the `.groups` argument.

fac_order <- df1 %>%
filter(epentese ==1 ) %>%
  arrange(frequencia) %>%
  pull(tipo)

df1 %>%
  mutate(novotipo = factor(tipo, levels = fac_order)) %>%
  ggplot(aes(x = novotipo, y = frequencia, fill = epentese)) +
  geom_col() 
``r
图书馆(tidyverse)
#devtools::install_github(“alistaire47/read.so”)
dadospb%
总结(quantidade=n())%>%
变异(频率=量子化/总和(量子化))
#>`summary()`已按'tipo'将输出分组。可以使用“.groups”参数重写。
工厂订单%
过滤器(epentese==1)%>%
排列(频率)%>%
拉(提坡)
df1%>%
突变(novotipo=因子(tipo,水平=因子顺序))%>%
ggplot(aes(x=novotipo,y=frequencia,fill=epentese))+
geom_col()


由(v1.0.0)创建于2021-02-13,以帮助您翻译链接的问题和您手头问题的答案-

``` r
library(tidyverse)
# devtools::install_github("alistaire47/read.so")
dadospb <- read.so::read_so("sujeito epentese vozeamento teste posicao palavra tipo  ortografia cseguinte
   <chr>   <chr>    <chr>      <chr> <chr>   <chr>   <chr> <chr>      <chr>    
 1 a       1        1          P     L       alpes   ps    ces        d_v      
 2 a       0        1          P     L       crepes  ps    ces        d_v      
 3 a       0        0          P     L       chopes  ps    ces        d_v      
 4 a       1        0          P     L       jipes   ps    ces        d_d      
 5 a       1        0          P     L       naipes  ps    ces        d_d      
 6 a       0        0          P     L       xaropes ps    ces        d_d      
 7 a       0        0          P     L       artes   ts    ces        d_v      
 8 a       0        0          P     L       botes   ts    ces        d_v      
 9 a       1       0          P     L       dentes  ts    ces        d_v      
10 a       0        0          P     L       potes   ts    ces        d_d ")

df1 <- 
  dadospb%>%
  group_by(tipo, epentese)%>%
  summarise(quantidade = n())%>%
  mutate(frequencia = quantidade/sum(quantidade))
#> `summarise()` has grouped output by 'tipo'. You can override using the `.groups` argument.

fac_order <- df1 %>%
filter(epentese ==1 ) %>%
  arrange(frequencia) %>%
  pull(tipo)

df1 %>%
  mutate(novotipo = factor(tipo, levels = fac_order)) %>%
  ggplot(aes(x = novotipo, y = frequencia, fill = epentese)) +
  geom_col() 
``r
图书馆(tidyverse)
#devtools::install_github(“alistaire47/read.so”)
dadospb%
总结(quantidade=n())%>%
变异(频率=量子化/总和(量子化))
#>`summary()`已按'tipo'将输出分组。可以使用“.groups”参数重写。
工厂订单%
过滤器(epentese==1)%>%
排列(频率)%>%
拉(提坡)
df1%>%
突变(novotipo=因子(tipo,水平=因子顺序))%>%
ggplot(aes(x=novotipo,y=frequencia,fill=epentese))+
geom_col()


由(v1.0.0)创建于2021-02-13,我喜欢在进入ggplot之前使用
forcats
包对类别进行排序。在这种情况下,我们可以使用
fct\u顺序
对数据按epentese顺序排序(因此0首先出现),然后按frecuencia排序。然后它成为一个有序因子,并将以该顺序绘制ggplot。(请参见“我的合成数据”中群集4如何排在群集3之前。)

我使用了mtcars,但重命名为您的数据名称:

library(dplyr); library(forcats)
# Prep to make mtcars look like your data
mtcars %>%
  mutate(vs = as.character(vs)) %>%
  group_by(tipo = carb, epentese = vs) %>%
  summarise(quantidade = sum(wt))%>%
  mutate(frequencia = quantidade/sum(quantidade)) %>%
  ungroup() %>%


  # Arrange in the way you want and then make tipo an ordered factor
  # I want epentese = 1 first, then descending frecuencia
  # When ggplot receives an ordered factor, it will display in order
  arrange(desc(epentese), -frequencia) %>%  
  mutate(tipo = tipo %>% as_factor %>% fct_inorder) %>%
  ...
  [Your ggplot code]
  

我喜欢在进入ggplot之前使用
forcats
包对类别进行排序。在这种情况下,我们可以使用
fct\u顺序
对数据按epentese顺序排序(因此0首先出现),然后按frecuencia排序。然后它成为一个有序因子,并将以该顺序绘制ggplot。(请参见“我的合成数据”中群集4如何排在群集3之前。)

我使用了mtcars,但重命名为您的数据名称:

library(dplyr); library(forcats)
# Prep to make mtcars look like your data
mtcars %>%
  mutate(vs = as.character(vs)) %>%
  group_by(tipo = carb, epentese = vs) %>%
  summarise(quantidade = sum(wt))%>%
  mutate(frequencia = quantidade/sum(quantidade)) %>%
  ungroup() %>%


  # Arrange in the way you want and then make tipo an ordered factor
  # I want epentese = 1 first, then descending frecuencia
  # When ggplot receives an ordered factor, it will display in order
  arrange(desc(epentese), -frequencia) %>%  
  mutate(tipo = tipo %>% as_factor %>% fct_inorder) %>%
  ...
  [Your ggplot code]
  

这些可能会有帮助&谢谢!虽然我无法理解这些解决方案,但它们比我能处理的要复杂得多,就像一个副本一样,但无法关闭:但在OP的例子中,他已经有了类似于“plotOrder”向量中fill变量的重新排序值。仍然是一个副本。看看阿克塞曼的回答这些可能会有帮助&谢谢!虽然我无法理解这些解决方案,但它们比我能处理的要复杂得多,就像一个副本一样,但无法关闭:但在OP的例子中,他已经有了类似于“plotOrder”向量中fill变量的重新排序值。仍然是一个副本。正如你可能注意到的,我已经非常精简了你的代码。输出自然与您的绘图非常不同,因为您没有提供适当的样本数据。此外,我更改了epentese列的一个值,以便获得组ts的值。您可能需要更改过滤条件或按“-”排列。这只是演示了基本思想。正如您可能注意到的,我已经非常精简了您的代码。输出自然与您的绘图非常不同,因为您没有提供适当的样本数据。此外,我更改了epentese列的一个值,以便获得组ts的值。您可能需要更改过滤条件或按“-”排列。这正好说明了原则性的想法。