有没有办法在 R 中的三元图上设置不重叠的数据点标签?
Is there a way to have non-overlapping data point labels on a ternery plot in R?
我正在尝试复制我在 https://fivethirtyeight.com/features/how-cable-news-reacted-to-the-cohen-hearing/ 的 FiveThirtyEight 上找到的图表。这显示了一个三联图,其中单词在 3 轴中的位置显示了相应网络引用的比例。
我目前正在使用 R、ggplot2 和更重要的 ggtern(我将其广泛用于三联图)。然而,我从来没有找到一种方法来使点 上的数据标签不 重叠。我一直希望 ggtern 能与 ggrepel 交互,但遗憾的是它没有(据我所知)。 有什么方法可以强制它们相互作用,或者找到另一种方法吗?
图表显示在 link 中,以便清楚地了解我所追求的内容:
我的图表示例,其中单词重叠且看起来很糟糕:
编辑
创建丑陋图表的代码:
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()
好的,所以您需要 ggrepel
包中的功能。虽然 ggrepel 在这里不起作用,但您可以使用 position_nudge_tern
和 check_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)
这将为您提供非重叠标签:
我正在尝试复制我在 https://fivethirtyeight.com/features/how-cable-news-reacted-to-the-cohen-hearing/ 的 FiveThirtyEight 上找到的图表。这显示了一个三联图,其中单词在 3 轴中的位置显示了相应网络引用的比例。
我目前正在使用 R、ggplot2 和更重要的 ggtern(我将其广泛用于三联图)。然而,我从来没有找到一种方法来使点 上的数据标签不 重叠。我一直希望 ggtern 能与 ggrepel 交互,但遗憾的是它没有(据我所知)。 有什么方法可以强制它们相互作用,或者找到另一种方法吗?
图表显示在 link 中,以便清楚地了解我所追求的内容:
我的图表示例,其中单词重叠且看起来很糟糕:
编辑 创建丑陋图表的代码:
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()
好的,所以您需要 ggrepel
包中的功能。虽然 ggrepel 在这里不起作用,但您可以使用 position_nudge_tern
和 check_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)
这将为您提供非重叠标签: