R 我在ggplot2顶部设置的参数无效?

R 我在ggplot2顶部设置的参数无效?,r,ggplot2,dplyr,R,Ggplot2,Dplyr,我想把我的图表限制在前10个单词(做情绪分析)的范围内。我设置的参数无效。这是我的密码: df_boatstone_bing %>% group_by(sentiment) %>% top_n(10) %>% ungroup() %>% mutate(word = reorder(word, n)) %>% ggplot(aes(word, n, fill = sentiment)) + geom_col(show.

我想把我的图表限制在前10个单词(做情绪分析)的范围内。我设置的参数无效。这是我的密码:

df_boatstone_bing  %>%
    group_by(sentiment) %>%
    top_n(10) %>%
    ungroup() %>%
    mutate(word = reorder(word, n)) %>%
    ggplot(aes(word, n, fill = sentiment)) +
    geom_col(show.legend = FALSE) +
    facet_wrap(~sentiment, scales = "free_y") +
    labs(y = "Contribution to sentiment",
         x = NULL) +
    coord_flip()
如果有人能抓住我的错误那就太好了

编辑:包括dput

structure(list(word = c("awesome", "loud", "worth", "amazing", 
"excellent", "nice"), n = c(38L, 33L, 29L, 28L, 26L, 22L), sentiment = c("positive", 
"negative", "positive", "positive", "positive", "positive"), 
 method = c("Bing et al.", "Bing et al.", "Bing et al.", "Bing et al.", 
"Bing et al.", "Bing et al.")), .Names = c("word", "n", "sentiment", 
"method"), row.names = c(NA, -6L), class = c("tbl_df", "tbl", 
"data.frame"))

如果我们在
top\n(word,n)
术语中指定
n
,并且在输入数据中有足够的单词,它应该可以正常工作

# OP's 
df_bing_sample  %>%
  group_by(sentiment) %>%
  top_n(10, n) %>%   # <--- specify sort by n, rather than by method, the last column
  ungroup() %>%
  mutate(word = reorder(word, n)) %>%
  ggplot(aes(word, n, fill = sentiment)) +
  geom_col(show.legend = FALSE) +
  facet_wrap(~sentiment, scales = "free_y") +
  labs(y = "Contribution to sentiment",
       x = NULL) +
  coord_flip()
#OP's
df_bing_样本%>%
组别(情绪)%>%
前n(10,n)%>%
变异(单词=重新排序(单词,n))%>%
ggplot(aes(字、n、填充=情绪))+
几何坐标(show.legend=FALSE)+
小平面包裹(~情绪,尺度=“自由”)+
实验室(y=“对情绪的贡献”,
x=NULL)+
coord_flip()

此处的假数据样本,正负数据各超过10个:

library(tidytext)
set.seed(42)
df_bing_sample <- tidytext::sentiments %>%
  filter(lexicon == "bing") %>%
  sample_n(50) %>%
  mutate(n = rnorm(50, 30, 10) %>% floor %>% as.integer()) %>%
  mutate(method = "Bing et al.") %>%
  select(word, n, sentiment, method)
库(tidytext)
种子(42)
df_bing_样本%
过滤器(词典==“bing”)%>%
样本n(50)%>%
变异(n=rnorm(50,30,10)%%>%floor%%>%as.integer())%%>%
突变(method=“Bing等人”)%>%
选择(单词、n、情绪、方法)

如果我们在
top\n(word,n)
术语中指定
n
,并且在输入数据中有足够的单词,它应该可以正常工作

# OP's 
df_bing_sample  %>%
  group_by(sentiment) %>%
  top_n(10, n) %>%   # <--- specify sort by n, rather than by method, the last column
  ungroup() %>%
  mutate(word = reorder(word, n)) %>%
  ggplot(aes(word, n, fill = sentiment)) +
  geom_col(show.legend = FALSE) +
  facet_wrap(~sentiment, scales = "free_y") +
  labs(y = "Contribution to sentiment",
       x = NULL) +
  coord_flip()
#OP's
df_bing_样本%>%
组别(情绪)%>%
前n(10,n)%>%
变异(单词=重新排序(单词,n))%>%
ggplot(aes(字、n、填充=情绪))+
几何坐标(show.legend=FALSE)+
小平面包裹(~情绪,尺度=“自由”)+
实验室(y=“对情绪的贡献”,
x=NULL)+
coord_flip()

此处的假数据样本,正负数据各超过10个:

library(tidytext)
set.seed(42)
df_bing_sample <- tidytext::sentiments %>%
  filter(lexicon == "bing") %>%
  sample_n(50) %>%
  mutate(n = rnorm(50, 30, 10) %>% floor %>% as.integer()) %>%
  mutate(method = "Bing et al.") %>%
  select(word, n, sentiment, method)
库(tidytext)
种子(42)
df_bing_样本%
过滤器(词典==“bing”)%>%
样本n(50)%>%
变异(n=rnorm(50,30,10)%%>%floor%%>%as.integer())%%>%
突变(method=“Bing等人”)%>%
选择(单词、n、情绪、方法)

请将
dput(head(df_boatstone_bing))
发布到您的问题中,以便有人可以使用数据重现您的错误
top\n
而无需显式列变量使用
tibble
中的最后一个变量进行排序。这是您选择前10行的正确变量吗?@MauritsEvers不,不是。我在编辑中包含了数据结构。但基本上,积极情绪和消极情绪都是有道理的。我希望将前10名积极情绪和前10名消极情绪分别绘制成图表(根据情绪出现的次数排序)。@r如果是这样,将
top\n
命令替换为
top\n(10,n)
你想做什么?正如我所解释的,您可能需要指定要根据其选择前10个条目的列。除了您应该按照@MauritsEvers的建议使用
top\n(10,n)
之外,其他一切看起来都很好,并且您的输入数据集需要更大。你的例子只有一个否定词。当我把我自己更大的假数据输入到你的图表中时,它看起来不错<代码>库(tidytext)集合。种子(42)df_bing_sample%filter(字典==“bing”)%%>%sample_n(50)%%>%mutate(n=rnorm(50,30,10)%%>%floor%%>%as.integer())%%>%mutate(method=“bing et al.”)%%>%select(单词,n,情感,方法)请发布
dput(head(df_boatstone,df bone_bing))
对您的问题进行排序,以便有人可以使用数据重现您的错误
top\n
而无需显式列变量使用
tible
中的最后一个变量进行排序。这是您选择前10行的正确变量吗?@MauritsEvers不,不是。我在编辑中包含了数据结构。但基本上,积极情绪和消极情绪都是有道理的。我希望将前10名积极情绪和前10名消极情绪分别绘制成图表(根据情绪出现的次数排序)。@r如果是这样,将
top\n
命令替换为
top\n(10,n)
你想做什么?正如我所解释的,您可能需要指定要根据其选择前10个条目的列。除了您应该按照@MauritsEvers的建议使用
top\n(10,n)
之外,其他一切看起来都很好,并且您的输入数据集需要更大。你的例子只有一个否定词。当我把我自己更大的假数据输入到你的图表中时,它看起来不错<代码>库(tidytext)集合。种子(42)df_bing_sample%filter(字典==“bing”)%%>%sample_n(50)%%>%mutate(n=rnorm(50,30,10)%%>%floor%%>%as.integer())%%>%mutate(method=“bing et al.”)%%>%select(单词,n,情感,方法)