1

次のグラフのように、ggplot2 を使用してバープロットにラベルを付けることができます。

ここに画像の説明を入力

次のコードを使用して、添付のプロットを取得しています。

library(ggplot2)

test <- data.frame(x = c("Moderately Poor", "Deeply Poor", "Deeply & Intensely Poor", "Intensely Poor", "Overall Poverty"), y = c(0.024, -0.046, -0.025, -0.037, -0.083))

test$colour <- ifelse(test$y < 0, "firebrick1", "steelblue")
test$hjust <- ifelse(test$y > 0, 1.03, -0.03)

ggplot(test, aes(x, y, label = x, hjust = hjust)) + 
    geom_text(aes(y = 0, colour = colour)) + 
    geom_bar(stat = "identity", aes(fill = colour))

last_plot() + coord_flip() + labs(x = "", y = "") +
    scale_x_discrete(breaks = NA) + theme_bw() +
    opts(legend.position = "none")

各バーの 2 番目の数値ラベルを取得するにはどうすればよいですか?

ありがとう、

4

1 に答える 1

3

を含む R グラフィックスは、ggplot2紙にペンで描かれます。つまり、layers(それぞれgeom_...) が順番に描画されます。

geom_textしたがって、 を の上に置きたい場合は、 をgeom_bargeom_text後に置く必要がありgeom_barます。

ggplot20.9.3 (現在のバージョン)への更新

ggplot(test, aes(x, y)) + 
  geom_text(aes(y = 0, colour = colour, hjust =hjust, label = x), size=4.5) + 
  geom_bar(stat = "identity", aes(fill = colour)) + 
  geom_text(colour ='black', 
     aes(label = paste( formatC( round(y*100, 2 ), format='f', digits=2 ),'%'),
         hjust = ifelse(y>0,1,0))) +
  coord_flip() + labs(x = "", y = "") +
  scale_x_discrete(breaks = NULL) + theme_bw() +
  theme(legend.position = "none")

生産する

ここに画像の説明を入力

于 2013-03-01T03:31:58.477 に答える