【问题标题】:Improving resolution on geom_text entry in ggplot2 r提高 ggplot2 r 中 geom_text 条目的分辨率
【发布时间】:2012-07-21 22:29:16
【问题描述】:

关于如何提高 geom_text 的分辨率以使分辨率与轴标签的分辨率相当的任何建议?谢谢

df <- data.frame("x" = c(1,2,3,4),"y" = c(15,19,35,47))

p<-ggplot(df,aes(x,y))
p<- p + geom_point(size=1)

p<- p + geom_smooth(method="lm", se=FALSE, formula=y~x)
p<- p + xlab("Better Resolution")
p<- p +ylab("Better Resolution")

p<- p +opts(axis.title.x = theme_text(family="Times",face="bold", size=25, colour = "Black",vjust=0)) 

p<- p +opts(axis.title.y = theme_text(family="Times",face="bold", size=25, angle =90, colour ="Black",vjust=0.4))

p<- p + geom_text(aes(x = 3.5, y = 37, label ="123456789"),size=12, parse = TRUE)
p

#The zoomed in text looks like this after saving using ggsave

#Information about my version of R and OS

sessionInfo()
R version 2.15.1 (2012-06-22)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

R.version
           _                            
platform       x86_64-apple-darwin9.8.0     
arch           x86_64                       
os             darwin9.8.0                  
system         x86_64, darwin9.8.0          
status                                      
major          2                            
minor          15.1                         
year           2012                         
month          06                           
day            22                           
svn rev        59600                        
language       R                            
version.string R version 2.15.1 (2012-06-22)
nickname       Roasted Marshmallows  

##############
#The actual code I am using looks like this:

#function that creates the line equation
lm_eqn = function(df){
m = lm(y ~ x, df)
eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,
list(a = format(coef(m)[1], digits = 2),
b = format(coef(m)[2], digits = 2),
r2 = format(summary(m)$r.squared, digits = 3)))
as.character(as.expression(eq))
}


#creates basic plot and adds a line
p<-ggplot(df, aes(x,y))
p<- p + geom_point(alpha=1/10, colour="blue", size=5)

#controls background colours
p<-p + theme_bw()

#adds the labels, titles and makes them pretty

p<- p + geom_smooth(method="lm", se=FALSE, formula=y~x,colour="black")
p<- p + xlab("Species similarity for site pair (Tsim variable 'a')")
p<- p +ylab("Trait similarity for site pairs (Tsim)")
p<- p +opts(title="Species vs. Trait combination similarity 2-5m")
p<- p +opts(plot.title = theme_text(family="Times",face="bold", size=18, colour =   "Black",vjust=1)) 
p<- p +opts(axis.title.x = theme_text(family="Times",face="bold", size=15, colour = "Black",vjust=0)) 
p<- p +opts(axis.title.y = theme_text(family="Times",face="bold", size=15, angle =90, colour =  "Black",vjust=0.4))

#adds the equation
p<- p + geom_text(aes(x = 0.015, y = 0.08, label = lm_eqn(df)),size=6,  family="Times",face="italic", parse = TRUE)

ggsave(p,file="tsim.a.0-2.pdf") 

【问题讨论】:

  • 您可能多次绘制相同的文本标签。尝试改用annotate,或者给最后一层数据。
  • 当我绘制它们时,我看不出两者之间有什么区别。 (而且我很确定他们不会多次绘制它,@baptiste)
  • 我也看不出有什么区别,但操作系统可能是没有做抗锯齿的罪魁祸首。
  • 感谢您的浏览!尽管@joran 的决议肯定更糟,但我将不得不不同意你的看法。使用数字时可能更突出。我正在使用 geom_text 添加一个描述线条的方程,它看起来很草率。我会将示例文本更改为数字以向您展示我的意思
  • @baptiste 我使用的是 Mac OS X 版本 10.7.4。抗锯齿是什么意思?谢谢

标签: r ggplot2


【解决方案1】:

为什么不使用ggsave 保存屏幕上的情节。您在屏幕上看到的不一定是使用 pdf 或 ps 设备在输出图形中呈现的内容。 在 Windows 7 上使用 ggplot 0.9.1 使用 R2.15.1 的未编辑代码,我没有发现任何问题。

