gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 14:10:05 25 4
gpt4 key购买 nike

关于如何提高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")

最佳答案

为什么不使用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(当前!)。

**编辑**

好的,我想我已经通过herekohske找到了解决您问题的方法mbask。通过为标签创建一个数据框并将其传递给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上得到了这个:

关于r - 提高ggplot2 r中geom_text条目的分辨率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11596340/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com