0

I'd like to calculate the frequency of each country code by year and make a stacked column graph "frequency x year", with the country codes being the stacked columns.

I'm beginner on R, and I don't know the best way to do this

Thanks.

dput(head(table_suinos_p.country, 20))
structure(list(suinos.p.year = structure(c(9L, 8L, 14L, 7L, 14L, 
14L, 16L, 11L, 13L, 9L, 1L, 8L, 16L, 13L, 16L, 5L, 15L, 16L, 
9L, 18L), .Label = c("1998", "1999", "2000", "2001", "2002", 
"2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", 
"2011", "2012", "2013", "2014", "2015", "2016", "2017"), class = "factor"), 
    suinos.p.country = structure(c(4L, 17L, 3L, 17L, 1L, 17L, 
    11L, 17L, 17L, 17L, 17L, 17L, 6L, 5L, 17L, 17L, 17L, 11L, 
    6L, 11L), .Label = c("AU", "CA", "CN", "CU", "DE", "EP", 
    "FR", "GB", "IT", "JP", "KR", "PL", "RU", "SG", "TH", "TW", 
    "US", "WO"), class = "factor")), row.names = c(NA, 20L), class = "data.frame")
kstew
  • 1,104
  • 6
  • 21
  • 2
    Please provide more details and what problems you are experiencing. Please see [this post](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example?rq=1) about making a reprex. – kstew Aug 01 '19 at 20:24
  • Images are a really bad way of posting data (or code). Can you post sample data in `dput` format? Please edit **the question** with the output of `dput(df)`. Or, if it is too big with the output of `dput(head(df, 20))`. (`df` is the name of your dataset.) And don't [cross post](https://pt.stackoverflow.com/questions/401091/contar-numero-de-celulas-com-mesmo-valor-r). – Rui Barradas Aug 01 '19 at 20:27
  • thanks for the feedback and help. I'm new here – ivan.santos Aug 01 '19 at 20:43

1 Answers1

0

Here's a basic stacked bar chart with your example data.

df %>% count(suinos.p.year,suinos.p.country) %>% 
  ggplot(aes(suinos.p.year,n,fill=suinos.p.country)) +
  geom_bar(stat='identity')

enter image description here

kstew
  • 1,104
  • 6
  • 21