2015-05-27 34 views
1

我想躲開我的酒吧的位置,但它根本不這樣做,我老實說不知道爲什麼。position_dodge()不閃避

這是我的代碼:

y = c(10.3,12.3,2.3,4.5,6,7.8) 
df <- data.frame(
    video = factor(c(1, 2, 3, 4, 5, 6)), 
    ERDERS = y, 
    group = factor(c(1, 1, 2, 2, 3, 3))) 

    p1 <- ggplot(df, aes(fill=factor(group), y= ERDERS, x= video)) 
    p1 <- p1 + geom_bar(color = "gray60", stat="identity", position = position_dodge()) +    
    scale_y_continuous(limits=c(-3, 21), name = "Activity[%]")+         # Y-Axis scaling + title 
    scale_x_discrete(name = "VIDEO", labels=c("1", "2", "3", "4", "5", "6"))+ 
    theme_bw() 

謝謝!

+0

您有六個數據分和6個酒吧,究竟應該回避? – Axeman

+0

總是要躲避其中的兩個,即根據因素組。 –

+0

那麼,應該有一種方法可以將視頻1/2(組1),3/4(組2)和視頻5/6(組3)分組。如果我有6個不同的視頻,我想繪製這些值,我需要六個不同的x值。 –

回答

2

我還是有點困惑,但也許你要找的這個?:

p1 <- ggplot(df, aes(y = ERDERS, x = group, fill = video)) 
p1 <- p1 + geom_bar(color = "gray60", stat = "identity", position = 'dodge') + 
    scale_y_continuous(limits=c(-3, 21), name = "Activity[%]") + 
    scale_x_discrete(name = "GROUP", labels=c("1", "2", "3", "4", "5", "6")) + 
    theme_bw() 

這是你的示例代碼會產生什麼:

Yours

這是我的建議產生。視頻現在分組。

Mine

您還可以使用方面:

p3 <- ggplot(df, aes(y = ERDERS, x = video)) + 
    facet_wrap(~ group, scales = 'free_x') 
p3 <- p3 + geom_bar(color = "gray60", stat = "identity", position = 'dodge') + 
    scale_y_continuous(limits=c(-3, 21), name = "Activity[%]") + theme_bw() + 
    ggtitle('Groups') 

facets

+0

嗯,但這裏的結果與我用示例代碼得到的結果沒有什麼不同,是嗎? –

+0

他們肯定看起來不同於我。添加了情節。 – Axeman

+0

啊,我的錯!有些東西壞了,不得不重新啓動R,現在它正在工作!這正是我所期待的,儘管我希望有一個解決方案來保持舊的x軸......謝謝! –