2017-10-04 69 views
0

私は溶融関数を使用する必要があると思いますが、どうすればよいかわかりません。以下のサンプルデータ、コード、結果グラフ。基本的に、「cnt」列は、各行の「登録済み」と「カジュアル」で構成されています。私はreshape2::meltを使用する 『あなたのデータを溶かすCNT「ではなく、全体の合計で、月あたり『』するにはggplot2を使って棒グラフで2つの変数を並べて表示

example data

#Bar Chart 
bar <- ggplot(data=subset(bikesharedailydata, !is.na(mnth)), aes(x=mnth, y=cnt)) + 
    geom_bar(stat="identity", position="dodge") + 
    coord_flip() + 
    labs(title="My Bar Chart", subtitle = "Total Renters per Month", caption = "Caption", x = "Month", y = "Total Renters") + 
    mychartattributes 

enter image description here

+0

私はあなたの第二の画像を公開してますが、データのエクセル・スクリーンショットではなく、あなたの最初の1います。列の追加なしにデータの重要な部分を抽出し、dput()を使用してデータセットを指定してください。 –

+0

Ooops、@ r2evansは両方とも公開しました.... –

+0

申し訳ありませんが、簡単に編集できます。 – r2evans

答えて

0

を』総「カジュアル対「登録」の合計を表示したいです:

library(ggplot2) 
library(reshape2) 

# Subset your data 
d <- subset(bikesharedailydata, !is.na(mnth)) 
# Select columns that you will plot 
d <- d[, c("mnth", "registered", "casual")] 
# Melt according to month 
d <- melt(d, "mnth") 

# Set fill by variable (registered or casual) 
ggplot(d, aes(mnth, value, fill = variable)) + 
    geom_bar(stat = "identity", position = "dodge") + 
    coord_flip() + 
    labs(title="My Bar Chart", subtitle = "Total Renters per Month", 
     caption = "Caption", 
     x = "Month", y = "Total Renters") 
0

使用tidyrとdplyr:

set.seed(1000) 
library(dplyr) 
library(tidyr) 
library(ggplot2) 

bikesharedailydata <- data.frame(month = month.abb, registered = rpois(n = 12, lambda = 2), casual =rpois(12, lambda = 1)) 


bikesharedailydata %>% gather(key="type", value = "count", -month) %>% 
    ggplot(aes(x=month, y=count, fill = type))+geom_bar(stat = "identity", position = "dodge") 

enter image description here

関連する問題