2016-05-05 3 views
-1

は、私は次のワークフローがあります。ggplot:position_dodgeはオーバーラップしますか?

rm(list=ls()) 

data(mtcars) 
attach(mtcars) 

library(ggplot2) 
library(plyr) 
library(dplyr) 
library(scales) 
library(reshape2) 
library(lazyeval) 

my_func <- function(x, y) { 
    test<<-mtcars %>% group_by_(x, y) %>% 
    summarise(Freq = n()) %>% 
    mutate(Freq = Freq/sum(Freq)) 
    test 
    } 
my_func('gear', 'cyl') 

ggplot(test, aes(x=gear, y=Freq))+ 
    geom_bar(stat="identity", aes(fill=cyl), position=position_dodge(width=0.1))+ 
    scale_y_continuous(labels=percent_format(), limits = c(0,1)) 

しかし、結果のプロットは、互いの隣にバーが表示されないのではなく、いくつかの互いの上に。何を提供し、私はこれをどのように修正するのですか?

+0

あなたは、最小限の再現性の例_minimal_行う必要がありますだけで処理したデータセットとここで問題の関数をプロットします。 – alistaire

+0

申し訳ありませんが、それはどういう意味ですか? – vashts85

+0

使用していないパッケージをインポートしないでください。または、例で直接データを処理できるだけの場合は関数を作成してください。シンプルにしておくと、可能な限り明白です。 – alistaire

答えて

2

cyl列をfactorに変換する必要があります。

test$cyl <- as.factor(test$cyl) 
ggplot(test, aes(x=gear, y=Freq))+ 
     geom_bar(stat="identity", aes(fill=cyl), position=position_dodge(width=1))+ 
     scale_y_continuous(labels=percent_format(), limits = c(0,1)) 

enter image description here

+0

これをプロットする関数をどのように作成しますか?私はこれについての変数置換に苦労しています。 – vashts85

+0

あなたはどんな機能をお探しですか? Fyi、変数名を渡すことについて話しているなら、 'aes()'の代わりに 'aes_string(x =" gear "、y = var)'を試してみてください。 – Psidom

+0

私は期待していた仕事であろう次の、しかし明らかにしない。 my_graph < - 関数(VAR1、VAR2){ ggplot()試験、AES(X = VAR1、Y =のFreq)+ geom_bar(STAT = "同一"、aes(fill = var2)、position = position_dodge(width = 1))+ scale_y_continuous(labels = percent_format()、limits = c(0,1)) } my_graph(gear、cyl) – vashts85

関連する問題