I am having errors from barplot using ggplot

Please I am having issues with the output of my plot. The graphs are not well aligned. I need help on it please!

tidy_text <- clean_data %>%
  unnest_tokens(bigram, review, token="ngrams", n=2)

##SEPERATE WORDS
bigrams_separated <-tidy_text %>% 
  separate(bigram,c("word1","word2"),sep=" ")
####filter stop words and NA                                               
bigrams_filtered <- bigrams_separated %>% 
  filter(!word1 %in% stop_words$word) %>%
  filter(!word2 %in% stop_words$word) %>%
  filter(!is.na(word1))%>%
  filter(!is.na(word2))
####
#bigram united
bigrams_united <- bigrams_filtered%>% 
  unite(bigram, word1, word2, sep=" ")
#####
my_stopwords <-data.frame(word=c("air canada", "air hostess","fly air","ac 165leaving",
                                 "ac 165scheduled", "seat not","and seat",
                                 "not suitable","suitable for",
                                 "for long","1abreast pitch","layout 1abreast",
                                 "bag arrived","flight delays","delayed bags",
                                 "canada staff","canada waiting","adult males",
                                 "additional 1minutes","canada flight","airport air",
                                 "aeroplane air", "chair assistance","aeroplan air",
                                 "ac baggage", "2voucher issued","arrived speaking",
                                 "bags arrived","cancelled times", "bed total",
                                 "flight attendants","flight ac","flag carrier","elite million",
                                 "delayed untill","ground crew","handle people's","pearson terminal",
                                 "pearson airport","palm beach","million miler","mention specifically",
                                 "national flag","cancelation hotel"))
airline <- bigrams_united %>% anti_join(my_stopwords, by=c("bigram"="word"))

####term frequency
bigram_tf_idf <- airline %>% count(verification,bigram)%>%
  bind_tf_idf(bigram, verification, n) %>%
  arrange(desc(tf_idf))


###bar plot of the term frequency
bigram_tf_idf %>%
  group_by(verification)%>%
  slice_head(n=20)%>%
  ungroup() %>%
  ggplot(aes(x=reorder(bigram, tf_idf),y=tf_idf, fill=verification))+
  geom_col(show.legend=FALSE)+
  labs(x=NULL, y="tf_idf") +
  scale_x_reordered()+
  coord_flip()+
  facet_wrap(~verification, ncol=2, scales="free")+
  theme(axis.text.x=element_text(angle=90, hjust=1, vjust=0.5))

This is the out put of my plot....
image

If by "The graphs are not well aligned" you mean facets don't share the same y-axis then remove the scales="free" argument from this line, otherwise, can you please elaborate?

I mean the length of the barplot are not suppose to be the same. The tf_idf were the same which is not supposed to be .

Since we can't see your data we can't tell if your expectations match your data or not, most likely this is not related to the ploting but to the underlying data or its processing.

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

I would expect this to give the top 20 in each group ...
then if there are two groups, it would show me 40; 20 on the left and 20 on the right...
maybe you didnt intend that ?

A handy way to supply some sample data is the dput() function. In the case of a large dataset something like dput(head(mydata, 100)) should supply the data we need. Just do dput(mydata) where mydata is your data. Copy the output and paste it here.

Alright, thank you! The output is down below

  id verification  review                                 
<dbl> <chr>         <chr>                                  

