2017-12-27 11 views
-2

私はcsv「有名人の死」と「死因」を持っています。私はggplot2のチャートを作成したいと思います。 csvの日付を使用しているときにRでカウントする方法がありません。gsplot2を使用しているcsvからのトップ10カウントR

私は次のようになりますCSVました: https://i.imgur.com/WFTpzDE.png

そして私は、私はベクトルがすべての原因だろう必要があることだと思うが。しかし、私はトップ10にそれらをグループ化する方法がわかりません。

+1

まずあなたがRのデータフレームにCSVデータをインポートする必要があります。 [Rタグwiki](https://stackoverflow.com/tags/r/info)には初心者の資質がたくさんありますが、* R *入門から始めることをお勧めしますが、ここで多くの質問を見つけることもできます"[r] csv'を検索するとスタックオーバーフローが発生する – Gregor

+2

[良い質問をする方法](http://stackoverflow.com/help/how-to-ask)に関する情報をお読みください。 [再現可能な例](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610)。これは他の人があなたを助けることをはるかに容易にします。 – Jaap

+0

私は特定の年齢で何人の有名人が死亡したかなど、他のグラフを作成しました。しかし、私はその死のトップ10の原因をどうやって作るのか分かりません。 – Mateusz

答えて

0

私はあなたがベースRと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) 

私の感覚は、あなたが死の原因をランク付けして、それから何かをプロットしたいということです。これはあまりにも複雑ですが、私はあなたにそれを行うための一歩一歩を示すだけであると考えました。

#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") 

あなたのフリルは、プレーンな結果は次のとおりです。 enter image description here

関連する問題