<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PMean &#187; Cheat sheet</title>
	<atom:link href="http://blog.pmean.com/tag/cheat-sheet/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.pmean.com</link>
	<description>A blog about statistics, evidence-based medicine, and research ethics</description>
	<lastBuildDate>Sat, 22 Jun 2024 17:10:26 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.7.41</generator>
	<item>
		<title>PMean: Measuring pixels in an R graph</title>
		<link>http://blog.pmean.com/measuring-pixels/</link>
		<comments>http://blog.pmean.com/measuring-pixels/#comments</comments>
		<pubDate>Tue, 01 Nov 2016 21:50:15 +0000</pubDate>
		<dc:creator><![CDATA[pmean]]></dc:creator>
				<category><![CDATA[Statistics]]></category>
		<category><![CDATA[Cheat sheet]]></category>
		<category><![CDATA[R software]]></category>

		<guid isPermaLink="false">http://blog.pmean.com/?p=970</guid>
		<description><![CDATA[I have an R cheat sheet, How Big Is Your Graph, that explains how to measure the size of various features of your graph in R. This blog post illustrates unit conversions. If you want to measure the length of a diagonal line segment in an R graph, you need to calculate the size of [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>I have an R cheat sheet, <a href="http://blog.pmean.com/cheatsheets/">How Big Is Your Graph</a>, that explains how to measure the size of various features of your graph in R. This blog post illustrates unit conversions. If you want to measure the length of a diagonal line segment in an R graph, you need to calculate the size of the plotting region in pixels, compare that to the range of the plotting region in the x and y directions, and then apply the Pythagorean Theorem.<span id="more-970"></span></p>
<p>For many applications, you need to be able to translate user coordinates to pixels or inches. Here’s a simple example.</p>
<p>The following code was run inside a program chunk with fig.width=1.7, fig.height=1.7.</p>
<pre class="r"><code>par(mar=c(1.9, 1.9, 0.1, 0.1), cex=0.75,
  cex.axis=0.75, xaxs="i", yaxs="i")
plot(c(-1,9), c(-1,7), type="n")
lines(x=c(0,8,8,0), y=c(0,0,6,0)) # triangle
user.range &lt;- par("usr")[c(2,4)] -
  par("usr")[c(1,3)]
region.pct &lt;- par("plt")[c(2,4)] - 
  par("plt")[c(1,3)]
region.px &lt;- dev.size(units="px") * region.pct
px.per.usr &lt;- region.px / user.range

a.px &lt;- round(8*px.per.usr[1])
b.px &lt;- round(6*px.per.usr[2])
c.px &lt;- round(sqrt(a.px^2+b.px^2))

text(4, -0.5, paste("8 x =", a.px, "px"))
text(8.5, 3, paste("6 y =", b.px, "px"), srt=90)
text(4, 3.5, paste("10 diag =", c.px, "px"), srt=45)
# Note: srt=45 is just a rough guess

s1 &lt;- paste("&lt;&lt;", round(user.range[1]),
  "x =", round(region.px[1]), "px &gt;&gt;")

s2 &lt;- paste("&lt;&lt;", round(user.range[2]),
  "y =", round(region.px[2]), "px &gt;&gt;")

text(4, 6.5, s1)
text(-0.5, 3, s2, srt=90)</code></pre>
<p><img alt="" src="http://www.pmean.com/0000images/pixel01.jpg" /></p>
<p>This page has moved to <a href="http://new.pmean.com/measuring-pixels/">a new website</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.pmean.com/measuring-pixels/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PMean: Rotating text in an R graph</title>
		<link>http://blog.pmean.com/rotating-text/</link>
		<comments>http://blog.pmean.com/rotating-text/#comments</comments>
		<pubDate>Tue, 01 Nov 2016 00:11:13 +0000</pubDate>
		<dc:creator><![CDATA[pmean]]></dc:creator>
				<category><![CDATA[Statistics]]></category>
		<category><![CDATA[Cheat sheet]]></category>
		<category><![CDATA[R software]]></category>

		<guid isPermaLink="false">http://blog.pmean.com/?p=963</guid>
		<description><![CDATA[I have an R cheat sheet, How Big Is Your Graph, that explains how to measure the size of various features of your graph in R. This blog post illustrates how you can use some of the commands described in that cheat sheet to rotate text to match a diagonal line in an R graph. [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>I have an R cheat sheet, <a href="http://blog.pmean.com/cheatsheets/">How Big Is Your Graph</a>, that explains how to measure the size of various features of your graph in R. This blog post illustrates how you can use  some of the commands described in that cheat sheet to rotate text to match a diagonal line in an R graph. It&#8217;s trickier than it seems.<span id="more-963"></span></p>
<p>p&gt;There are times when you want to put text on a diagonal slope, rather than vertical or horizontal. Diagonals are tricky because you need to adjust between the slope as measured in terms of the x-y coordinates and the slope as measured in terms of the physical plot itself, typically in pixels. Here’s an example where you might want diagonal text.</p>
<pre class="r"><code>plot(0, 0, xlim=c(0, 20), ylim=c(0, 100), type="n", xlab=" ", ylab=" ")
x0 &lt;-  5
x1 &lt;- 10
x2 &lt;- 15
y0 &lt;- 20
y1 &lt;- 50
y2 &lt;- 80
lines(x=c(x0, x2, x2, x0), y=c(y0, y0, y2, y0))
text(x1, y0-1.5*strheight("a", units="user"), "a")
text(x2+1.5*strwidth("b", units="user"), y1, "b")
delta.x &lt;- x2 - x0
delta.y &lt;- y2 - y0
ctext &lt;- expression(sqrt(a^2+b^2))
text(x1, y1+1.5*strheight(ctext, units="user"), ctext)</code></pre>
<p><img alt="Graph with unrotated text" src="http://www.pmean.com/0000images/rotate01.jpg" width="448" height="320" /><br />
That looks nice, but wouldn’t it be better if you could tilt the formula on the hypotenuse? You can do this with the srt argument, but you need transform the dimension in the x-y coordinates to the dimension of inches or pixels.</p>
<pre class="r"><code>plot(0, 0, xlim=c(0, 20), ylim=c(0, 100), type="n", xlab=" ", ylab=" ")
lines(x=c(x0, x2, x2, x0), y=c(y0, y0, y2, y0))
text(x1, y0-1.5*strheight("a", units="user"), "a")
text(x2+1.5*strwidth("b", units="user"), y1, "b")
px.per.xy &lt;- par("cra") / par("cxy")
hyp.angle &lt;- atan2(delta.y*px.per.xy[2], delta.x*px.per.xy[1]) * 180 / pi
text(x1, y1+1.5*strheight(ctext, units="user"), ctext, srt=hyp.angle)</code></pre>
<p><img alt="Graph with rotated text" src="http://www.pmean.com/0000images/rotate02.jpg" width="448" height="320" /><br />
Let’s look at the conversion in detail. If you calculated the slope in x-y coordinates, you would get an angle of 81 degrees. That’s because in x-y coordinates, the line is very steep. The horizontal distance is only 10 units, but the vertical distance is 60 units. But when you look at the graph on the screen, the horizontal distance looks to be almost twice as big as the vertical distance. That’s because there are 51 pixels for each unit in the x direction and 6 pixels for each unit in the y direction. That means that you have 512 horizontal pixels and 337 vertical pixels. The arctangent in pixels will give you an angle of 33 degrees.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.pmean.com/rotating-text/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PMean: Drawing the perfect circle</title>
		<link>http://blog.pmean.com/circle/</link>
		<comments>http://blog.pmean.com/circle/#comments</comments>
		<pubDate>Mon, 31 Oct 2016 12:36:16 +0000</pubDate>
		<dc:creator><![CDATA[pmean]]></dc:creator>
				<category><![CDATA[Statistics]]></category>
		<category><![CDATA[Cheat sheet]]></category>
		<category><![CDATA[R software]]></category>

		<guid isPermaLink="false">http://blog.pmean.com/?p=954</guid>
		<description><![CDATA[I have an R cheat sheet, How Big Is Your Graph, that explains how to measure the size of various features of your graph in R. This blog post illustrates how you can use some of the commands described in that cheat sheet to draw a perfect circle. When you try to draw a circle [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>I have an R cheat sheet, <a href="http://blog.pmean.com/cheatsheets/">How Big Is Your Graph</a>, that explains how to measure the size of various features of your graph in R. This blog post illustrates how you can use  some of the commands described in that cheat sheet to draw a perfect circle.<span id="more-954"></span></p>
<p>When you try to draw a circle on a graph, it often ends up looking like an ellipse. Here’s an example.</p>
<pre class="r"><code>draw.circle &lt;- function(x0, y0, r, ...) {
  # draw a circle of radius r centered at x0, y0
  pi.seq &lt;- seq(0, 2*pi, length=100)
  x.circle &lt;- x0 + r * cos(pi.seq)
  y.circle &lt;- y0 + r * sin(pi.seq)
  polygon(x.circle, y.circle, ...)
}
plot(0, 0, xlim=c(0, 20), ylim=c(0, 50), type="n", xlab=" ", ylab=" ")
draw.circle(5, 25, 5, lty="dotted")
text(5, 80, "r=5 (usr)")</code></pre>
<p><img alt="Circle in user coordinates" src="http://www.pmean.com/0000images/perfect01.jpg" width="448" height="320" /></p>
<pre class="r"><code>dv &lt;- dev.size()
ma &lt;- par("mai")
us &lt;- par("usr")</code></pre>
<p>There are three things that are contributing to this problem.</p>
<ol style="list-style-type: decimal;">
<li>The default graph in R Markdown is rectangular. You can measure the size of a graph in inches with the <strong>dev.size()</strong> function: 7, 5</li>
<li>The default margins are uneven with more margins on the top and bottom compared to the left and right sides. You can measure the size of the margins in inches with <strong>par(“mai”)</strong>: 1.02, 0.82, 0.82, 0.42. The size of the bottom margin is listed first, followed by the left, top, and right margins.</li>
<li>The number of units on this graph are different for the x-axis and the y-axis. You can measure the minimum and maximum values on the x and y axes using <strong>par(“usr”)</strong>: -0.8, 20.8, -2, 52. Note that, by default, R adds an extra 4% to each end of the axis to reduce problems with clipping.</li>
</ol>
<p>It’s not necessarily bad to see an ellipse where you were expecting a circle. But if you do want a perfect circle, you need to calculate the size of the plotting region and adjust appropriately.</p>
<pre class="r"><code>draw.circle &lt;- function(x0, y0, r, adjust.units=function() {list(x=1, y=1)}, ...) {
  # draw a circle of radius r centered at x0, y0
  # with an adjustment in the horizontal and vertical dimensions
  # The default adjustment is no adjustment
  pi.seq &lt;- seq(0, 2*pi, length=100)
  adj &lt;- adjust.units()
  x.circle &lt;- x0 + adj$x * r * cos(pi.seq)
  y.circle &lt;- y0 + adj$y * r * sin(pi.seq)
  polygon(x.circle, y.circle, ...)
}

convert_to_inches &lt;- function() {
  # This funciton adjusts from user coordinates to inches.
  # There is a short-cut: ratio &lt;- par("cxy") / par("cin")

  # Range of the plotting region in user coordinates
  plot.range.usr &lt;- c(par("usr")[2] - par("usr")[1], par("usr")[4] - par("usr")[3])
  if (verbose) cat("\nThe range of the plotting region is", plot.range.usr, "in user coordinates")

  # Range of the plotting region in inches
  plot.range.in &lt;- par("pin")
  if (verbose) cat("\nThe range of the plotting region is", plot.range.in, "in inches")

  # Number of inches per user coordinate
  ratio &lt;- plot.range.usr / plot.range.in
  if (verbose) cat("\nThere are", ratio, "user coordinates per inch")
  return(list(x=ratio[1], y=ratio[2]))
}

plot(0, 0, xlim=c(0, 20), ylim=c(0, 50), type="n", xlab=" ", ylab=" ")
draw.circle( 5, 25, 5, lty="dotted")
text( 5, 25, "r=5 (usr)")
verbose &lt;- TRUE
draw.circle(15, 25, 0.5, convert_to_inches, col="red")</code></pre>
<pre><code>## 
## The range of the plotting region is 21.6 54 in user coordinates
## The range of the plotting region is 5.759999 3.159999 in inches
## There are 3.75 17.08861 user coordinates per inch</code></pre>
<pre class="r"><code>text(15, 25, "r=0.5 (in)")</code></pre>
<p><img alt="Circle in user coordinates" src="http://www.pmean.com/0000images/perfect02.jpg" width="448" height="320" /></p>
<p>The concept of inches in a graph is a loose one at best. The half inch radius shown above might appears slightly bigger or slightly smaller than a half inch and it will probably change when you move from one computer monitor to another. The size will probably change when you print this graph.</p>
<p>If you’re lucky, the relative size of an inch will mean the same thing in the horizontal and vertical direction, and this will give you a perfect circle, though of an uncertain size. But there is no guarantee even of this. See, for example,</p>
<p><a class="uri" href="https://cran.r-project.org/bin/windows/base/old/2.0.1/rw-FAQ.html#Circles-appear-as-ovals-on-screen">https://cran.r-project.org/bin/windows/base/old/2.0.1/rw-FAQ.html#Circles-appear-as-ovals-on-screen</a></p>
<p>You may wish to specify the size of your circle in pixels instead. This may or may not be a more stable measure. To do this, you need a measure of pixels per inch.</p>
<p>You can also draw a circle using <strong>pch=21</strong> or <strong>pch=“o”</strong> or <strong>pch=“O”</strong>. The latter two choices are not quite perfect circles, but come pretty close. You can control the size of these circles using the pointsize and/or cex arguments. With <strong>pch=21</strong>, you can control the fill color with the bg argument.</p>
<pre class="r"><code>plot(0, 0, xlim=c(0, 20), ylim=c(0, 50), type="n", xlab=" ", ylab=" ")
co &lt;- c("black", "blue", "red", "green")
points(rep( 5, 4), 10*(1:4), pch=21,  cex=1:4, col=co, bg=rev(co))
points(rep(10, 4), 10*(1:4), pch="o", cex=1:4, col=co)
points(rep(15, 4), 10*(1:4), pch="O", cex=1:4, col=co)</code></pre>
<p><img alt="Circle in user coordinates" src="http://www.pmean.com/0000images/perfect03.jpg" width="448" height="320" /></p>
<p>Notice the subtle differences in shape and thickness.</p>
<p>If you try this code on your system, your results may not match the results you see here, for a wide variety of reasons that are impossible to list. Graphics are often the result of trial and error, and if you don’t get the results you want right away, keep plugging away.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.pmean.com/circle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
