提高ggplot2中geom_text条目的分辨率r

前端之家收集整理的这篇文章主要介绍了提高ggplot2中geom_text条目的分辨率r前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有关如何改进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",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],r2 = format(summary(m)$r.squared,digits = 3)))
as.character(as.expression(eq))
}


#creates basic plot and adds a line
p<-ggplot(df,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",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",size=18,colour =   "Black",vjust=1)) 
p<- p +opts(axis.title.x = theme_text(family="Times",size=15,vjust=0)) 
p<- p +opts(axis.title.y = theme_text(family="Times",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")

解决方法

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

我使用ggsave保存了您的屏幕上的图形,并放大并且pdf看起来很棒:@H_502_12@

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

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

你也应该改为主题而不是opts和element_text而不是theme_text(目前!).@H_502_12@

**编辑**@H_502_12@

好的,我想我已经找到了解决您的问题here感谢kohskembask.显然,更好的结果可以通过为您的标签创建一个数据框,并以这种方式传递给geom_text可以实现.@H_502_12@

尝试使用:@H_502_12@

df <- data.frame("x" = c(1,47))

lm_eqn = function(df){
m = lm(y ~ x,digits = 3)))
as.character(as.expression(eq))
}

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

#creates basic plot and adds a line
p<-ggplot(df,vjust=0.4))

### NEW
####   Change your call to geom_text ####
p<- p + geom_text(data = data.label,aes(x = x,y = y,label = label ),file="tsim.a.0-2.pdf")

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

原文链接:https://www.f2er.com/html/230873.html

猜你在找的HTML相关文章