1 1 Trip Verified Uncomfortable seat layout 1abreast. Pi…
2 1 Trip Verified Extremely rude staff. The air hostess …
3 1 Not Verified I am a Super Elite Million Miler with …
4 1 Not Verified Stunning incompetence and disregard fo…
5 1 Not Verified We sat in seats 2D and 2F. From the ch…
6 1 Not Verified I was booked on flight AC 165scheduled…
7 1 Trip Verified Casablanca to Montreal. My pillow was …
8 1 Trip Verified Disastrous delayed baggage policy, day…
9 1 Trip Verified Flight delays for maintenance is horri…
10 1 Not Verified Coming back from Yellowknife made it t…
11 2 Trip Verified Uncomfortable seat layout 1abreast. Pi…
12 2 Trip Verified Extremely rude staff. The air hostess …
13 2 Not Verified I am a Super Elite Million Miler with …
14 2 Not Verified Stunning incompetence and disregard fo…
15 2 Not Verified We sat in seats 2D and 2F. From the ch…
16 2 Not Verified I was booked on flight AC 165scheduled…
17 2 Trip Verified Casablanca to Montreal. My pillow was …
18 2 Trip Verified Disastrous delayed baggage policy, day…
19 2 Trip Verified Flight delays for maintenance is horri…
20 2 Not Verified Coming back from Yellowknife made it t…
21 3 Trip Verified Uncomfortable seat layout 1abreast. Pi…
22 3 Trip Verified Extremely rude staff. The air hostess …
23 3 Not Verified I am a Super Elite Million Miler with …
24 3 Not Verified Stunning incompetence and disregard fo…
25 3 Not Verified We sat in seats 2D and 2F. From the ch…
26 3 Not Verified I was booked on flight AC 165scheduled…
27 3 Trip Verified Casablanca to Montreal. My pillow was …
28 3 Trip Verified Disastrous delayed baggage policy, day…
29 3 Trip Verified Flight delays for maintenance is horri…
30 3 Not Verified Coming back from Yellowknife made it t…
31 4 Trip Verified Uncomfortable seat layout 1abreast. Pi…
32 4 Trip Verified Extremely rude staff. The air hostess …
33 4 Not Verified I am a Super Elite Million Miler with …
34 4 Not Verified Stunning incompetence and disregard fo…
35 4 Not Verified We sat in seats 2D and 2F. From the ch…
36 4 Not Verified I was booked on flight AC 165scheduled…
37 4 Trip Verified Casablanca to Montreal. My pillow was …
38 4 Trip Verified Disastrous delayed baggage policy, day…
39 4 Trip Verified Flight delays for maintenance is horri…
40 4 Not Verified Coming back from Yellowknife made it t…
41 5 Trip Verified Uncomfortable seat layout 1abreast. Pi…
42 5 Trip Verified Extremely rude staff. The air hostess …
43 5 Not Verified I am a Super Elite Million Miler with …
44 5 Not Verified Stunning incompetence and disregard fo…
45 5 Not Verified We sat in seats 2D and 2F. From the ch…
46 5 Not Verified I was booked on flight AC 165scheduled…
47 5 Trip Verified Casablanca to Montreal. My pillow was …
48 5 Trip Verified Disastrous delayed baggage policy, day…
49 5 Trip Verified Flight delays for maintenance is horri…
50 5 Not Verified Coming back from Yellowknife made it t…
51 6 Trip Verified Uncomfortable seat layout 1abreast. Pi…
52 6 Trip Verified Extremely rude staff. The air hostess …
53 6 Not Verified I am a Super Elite Million Miler with …
54 6 Not Verified Stunning incompetence and disregard fo…
55 6 Not Verified We sat in seats 2D and 2F. From the ch…
56 6 Not Verified I was booked on flight AC 165scheduled…
57 6 Trip Verified Casablanca to Montreal. My pillow was …
58 6 Trip Verified Disastrous delayed baggage policy, day…
59 6 Trip Verified Flight delays for maintenance is horri…
60 6 Not Verified Coming back from Yellowknife made it t…
61 7 Trip Verified Uncomfortable seat layout 1abreast. Pi…
62 7 Trip Verified Extremely rude staff. The air hostess …
63 7 Not Verified I am a Super Elite Million Miler with …
64 7 Not Verified Stunning incompetence and disregard fo…
65 7 Not Verified We sat in seats 2D and 2F. From the ch…
66 7 Not Verified I was booked on flight AC 165scheduled…
67 7 Trip Verified Casablanca to Montreal. My pillow was …
68 7 Trip Verified Disastrous delayed baggage policy, day…
69 7 Trip Verified Flight delays for maintenance is horri…
70 7 Not Verified Coming back from Yellowknife made it t…
71 8 Trip Verified Uncomfortable seat layout 1abreast. Pi…
72 8 Trip Verified Extremely rude staff. The air hostess …
73 8 Not Verified I am a Super Elite Million Miler with …
74 8 Not Verified Stunning incompetence and disregard fo…
75 8 Not Verified We sat in seats 2D and 2F. From the ch…
76 8 Not Verified I was booked on flight AC 165scheduled…
77 8 Trip Verified Casablanca to Montreal. My pillow was …
78 8 Trip Verified Disastrous delayed baggage policy, day…
79 8 Trip Verified Flight delays for maintenance is horri…
80 8 Not Verified Coming back from Yellowknife made it t…
81 9 Trip Verified Uncomfortable seat layout 1abreast. Pi…
82 9 Trip Verified Extremely rude staff. The air hostess …
83 9 Not Verified I am a Super Elite Million Miler with …
84 9 Not Verified Stunning incompetence and disregard fo…
85 9 Not Verified We sat in seats 2D and 2F. From the ch…
86 9 Not Verified I was booked on flight AC 165scheduled…
87 9 Trip Verified Casablanca to Montreal. My pillow was …
88 9 Trip Verified Disastrous delayed baggage policy, day…
89 9 Trip Verified Flight delays for maintenance is horri…
90 9 Not Verified Coming back from Yellowknife made it t…
91 10 Trip Verified Uncomfortable seat layout 1abreast. Pi…
92 10 Trip Verified Extremely rude staff. The air hostess …
93 10 Not Verified I am a Super Elite Million Miler with …
94 10 Not Verified Stunning incompetence and disregard fo…
95 10 Not Verified We sat in seats 2D and 2F. From the ch…
96 10 Not Verified I was booked on flight AC 165scheduled…
97 10 Trip Verified Casablanca to Montreal. My pillow was …
98 10 Trip Verified Disastrous delayed baggage policy, day…
99 10 Trip Verified Flight delays for maintenance is horri…
100 10 Not Verified Coming back from Yellowknife made it t…

Here is a very simple example of how to provide what we need.

dat <- data.frame(xx = 1:10, yy = letters[1:10])

dput(dat)

This give us

structure(list(xx = 1:10, yy = c("a", "b", "c", "d", "e", "f", 
                                 "g", "h", "i", "j")), class = "data.frame", row.names = c(NA,  -10L))

We can then copy it into R

my_dat  <- structure(list(xx = 1:10, yy = c("a", "b", "c", "d", "e", "f", 
                                            "g", "h", "i", "j")), class = "data.frame", row.names = c(NA,  -10L))

and we have an exact copy of your data.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.