-2

I have csv "celebrity deaths" and column "cause of death". I would like to make in ggplot2 chart, when I have count of top 10 death causes. I do not how to count in R when I'm using date from csv.

I got csv which looks like: https://i.stack.imgur.com/HqwF5.png

And I think that I will need vector will all causes. But I do not have idea how to group them in top 10.

Mateusz
  • 27
  • 3
  • 1
    First you need to import the data in the CSV into an R data frame. The [R tag wiki](https://stackoverflow.com/tags/r/info) has lots of good beginner resources, I would suggest starting with *An Introduction to R*, but you can also find lots of questions here on Stack Overflow if you search for "`[r] csv`" – Gregor Thomas Dec 27 '17 at 20:50
  • 2
    Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). This will make it much easier for others to help you. – Jaap Dec 27 '17 at 20:50
  • I did other charts for example how many celebrities died in specific age. But i do not know how to make that top 10 causes of death. – Mateusz Dec 27 '17 at 20:51
  • 1
    Please provide sample data and more information to work with. Otherwise, your post will be downvoted due to lack of clarity and vagueness. This also sounds like a homework question. – InfiniteFlash Dec 27 '17 at 20:52
  • Okay, sorry i edited post. – Mateusz Dec 27 '17 at 20:58
  • 1
    Well, your post still needs editing, but this just good enough to be answered. – InfiniteFlash Dec 27 '17 at 21:00

1 Answers1

0

Im going to try to produce something as best I can with what you provided with base R and ggplot2.

library(ggplot2)

celeb <- c("Kim Kardashian", "The chubby kid from stand by me", "The bassist from the local Clash cover band", "One of L. Ron Hubbard's polyps", "Frank Zappa", "Dweezil Zappa", "Moonunit Zappa", "Scott Evil")

death <- c("Gored by rhino", "Eaten by Compies", "Choked on funyun", "Gored by rhino", "Gored by rhino", "Eaten by Compies", "Gored by rhino", "Failed to meet dad's expectations")




df <- cbind(celeb, death)
df <- as.data.frame(df)

So, my sense is that you just want to rank causes of death and then barplot them or something. This is overly complicated, but I figured I would just show you a step by step way of doing it.

    #first get counts of deaths
deathcounts <- as.data.frame(table(df$death))

#next put them in decreasing order
topfour <- deathcounts[order(deathcounts$Freq, decreasing=T)[1:4],]

#cool, so rhinos are dangerous mofos. Let's plot these results
deathplot <- ggplot(topfour, aes(x=Var1, y=Freq)) + geom_bar(stat="identity")

Your no frills, plain result is: enter image description here

Lee
  • 224
  • 1
  • 2
  • 9