Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.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 - Fatal编程技术网

R 位置为“的条形图”;道奇;,重新排序和分组

R 位置为“的条形图”;道奇;,重新排序和分组,r,ggplot2,R,Ggplot2,我试图将以下数据绘制为减淡条形图: structure(list(word = c("evil", "intelligence", "medical", "revolution", "change", "dominate", "deplorable", "broken", "fight", "government", "agreement", "real", "intelligence", "medical", "system", "mainstay", "expert", "intell

我试图将以下数据绘制为减淡条形图:

structure(list(word = c("evil", "intelligence", "medical", "revolution", 
"change", "dominate", "deplorable", "broken", "fight", "government", 
"agreement", "real", "intelligence", "medical", "system", "mainstay", 
"expert", "intelligent", "excellence", "guide"), sentiment = c("fear", 
"fear", "fear", "fear", "fear", "fear", "fear", "fear", "fear", 
"fear", "trust", "trust", "trust", "trust", "trust", "trust", 
"trust", "trust", "trust", "trust"), n = c(304L, 257L, 159L, 
152L, 127L, 118L, 96L, 92L, 81L, 79L, 300L, 297L, 257L, 159L, 
131L, 92L, 83L, 78L, 75L, 74L)), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -20L), vars = "sentiment", drop = TRUE, indices = list(
    0:9, 10:19), group_sizes = c(10L, 10L), biggest_group_size = 10L, labels = structure(list(
    sentiment = c("fear", "trust")), class = "data.frame", row.names = c(NA, 
-2L), vars = "sentiment", drop = TRUE, indices = list(c(1L, 2L, 
3L, 4L, 5L, 8L, 9L, 11L, 15L, 18L), c(0L, 6L, 7L, 10L, 12L, 13L, 
14L, 16L, 17L, 19L)), group_sizes = c(10L, 10L), biggest_group_size = 10L))
在x轴上,我查看要显示的单词,在y轴上,计数n。然而,我想把这十个最重要的词,按照它们的情感并排进行比较

我尝试过这段代码,但显然没有达到预期的效果:

ggplot(x, aes(fill=sentiment, y=n, x=word)) +
    geom_bar(position="dodge", stat="identity")+coord_flip()
我想看到的是“邪恶”和“协议”这两个词,然后是“智慧”和“真实”,等等


任何帮助都将不胜感激。

您可以尝试将
word
转换为
因子
,如下所示:

x$word <- factor(x$word, levels = c("evil", "agreement", "intelligence", "real","medical", "revolution", 
                                "change", "dominate", "deplorable", "broken", "fight", "government", 
                                "intelligence", "medical", "system", "mainstay", 
                                "expert", "intelligent", "excellence", "guide"),
             labels  = c("evil", "agreement", "intelligence", "real","medical", "revolution", 
                         "change", "dominate", "deplorable", "broken", "fight", "government", 
                         "intelligence", "medical", "system", "mainstay", 
                         "expert", "intelligent", "excellence", "guide"))


ggplot(x, aes(fill=sentiment, y=n, x=word)) +
       geom_bar(position="dodge", stat="identity")+coord_flip()
x$word你可以试试这个

library(tidyverse)
d %>% 
  group_by(sentiment) %>% 
  mutate(index=1:n()) %>% 
  ggplot(aes(x=factor(index), y=n, fill=sentiment)) +
    geom_col(position = position_dodge(width = 0.9)) +
    geom_text(aes(y=0,label=word), hjust=0,
              position = position_dodge(width = 0.9))+
      coord_flip()

或者您可以尝试
facet\u wrap
解决方案

d %>% 
    group_by(sentiment) %>% 
    mutate(index=1:n()) %>% 
    ggplot(aes(x=word, y=n,fill=sentiment)) +
    geom_col(position = position_dodge(width = 0.9)) +
    coord_flip() + 
    facet_wrap(~index, scale="free_y", strip.position="right", ncol=1) + 
    theme(strip.text = element_blank())

在这两种解决方案中,您都需要添加索引,例如使用
tidyverse
来定义哪些词属于同一个词