2017-02-20 3 views
0

棒グラフとして2つの時系列をグラフ化しようとしています(スタックされていない)。私はそれを達成するためにposition="identity"を使用していますが、バーが間違った順序で出てきている:R ggplot2:棒グラフの並べ替え表示

library(reshape2) 
library(ggplot2) 

test<-abs(rnorm(12)*1000) 
test<-rbind(test, test+500) 
colnames(test)<-month.abb[seq(1:12)] 
rownames(test)<-c("first", "second") 
otherTest<-apply(test, 2, mean) 
test<-melt(test) 
#otherTest<-as.data.frame(otherTest) 


otherTest <- data.frame(
    Var2 = names(otherTest), 
    value = otherTest 
) 

otherTest$Var2 = factor(otherTest$Var2, levels = levels(test$Var2)) 

ggplot(test, aes(x = Var2, y = value, group = 1,order=-as.numeric(Var2))) + 
    geom_bar(aes(fill = Var1), stat="identity", position="identity") + 
    geom_line(data = otherTest) 

は、下のグラフを生成します。ご覧のように、 'second'の値は 'first'よりも高く、青いバーはピンクのバーを隠しています。どのようにして「第1」を「第2」の上に得ることができますか?私はtestVar2に関連する要因を無駄に並べ替えようとしています。

enter image description here

+0

たぶん私はそれは私がまだバーをしたいと私は探しています何であることない位置= dodge' –

+0

@KarthikArumugham 'てみてくださいお互いの上にまた、私が試してみると、グラフは同じように見えます。 – user3390169

+1

'geom_bar()'に 'alpha 'を追加します。 'alpha = 0.5'のようなもの –

答えて

1

私は単純化するためにコードを書き換えなければなりませんでした。

library(dplyr) 
test1 <- data_frame(month = factor(month.abb, levels=month.abb), 
        value = abs(rnorm(12)*1000), name="first") 
glimpse(test1) 
#Observations: 12 
#Variables: 3 
#$ month <fctr> Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec 
#$ value <dbl> 1477.63915, 690.10964, 218.79066, 338.01241, 1952.10102, 354.65286, 340.09479, 1070.... 
#$ name <chr> "first", "first", "first", "first", "first", "first", "first", "first", "first", "fi... 

test2 <- data_frame(month = factor(month.abb, levels=month.abb), 
        name="second") 
test2$value <- test1$value+500 
glimpse(test2) 
#Observations: 12 
#Variables: 3 
#$ month <fctr> Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec 
#$ name <chr> "second", "second", "second", "second", "second", "second", "second", "second", "sec... 
#$ value <dbl> 1977.6391, 1190.1096, 718.7907, 838.0124, 2452.1010, 854.6529, 840.0948, 1570.0937, ... 

test <- data_frame(month = factor(month.abb, levels=month.abb)) 
test$value <- (test1$value+test2$value)/2 
glimpse(test) 
#Observations: 12 
#Variables: 2 
#$ month <fctr> Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec 
#$ value <dbl> 1727.6391, 940.1096, 468.7907, 588.0124, 2202.1010, 604.6529, 590.0948, 1320.0937, 3... 

# Plot 
library(ggplot2) 
p <- ggplot(NULL, aes(month, value)) + 
    geom_bar(aes(fill = "second"), data = test2, stat="identity") + 
    geom_bar(aes(fill = "first"), data = test1, stat="identity") + 
    geom_line(data = test, aes(as.numeric(month), value)) 
p 

ggplot2

1

問題がpositionパラメータです。ただ、プロットを作成するにはplotコマンドでposition = "stack"

ggplot(test, aes(x = Var2, y = value, group = 1, order = -as.numeric(Var2))) + 
    geom_bar(aes(fill = Var1), stat = "identity", position = "stack") + 
    geom_line(data = otherTest) 

を使用します。ggplot2バージョン2.0.0+

、コードを合理化することができます。

ggplot(test, aes(x = Var2, y = value, group = 1)) + 
    geom_col(aes(fill = Var1), position="stack") + 
    geom_line(data = otherTest) 
関連する問題