1

私はggrepelを使用して、二次元プロットにいくつかの名前を書き込んでいます。追加の注釈もいくつかあります。まったく同じコードの場合、一部の実行では 2 つの重複があり、一部の実行では重複していないことに気付きました。特に悪いのは、実際にプロットのマージンを変更し、他の単語と衝突しない単語でそれを実行するため、ggrepel が動き回らないことです。

geom_text_repel の代わりに geom_text を使用すると、問題が解決するか、シードも設定すると問題は解決しますが、さまざまな理由でどちらも実行できません。ggrepel には名前をシャッフルするためのランダムなコンポーネントが必要であることは理解していますが、これがプロットの制限をどのように変更するのかわかりません。これがサンプル コードです。違いを確認するには、数回実行する必要があります (右上に表示されます。Sirius B. は、一部の実行では「Controvers」と衝突し、他の実行では衝突しません)。

require(ggplot2)
require(ggrepel)
#set.seed(1)
# sample data
a = c(5, 6, 7, 6, 24, 4, 3, 5, 26, 8, 13, 4, 8, 11, 0, 11, 7, 5, 3, 10, 11, 8)
b = c(16 ,19 ,17 ,17 ,21 ,11 ,8 ,11 ,32 ,11 ,24 ,14 ,11 ,17 ,14 ,24 ,14 ,11 ,12 ,18 ,12 ,21)
noms = c("Hermione G." ,"Neville L." ,"Luna L." ,"Ron W." ,"Ginny W." ,"Percy W." ,"Lilly P." ,"Seamus F." ,"Sirius B." ,"Dean T." ,"Draco M." ,"Harry P." ,"Xo X." ,"Viktor K." ,"Hannah A." ,"Susan B." ,"Pansy P." ,"Fleur D." ,"Cormac M." ,"Cedric D." ,"Fay D." ,"Maisy R.")

# this is just to reproduce my exact results
df = cbind.data.frame(a, b, noms)
df[, 1] = scale(df[, 1])
df[, 2] = scale(df[, 2])

max_y = max(max(df[, 1]), abs(min(df[, 1])))
max_x = max(max(df[, 2]), abs(min(df[, 2])))
# actual plot
ggplot(df, aes(x = df[, 2], y = df[, 1], label = noms)) + 
  geom_text_repel(fontface = "bold") + 
  geom_text(aes(x = max_x - 0.25, y = max_y - 0.15, label = "Controvers"), fontface = "italic", angle = 40) +
  xlim(c(-max_x - .1, max_x + .1)) + 
  ylim(c(-max_y - .1, max_y + .1)) +
  theme_void() + 
  ggsave(file = "file.pdf", dpi = 1200, width = 25, height = 20, units = "cm") 

Windows 10 で R 3.5.3、ggplot2 3.1.1、ggrepel 0.8.1 を使用しています。

4

1 に答える 1