r-classes / ds4dh-hw1-TonLeon

ds4dh-hw1-TonLeon created by GitHub Classroom
0 stars 0 forks source link

Where to put Y-argument when you work with ggplot2 #1

Open TonLeon opened 5 years ago

TonLeon commented 5 years ago

Trying to work with the following dataset, which I called pubs, I wanted to select 30 the most popular titles of pubs, count the number of their appearence/mentions on the territory of England in a new column and visualize it in bar. I imagined that x-axis would consist of pubs' names and y-axis - of their quantity. But I faced with a problem stat_count() must not be used with a y aesthetic, so I don't know where to put y-argument in this code:

my_factor <- factor(pubs2$pub_name)
levels(my_factor)
pubs2 <- pubs%>%
  group_by(pub_name)%>%
  summarise(n = n())%>%
  arrange(desc(n))%>%
  slice(1:30)%>%
  mutate(pub_name = factor(pub_name, levels = c("Red Lion", "Rose & Crown",     "Royal Oak" ,       "The Bell",                                  "The Bell Inn" ,    "The Black Horse" , "The Crown"     ,   "The Crown Inn"  , "The George"    ,   "The Greyhound"    ,"The Kings Arms"   ,"The Kings Head" , "The New Inn"    ,  "The Plough"       ,"The Plough Inn"   ,"The Queens Head" ,"The Railway"     , "The Red Lion"    , "The Rising Sun"  , "The Rose & Crown","The Royal Oak"    ,"The Ship Inn"   ,  "The Sun Inn"    ,  "The Swan"        ,"The Swan Inn"     ,"The Victoria"  ,   "The Wheatsheaf",   "The White Hart"  ,"The White Horse"  ,"The White Lion")))%>%
  ggplot(aes(pub_name, n))+ #here is the error, but I can't understand the syntax
  geom_bar()+
  coord_flip()
pubs2

Cheers.

agricolamz commented 5 years ago

First, you need to reread section about aggregated and not aggregated data. You will find out that it is easy to get rid of error by using geom_col() instead of geom_bar(). Second, I think you could get rid of this really long line of yours using fct_reorder() function (read more about it in the class notes). Get me informed about the progress.