ggplot2:是否有解决由geom_text()生成的参差不齐的低质量文本? [英] ggplot2: Is there a fix for jagged, poor-quality text produced by geom_text()?

查看:793
本文介绍了ggplot2:是否有解决由geom_text()生成的参差不齐的低质量文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在给plot添加注解文本时,我注意到 geom_text()产生了难看的锯齿文本,而 annotate()制作出流畅,漂亮的文字。有谁知道为什么发生这种情况,如果有什么方法可以解决它?我知道我可以在这里使用 annotate(),但可能有些情况下 geom_text()是可取的,我想找一个修复程序。另外, geom_text()不能是意图给文本看起来不好看,所以要么我做错了,要么我遇到某种微妙的副作用。

以下是一些假数据和生成图形的代码,以及显示结果的图像。

  library(ggplot2)
age = structure(list(age = c(41L,40L,43L,44L, 40L,42L,44L,45L,
44L,41L,43L,40L,43L,43L,40L,42L,43L,44L,43L,41L)),
.Names =age .names = c(NA,-20L),class =data.frame)
ggplot(age,aes(age))+
geom_histogram()+
scale_x_continuous(breaks = seq (40,45,1))+
stat_bin(binwidth = 1,color =black,fill =blue)+
geom_text(41,5.2,
label =粘贴(Average =,round(mean(age),1))),size = 12)+
annotate(text,x = 41,y = 4.5,
label = paste Average =,round(mean(age $ age),1)),size = 12)

解决方案<尽管没有直接从 age data.frame中使用任何东西,但它仍在使用它为其数据源。因此,它将20个Average = 42.3的副本放在该图上,每行一个。这是多重覆盖,使它看起来很糟糕。 geom_text 旨在将文本放置在信息来自data.frame的图上(它直接或间接地在原始 ggplot call)。 annotate 专为简单的一次性添加而设计(它会创建一个 geom_text ,处理数据源)。



如果您真的想使用 geom_text(),只需重置数据源:

  ggplot(age,aes(age))+ 
scale_x_continuous(breaks = seq(40,45,1))+
stat_bin(binwidth = 1,color =black,fill =blue)+
geom_text(aes(41,5.2,
label = paste(Average =,round(mean年龄$年龄),1))),size = 12,
data = data.frame())+
annotate(text,x = 41,y = 4.5,
label = paste(Average =,round(mean(age $ age),1)),size = 12)


While adding annotation text to a plot I noticed that geom_text() produced unsightly, jagged text, while annotate() produced smooth, nice-looking text. Does anyone know why this happens and if there's any way to fix it? I know I could just use annotate() here, but there are probably cases where geom_text() is preferable, and I'd like to find a fix. Also, geom_text() can't be intended to give poor-looking text, so either I'm doing something wrong, or I've run into some sort of subtle side effect.

Here's some fake data and the code to produce the graph, plus an image showing the results.

library(ggplot2)
age = structure(list(age = c(41L, 40L, 43L, 44L, 40L, 42L, 44L, 45L, 
        44L, 41L, 43L, 40L, 43L, 43L, 40L, 42L, 43L, 44L, 43L, 41L)), 
        .Names = "age", row.names = c(NA, -20L), class = "data.frame")
ggplot(age, aes(age)) + 
  geom_histogram() +
  scale_x_continuous(breaks=seq(40,45,1)) +
  stat_bin(binwidth=1, color="black", fill="blue") +
  geom_text(aes(41, 5.2, 
            label=paste("Average = ", round(mean(age),1))), size=12) +
  annotate("text", x=41, y=4.5, 
           label=paste("Average = ", round(mean(age$age),1)), size=12)

解决方案

geom_text, despite not using anything directly from the age data.frame, is still using it for its data source. Therefore, it is putting 20 copies of "Average=42.3" on the plot, one for each row. It is that multiple overwriting that makes it look so bad. geom_text is designed to put text on a plot where the information comes from a data.frame (which it is given, either directly or indirectly in the original ggplot call). annotate is designed for simple one-off additions like you have (it creates a geom_text, taking care of the data source).

If you really want to use geom_text(), just reset the data source:

ggplot(age, aes(age)) + 
  scale_x_continuous(breaks=seq(40,45,1)) +
  stat_bin(binwidth=1, color="black", fill="blue") +
  geom_text(aes(41, 5.2, 
            label=paste("Average = ", round(mean(age$age),1))), size=12,
            data = data.frame()) +
  annotate("text", x=41, y=4.5, 
           label=paste("Average = ", round(mean(age$age),1)), size=12)

这篇关于ggplot2:是否有解决由geom_text()生成的参差不齐的低质量文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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