2017-09-27 10 views
0

Y軸に名前を、X軸に値(-0.5,0,0.5)の回転した棒グラフを作成しようとしています。私は棒グラフを使用していますが、scale_x_discreteを追加しようとするとY軸ラベルが消えます。私は間違って何をしていますか?ggplot scale_xを追加すると明らかにscale_yが削除される

dtm <- data.frame(
    month = factor(c("Police","Country","Rules")), 
    value = c(-.51,-.1,.9) 
) 
dtm$colour <- ifelse(dtm$value < 0, "firebrick1","steelblue") 

ggplot(dtm, aes(month, value, label = month)) + geom_text(aes(label=value), position = position_dodge(width = .9)) + 
    geom_bar(stat = "identity",aes(fill = colour))+ 
    coord_flip() + labs(x = "", y = "") + 
    scale_y_continuous(breaks = NULL,labels=dtm$month) +theme_bw() 

この行は、問題が

scale_x_discrete("Day of the Week",breaks = NULL,labels=c(-1,-.5,0,.5,1)) + 

答えて

3

あなたがcoord_flipを使用する場合はx軸はy軸、およびその逆となり発生します。私はそれを最後に置くことを示唆して、混乱させないようにします。またlabs空白を設定すると、scale_x_discrete

g1 <- ggplot(dtm, aes(month, value, label = month)) + geom_text(aes(label=value, hjust = ifelse(value >=0,-.1,1.2)), position = position_dodge(width = .9)) + 
    geom_bar(stat = "identity",aes(fill = colour))+ 
    scale_y_continuous(breaks =seq(-1,1,.5), labels=seq(-1,1,.5),expand=c(.1,.1)) + 
    scale_x_discrete("Day of the Week")+ 
    coord_flip() + 
    theme_bw() 

でそれを上書きしたときに、私は値が正であるか負であるかに基づいてhjustを設定するgeom_textifelseステートメントを使用し、いくつかの問題を引き起こしていました。私はまた、expand=c(.1,.1)scale_y_continuousを追加して、少し詰め物を加えて、ラベルが覆われないようにしました。

enter image description here

+0

あなたのソリューションに希望のラベルをどのように追加しますか? 'labels = c(-1、 - 。5,0、.5,1)'? – PoGibas

+0

@PoGibas更新しました。 – Mako212

+0

@PoGibasと1つ以上のアップデートがあるので、すべてのラベルを見ることができます。 – Mako212

関連する問題