我使用ggsave 保存了您的屏幕绘图并直接放大,pdf 看起来很棒:

使用ggsave("plot.pdf")(您可以设置其他几个可选参数,包括另存为 eps)。这会将最后一个图(默认情况下)保存到当前工作目录。检查情节。如果文字看起来仍然很有趣,我建议您的 Times 字体安装可能有问题。

在这种情况下,您应该尝试省略字体规范,以便 R 选择它的默认字体系列。

您还应该切换到theme 而不是optselement_text 而不是theme_text(目前!)。

**编辑**

好的,感谢kohskembask,我想我找到了您的问题here 的解决方案。显然,通过为您的标签创建一个数据框并将其传递给 geom_text 可以实现更好的结果。

尝试使用:

df <- data.frame("x" = c(1,2,3,4),"y" = c(15,19,35,47))

lm_eqn = function(df){
m = lm(y ~ x, df)
eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,
list(a = format(coef(m)[1], digits = 2),
b = format(coef(m)[2], digits = 2),
r2 = format(summary(m)$r.squared, digits = 3)))
as.character(as.expression(eq))
}

### NEW ###
# Create a data frame to hold your label variables
data.label <- data.frame(
x = 0.015,
y = 0.08,
label = c(lm_eqn(df))
)

#creates basic plot and adds a line
p<-ggplot(df, aes(x,y))
p<- p + geom_point(alpha=1/10, colour="blue", size=5)

#controls background colours
p<-p + theme_bw()

#adds the labels, titles and makes them pretty

p<- p + geom_smooth(method="lm", se=FALSE, formula=y~x,colour="black")
p<- p + xlab("Species similarity for site pair (Tsim variable 'a')")
p<- p +ylab("Trait similarity for site pairs (Tsim)")
p<- p +opts(title="Species vs. Trait combination similarity 2-5m")
p<- p +opts(plot.title = theme_text(family="Times",face="bold", size=18, colour =   "Black",vjust=1)) 
p<- p +opts(axis.title.x = theme_text(family="Times",face="bold", size=15, colour = "Black",vjust=0)) 
p<- p +opts(axis.title.y = theme_text(family="Times",face="bold", size=15, angle =90, colour =  "Black",vjust=0.4))

### NEW
####   Change your call to geom_text ####
p<- p + geom_text(data = data.label, aes(x = x , y = y , label = label ) , size=6,  family="Times" , face="italic" , parse = TRUE)

ggsave(p,file="tsim.a.0-2.pdf") 

我在 Mac OS X 10.7.4 和 R 2.15.1 上得到了这个:

【讨论】:

  • 感谢您的浏览。我正在使用 ggsave 但我的分辨率比您的屏幕截图差得多。除了简化示例之外,我在上面的问题中还包括了我正在使用的实际代码,以防代码中存在任何字体冲突? My Times 字体在轴标签和标题上看起来不错。这正是我通过 geom_text 添加的,似乎分辨率很差。还有其他想法吗?感谢您的宝贵时间。
  • @Elizabeth 此时,您将不得不上传 PDF 来演示发生了什么,因为到目前为止,没有人能够重现您所描述的内容。
  • @joran 当然可以。刚刚添加了一张显示问题的图片。
  • @Elizabeth 您使用的是什么版本的 R?使用sessionInfo()R.version 并粘贴输出。另外,您使用的是什么版本的 ggplot2 (packageVersion("ggplot2"))?上面渲染的图看起来与当前的 ggplot2 默认值明显不同。
  • @Elizabeth 如果您对答案感到满意,可以按绿色勾号以便我得到第一个接受的答案吗?!谢谢
【解决方案2】:

使用annotate() 而不是geom_text() 在图形上进行单个注释。否则,ggplot() 将尝试为每个数据点绘制相同的文本,从而导致奇怪的分辨率。

【讨论】:

  • 这就是OP问题的实际解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-04
  • 2015-04-19
  • 2020-06-04
  • 2020-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多