有没有一种方法可以在R中的ternette图上具有不重叠的数据点标签?

时间:2019-04-03 09:33:32

标签: r ggplot2 ggtern

我正在尝试复制在https://fivethirtyeight.com/features/how-cable-news-reacted-to-the-cohen-hearing/的FiveThirtyEight上找到的图表。这显示了一个三角图,其中单词在3轴上的位置显示了该相应网络所引用的比例。

我目前正在使用R,ggplot2,更重要的是ggtern(我在ternery图中广泛使用)。但是,我从未找到一种方法来使点上的数据标签重叠。我一直希望ggtern与ggrepel互动,但不幸的是,据我所知,它不会。 有没有办法强迫他们互动,或者找到另一种方式?

为清楚起见,链接中显示了图表: FiveThirtyEight ternery example

我的图表示例中单词重叠且看起来很糟糕: Bad ternery plot and bad visualization

编辑 创建我的可怕图表的代码:

    data <- data.frame(word = c("A","random","set","of","words","that","can","hopefully","help","someone","solve","my","issue","of","overlapping","labels","and","make","my","chart","readable","and","a","good","visualization"),
               axis1 = sample(1:100),
               axis2 = sample(1:100),
               axis3 = sample(1:100))

    ggtern(data = data,
        aes(x = axis1, y = axis2, z = axis3, colour = word, label = word)) +
      geom_point(size = 1) +
      geom_text()

1 个答案:

答案 0 :(得分:0)

好吧,所以您需要ggrepel包中的功能。虽然ggrepel在这里不起作用,但是您可以使用position_nudge_terncheck_overlap

word = c("A","random","set","of","words","that","can","hopefully","help","someone","solve","my","issue","of","overlapping","labels","and","make","my","chart","readable","and","a","good","visualization")
col = c("red", "blue", "green", "red", "blue", "green","red", "blue", "green", "red", "blue", "green","red", "blue", "green", "red", "blue", "green","red", "blue", "green", "red", "blue", "green","red")

n  = 25   #Number of Data Points
nv = 0.1  #Vertical Adjustment
pn = position_nudge_tern(y=nv,x=-nv/2,z=-nv/2)

data <- data.frame(x = sample(1:25),
               y = sample(1:25),
               z = sample(1:25), 
               label=word)

ggtern(data = data, aes(x = x, y = y, z = z, colour = col, label = word)) +
  geom_point(size = 1) +
  theme_nomask() + #Allow Labels to Spool Over Edges
  geom_text(position=pn,aes(label=word),check_overlap=T, size=5)

哪个会给您不重叠的标签:enter image description here