通过选择后记输出提高绘图质量

发布于 2024-12-19 11:08:39 字数 2583 浏览 2 评论 0原文

我仍在学习 SweaveR。我下面有一个示例代码,它读取数据文件并绘制它。我选择 postscript 选项,因为我喜欢最终得到 EPS 文件。我想对情节进行改进。这是我的代码,其中包含我自己的学习评论:

\documentclass[a4paper,12pt]{article}
\usepackage{Sweave}  %%%%%%
\SweaveOpts{eps=TRUE}

\begin{document}

<<echo=FALSE, results=hide>>=
test.frame<-data.frame(ratio= c(0.0, 144.321, 159.407, 178.413, 202.557), value= c(0, 0.84, 0.8925, 0.945, 0.9975))
@


<<echo=FALSE,results=hide,eval=TRUE>>=
postscript('doudou.eps',
               width=7, height=6,
               colormodel="cmyk",
               family = "ComputerModern",
               horizontal = FALSE,
               onefile=FALSE,
               paper = "special",
               encoding = "TeXtext.enc",
               pagecentre=FALSE)

with(test.frame,plot(ratio, value, ylab= "Hello",
                                   xlab="Wonderful",
                                   type="o",        # line and markers
                                   bty="o",         # box around graph
                                   lty="solid",     # solid line or put 1
                                   lwd=3,            # line width
                                   pch=1,            # or enclose symbol in quotes
                                   cex=3,             # size of markers
                                   cex.lab=2,        # label size
                                   cex.axis=1.5,    # axis annot size problem if big
                                   cex.main=2,          # main title size
                                   xaxp=c(0, 200, 4),  #c(x1, x2, n)
                                   col=2,              # plotting color
                                   xlim=c(0,200),
                                   yaxt = "n",         #suppresses axis
                                   main=" My curve"))

axis(2,seq(0,1, by=0.5), las=2,cex=3,cex.lab=2,cex.axis=1.5,cex.main=2)

dev.off()
@

\begin{figure}[htbp]
\begin{center}
\includegraphics[width=0.8\textwidth]{doudou.eps}
\end{center}
\end{figure}


\end{document}

我想了解更多有关改进的一些事情:

  1. 我在绘图周围有一个框框。如何控制其线宽?

  2. 我使用cex.axis=1.5作为轴注释大小。如果我将其更改为 cex.axis=3,那么 x 轴上的值就会变大,并且它们与刻度线重叠。有没有办法将 x 轴值放置得离绘图远一点?

  3. y 标签 Hello 在图中 H 字母的顶部被截断。如何解决此问题?

  4. 如何将 x 标签 Wonderful 或 y 标签 Hello 移离绘图更远?

  5. 如果我们查看绘制的曲线,尽管数据集的初始值为 (0,0),但轴并非从 (0,0) 开始。如何控制轴使其从 (0,0) 开始?

多谢...

I am still learning Sweave and R. I have below a sample code that reads in a data file and plots it. I am choosing the postscript option as I like ending up with an EPS file. There are a number of things that I would like to improve with the plot. Here is my code with my own comments for my own learning:

\documentclass[a4paper,12pt]{article}
\usepackage{Sweave}  %%%%%%
\SweaveOpts{eps=TRUE}

\begin{document}

<<echo=FALSE, results=hide>>=
test.frame<-data.frame(ratio= c(0.0, 144.321, 159.407, 178.413, 202.557), value= c(0, 0.84, 0.8925, 0.945, 0.9975))
@


<<echo=FALSE,results=hide,eval=TRUE>>=
postscript('doudou.eps',
               width=7, height=6,
               colormodel="cmyk",
               family = "ComputerModern",
               horizontal = FALSE,
               onefile=FALSE,
               paper = "special",
               encoding = "TeXtext.enc",
               pagecentre=FALSE)

with(test.frame,plot(ratio, value, ylab= "Hello",
                                   xlab="Wonderful",
                                   type="o",        # line and markers
                                   bty="o",         # box around graph
                                   lty="solid",     # solid line or put 1
                                   lwd=3,            # line width
                                   pch=1,            # or enclose symbol in quotes
                                   cex=3,             # size of markers
                                   cex.lab=2,        # label size
                                   cex.axis=1.5,    # axis annot size problem if big
                                   cex.main=2,          # main title size
                                   xaxp=c(0, 200, 4),  #c(x1, x2, n)
                                   col=2,              # plotting color
                                   xlim=c(0,200),
                                   yaxt = "n",         #suppresses axis
                                   main=" My curve"))

axis(2,seq(0,1, by=0.5), las=2,cex=3,cex.lab=2,cex.axis=1.5,cex.main=2)

dev.off()
@

\begin{figure}[htbp]
\begin{center}
\includegraphics[width=0.8\textwidth]{doudou.eps}
\end{center}
\end{figure}


\end{document}

Some things that I would like to know more about an improve:

  1. I have a boxed frame around the plot. How to control its line width?

  2. I am using cex.axis=1.5for the axis annotation size. if I change it to say cex.axis=3 then the values on the x-axis get big and they overlap with the tickmarks. Is there a way to then place the x-axis values a bit lower further away from the plot?

  3. The y-label Hello is truncated at the top of the H letter in the plot. How to fix this?

  4. How to move either the x-label Wonderful or y-label Hello further away from the plot?

  5. If we look at the plotted curve, the axes do not start at (0,0) though the initial values are (0,0) for the data set. How to control the axes so that they start at (0,0)?

