如何抖动文本以避免在ggplot2散点图中重叠? [英] How to jitter text to avoid overlap in a ggplot2 scatterplot?

查看:1317
本文介绍了如何抖动文本以避免在ggplot2散点图中重叠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在ggplot2中创建一个纯文本标签散点图。目标是视觉上呈现与约25项相关的增加值。我已经在使用position_jitter,但我想知道我能做得更好。



以下是一些模拟数据:

  title<  -  rep(A Really Rather Long Text Label,25)
value< - runif(25,1,10)
spacing < - seq(1:25)
df< - data.frame(title,value,spacing,stringsAsFactors = FALSE)

下面是生成图的代码:

  library(ggplot2)
myplot< - ggplot(data = df,aes(x = spacing,y = value,label = title))+
geom_text(aes(color = value),
size = 2.5,fontface =bold,
vjust = 0,
position = position_jitter(width = 5,height = 0))+
theme_bw()+
scale_x_continuous(limits = c( - 5,30))+
scale_colour_gradient(low =#6BAED6,high =#08306B)+
theme(axis.title.x = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_blank(),
legend.position =none)
myplot

在合理大小的图表中,所有文字都有足够的空间 - 只要文字可以随意移动水平!我不想垂直抖动,因为重点是显示与每个文本标签关联的y值。

每次运行ggplot命令时,图形都会略有不同 - 有时抖动结果比其他时间更好。但我还没有找到一种方法来系统地防止标签重叠。



对于如何清理文本的水平位置的任何建议,我将不胜感激而无需手动移动单个项目。我也很高兴听到关于如何改善这种类型的视觉表示的其他提示。

看看 FField

  install.packages(FField,type =源代码)
install.packages(ggplot2)
install.packages(gridExtra)
library(FField)
FFieldPtRepDemo()

(来自此文章 https:// stats .stackexchange.com / a / 62856

I would like to create a clean version of a scatterplot of text labels in ggplot2. The goal is to represent visually the increasing values associated with about 25 items. I am already using "position_jitter," but I wonder if I can do better.

Here is some mock data:

title <- rep("A Really Rather Long Text Label", 25)
value <- runif(25, 1,10)
spacing <- seq(1:25)
df <- data.frame(title, value, spacing, stringsAsFactors = FALSE)

And here is the code that generates the graph:

library(ggplot2)
myplot <- ggplot(data=df, aes(x=spacing, y=value, label = title)) +
geom_text(aes(colour = value),
    size = 2.5, fontface = "bold",
    vjust = 0,
    position = position_jitter(width=5, height=0)) +
theme_bw() +
scale_x_continuous(limits = c(-5, 30))+
scale_colour_gradient(low = "#6BAED6", high = "#08306B") +
theme(axis.title.x = element_blank(),
      axis.ticks = element_blank(),
      axis.text.x = element_blank(),
      legend.position = "none")
myplot

There is plenty of space for all of this text in a graph of reasonable size -- so long as the text is free to shift as far as it needs to horizontally! I don't want to jitter vertically, because the point is to show the y value associated with each text label.

The graph comes out slightly differently every time you run the ggplot command -- and sometimes the jittering results are better than other times. But I haven't found a way to systematically prevent the labels from overlapping.

I'd be grateful for any suggestions on how to clean up the horizontal positioning of the text without having to move individual items manually. I'd also be glad to hear other tips on how to improve this type of visual representation.

解决方案

Have a look at the FField package

install.packages("FField", type = "source")
install.packages("ggplot2")
install.packages("gridExtra")
library(FField)
FFieldPtRepDemo()

(from this post https://stats.stackexchange.com/a/62856)

这篇关于如何抖动文本以避免在ggplot2散点图中重叠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