2016-04-08 49 views
0

dplyrとggplot2でna値を含む積み重ね棒グラフを作成するにはどうすればよいですか?dplyr/ggplot2:na値を含む積み上げ棒グラフのプロット

X軸:年
Y軸:私が試したどのようなNAの観測結果の数字と数字

data <- data.frame(year = c(2015, 2015, 2016, 2016), 
        column2 = c(4, NA, 9, 1)) 


library (dplyr) 

missing_data <- data %>% 
        count(year, complete.cases(column2)) 

マイ結果

year complete.cases(column2)  n 
(dbl)      (lgl) (int) 
1 2015     FALSE  1 
2 2015     TRUE  1 
3 2016     TRUE  2 

library(ggplot2) 
na_plot <- ggplot (missing_data, aes(x=year, y=n)) 
na_plot+ 
geom_bar(stat="identity", aes(fill = complete.cases(column2)) 
+0

はAES文の中 "グループ= complete.cases(column2の)" を追加することでしょうトリックを行いますか? –

+0

文法上有効でない名前については、バックティックを使うことができます:\ 'complete.cases(column2) ' – aosmith

答えて

1

は私が考えます関数complete.casesは何らかの形でバリアブルに干渉します名前。試してみてください名前を変更(もfactoryear):

data <- data.frame(year = c(2015, 2015, 2016, 2016), 
        column2 = c(4, NA, 9, 1)) 

library(dplyr) 
library(ggplot2) 

missing_data <- data %>% 
    count(year, complete.cases(column2)) 

names(missing_data)[2] = "col2" 

na_plot <- ggplot(missing_data, aes(x=factor(year), y=n)) 
na_plot + geom_bar(stat="identity", aes(fill = col2)) 
+0

それは素晴らしいです。私は成功なしでdplyrリネーム機能で試しました。プロットでTRUEまたはFALSEを変更することはできますか? – Wilcar

+0

@Wilcarあなたは可変レベルを記録するのが好きですか? '?ifelse'を試してみてください。 – jakub

+0

ifelseはどのように使用できますか? – Wilcar

関連する問題