Thanks a lot...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

护你周全 2024-12-26 11:08:39

“我在绘图周围有一个方框。如何控制它的线宽?”

 box(lwd=3)

“我使用 cex.axis=1.5 作为轴注释大小。如果我将其更改为 cex.axis=3 ,那么 x 轴上的值会变大并且它们与刻度线重叠。有没有办法将 x 轴值放置在离绘图更远的地方?”

par(mgp=c(3,1.5,0) )  # second element is number of lines below the box for the labels

“y 标签 Hello 在图中 H 字母的顶部被截断。如何修复此问题?”

# use par() to increase left margins

“如何将 x 标签 Wonderful 或 y 标签 Hello 移离绘图更远?”

par( mgp=c(4,1.5,0) ) # First element in mgp vector

“如果我们查看绘制的曲线,尽管数据集的初始值为 (0,0),但轴并非从 (0,0) 开始。如何控制轴,使其从 (0,0) 开始?”

 ..., xaxs="i", yaxs="i", ... # can be done in `par` or in the plot call

所以该图的R代码如下:
所以该图的 R 代码如下:

postscript('doudou.eps',
               width=7, height=6,
               colormodel="cmyk",
               family = "ComputerModern",
               horizontal = FALSE,
               onefile=FALSE,
               paper = "special",
               encoding = "TeXtext.enc",
               pagecentre=FALSE)
par( mgp=c(4,1.5,0), mai=c(1.5, 1.5, 1.5, .75) )  # using inches as the spacing unit
with(test.frame, plot(ratio, value, ylab= "Hello", 
                            xaxs="i", yaxs="i",
                                   xlab="Wonderful",
                                   type="o",        # line and markers
                                   bty="o",         # box around graph
                                   lty="solid",     # solid line or put 1
                                   lwd=3,            # line width
                                   pch=1,            # or enclose symbol in quotes
                                   cex=3,             # size of markers
                                   cex.lab=2,        # label size
                                   cex.axis=3,    # axis annot size problem if big
                                   cex.main=2,          # main title size
                                   xaxp=c(0, 200, 4),  #c(x1, x2, n)
                                   col=2,              # plotting color
                                   xlim=c(0,200),
                                   yaxt = "n",         #suppresses axis
                                   main=" My curve"))

axis(2,seq(0,1, by=0.5), las=2,cex=3,cex.lab=2,cex.axis=1.4, cex.main=2)
box(lwd=3)
dev.off()

不太漂亮,但它确实说明了控制功能。基本上你需要花更多的时间在帮助(par)页面上。

"I have a boxed frame around the plot. How to control its line width?"

 box(lwd=3)

"I am using cex.axis=1.5 for the axis annotation size. If I change it to say cex.axis=3 , then the values on the x-axis get big and they overlap with the tickmarks. Is there a way to then place the x-axis values a bit lower further away from the plot?"

par(mgp=c(3,1.5,0) )  # second element is number of lines below the box for the labels

"The y-label Hello is truncated at the top of the H letter in the plot. How to fix this?"

# use par() to increase left margins

"How to move either the x-label Wonderful or y-label Hello further away from the plot?"

par( mgp=c(4,1.5,0) ) # First element in mgp vector

"If we look at the plotted curve, the axes do not start at (0,0) though the initial values are (0,0) for the data set. How to control the axes so that they start at (0,0)?"

 ..., xaxs="i", yaxs="i", ... # can be done in `par` or in the plot call

So the R code for the figure is below:
So the R code for the figure is below:

postscript('doudou.eps',
               width=7, height=6,
               colormodel="cmyk",
               family = "ComputerModern",
               horizontal = FALSE,
               onefile=FALSE,
               paper = "special",
               encoding = "TeXtext.enc",
               pagecentre=FALSE)
par( mgp=c(4,1.5,0), mai=c(1.5, 1.5, 1.5, .75) )  # using inches as the spacing unit
with(test.frame, plot(ratio, value, ylab= "Hello", 
                            xaxs="i", yaxs="i",
                                   xlab="Wonderful",
                                   type="o",        # line and markers
                                   bty="o",         # box around graph
                                   lty="solid",     # solid line or put 1
                                   lwd=3,            # line width
                                   pch=1,            # or enclose symbol in quotes
                                   cex=3,             # size of markers
                                   cex.lab=2,        # label size
                                   cex.axis=3,    # axis annot size problem if big
                                   cex.main=2,          # main title size
                                   xaxp=c(0, 200, 4),  #c(x1, x2, n)
                                   col=2,              # plotting color
                                   xlim=c(0,200),
                                   yaxt = "n",         #suppresses axis
                                   main=" My curve"))

axis(2,seq(0,1, by=0.5), las=2,cex=3,cex.lab=2,cex.axis=1.4, cex.main=2)
box(lwd=3)
dev.off()

Not pretty, but it does illustrate the control functions. Basically you need to spend more time on the help(par) page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文