<?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>Jakob Richter</title>
	<atom:link href="http://berlani.de/feed/" rel="self" type="application/rss+xml" />
	<link>http://berlani.de</link>
	<description>Statistik, R, Fotografie und Sonstiges</description>
	<lastBuildDate>Sat, 19 Jan 2013 22:09:18 +0000</lastBuildDate>
	<language>de-DE</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.1</generator>
		<item>
		<title>ggplot2: Tipps für Grafiken in schwarz weiß</title>
		<link>http://berlani.de/2012/07/r-statistik/r/ggplot2-tipps-fur-grafiken-in-schwarz-weis/</link>
		<comments>http://berlani.de/2012/07/r-statistik/r/ggplot2-tipps-fur-grafiken-in-schwarz-weis/#comments</comments>
		<pubDate>Sat, 28 Jul 2012 16:09:10 +0000</pubDate>
		<dc:creator>Jakob</dc:creator>
				<category><![CDATA[R]]></category>

		<guid isPermaLink="false">http://berlani.de/?p=391</guid>
		<description><![CDATA[Das ggplot2 hübschere und buntere Grafiken als unter R standard erzeugt sollte mittlerweile jeder wissen. Doch es geht mit ein paar Tricks auch in Graustufen und druckerfreundlich. Am schnellsten geht es in dem wir unserem ggplot-Objekt einfach ein theme_bw() hinzufügen (g]]></description>
			<content:encoded><![CDATA[<p>Das ggplot2 hübschere und buntere Grafiken als unter R standard erzeugt sollte mittlerweile jeder wissen. Doch es geht mit ein paar Tricks auch in Graustufen und druckerfreundlich.<br />
Am schnellsten geht es in dem wir unserem ggplot-Objekt einfach ein <code>theme_bw()</code> hinzufügen (<code>g <- g + theme_bw()</code>). Unter umständen ist man mit dem Ergebnis sogar schon zufrieden. Wer das Theme nicht so schön findet, dem sei <a href="https://github.com/hadley/ggplot2/wiki/Themes" title="Themes für ggplot2 selber definieren." target="_blank">diese informative Seite</a> ans Herz gelegt.<br />
Wichtig ist, dass bei umfangreicheren ggplot2-Objekten das Farbschema schon am Anfang hinzugefügt wird, da ansonsten Einstellungen die in <code>+opts()</code> vorgenommen wurden überschrieben werden können.</p>
<h3>Ein Beispiel</h3>
<p>Nehmen wir die recht umfangreiche schon erstellte Grafik aus dem <a href="http://berlani.de/2012/06/r-statistik/r/ggplot2-ein-umfangreiches-beispiel/" title="ggplot2 – ein umfangreiches Beispiel" target="_blank">letzten ggplot2-Beitrag</a>.</p>
<p>Machen wir von Anfang an klar, welches Theme wir nutzen wollen:</p>

<div class="wp_codebox"><table><tr id="p3914"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p391code4"><pre class="rsplus" style="font-family:monospace;">g <span style="color: #080;">&lt;-</span> <a href="http://google.com/search?btnI=1&q=ggplot&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">ggplot</span></a><span style="color: #080;">&#40;</span>mdat,<a href="http://google.com/search?btnI=1&q=aes&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">aes</span></a><span style="color: #080;">&#40;</span>y<span style="color: #080;">=</span>value,x<span style="color: #080;">=</span>Jahr<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> <a href="http://google.com/search?btnI=1&q=theme_bw&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">theme_bw</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>Hier verwenden wir nun anstatt <code>color</code> für die Färbung der Punkte einfach <code>shape</code>. Dies entspricht genau dem <code>pch</code> wie man es aus R kennt.<br />
<span id="more-391"></span><br />
Für den Konfidenzintervall ist die Flächenfarbe (<code>fill=</code>) schwarz und die Farbe der Mittellinie (<code>fill=</code>) grau. Ich finde es übrigens sehr praktisch Farben in der Hex-Schreibweise einzugeben, da mit wenig Tipparbeit die Farbkomponenten festgelegt sind: #<span style="color:#F00">FF</span><span style="color:#0F0">FF</span><span style="color:#00F">FF</span> oder man <a href="http://www.hexfarben.de/" title="HEX-Colorpicker" target="_blank">sucht</a> sich einfach eine <a href="http://www.farb-tabelle.de/de/farbtabelle.htm" title="Farbtabelle" target="_blank">schöne Farbe</a>.<br />
Das nur nebenbei. Wichtig ist die Angabe von <code>alpha=</code> der <em>Un</em>durchsichtigkeit des Geoms, da ansonsten alles tatsächlich schwarz wäre.</p>

<div class="wp_codebox"><table><tr id="p3915"><td class="line_numbers"><pre>3
4
5
</pre></td><td class="code" id="p391code5"><pre class="rsplus" style="font-family:monospace;">g <span style="color: #080;">&lt;-</span> g <span style="color: #080;">+</span> <a href="http://google.com/search?btnI=1&q=geom_point&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">geom_point</span></a><span style="color: #080;">&#40;</span><a href="http://google.com/search?btnI=1&q=aes&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">aes</span></a><span style="color: #080;">&#40;</span>shape<span style="color: #080;">=</span>Probenart<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
  <a href="http://google.com/search?btnI=1&q=stat_smooth&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">stat_smooth</span></a><span style="color: #080;">&#40;</span>fill<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;black&quot;</span>,color<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;#555555&quot;</span>,alpha<span style="color: #080;">=</span><span style="color: #ff0000;">0.1</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
  <a href="http://google.com/search?btnI=1&q=facet_wrap&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">facet_wrap</span></a><span style="color: #080;">&#40;</span>~Analyt<span style="color: #080;">+</span>Messeinheit, scales<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;free_y&quot;</span>, <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/ncol.html"><span style="color: #0000FF; font-weight: bold;">ncol</span></a><span style="color: #080;">=</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>Im folgenden Schritt passen wir noch die vielen Optionen aus dem Ausgangsbeispiel an. Aus <code>guides(color = ...)</code> muss natürlich <code>guides(shape = ...)</code> werden, da wir ja nun die Legende für die Symbole (Shapes) modifizieren wollen.<br />
Hinzu kommt noch - und das ist relativ wichtig - die Festlegung der shape-Scale via <code>+scale_shape_manual(values=c(1,2,...))</code>, denn unter der Standardeinstellung gibt es nur 6 verschiedene Symbole, alles drüber beleibt leer. Welche Zahl für welches Symbol steht ist wie bei den base graphics.</p>
<div id="attachment_393" class="wp-caption alignnone" style="width: 610px"><img src="http://berlani.de/wp-content/uploads/2012/07/pch.png" alt="" title="pch Zahlen und ihre Symbole" width="600" height="100" class="size-full wp-image-393" /><p class="wp-caption-text">Welche Nummer liefert welches Symbol. Hier eine kleine Zuordnung. Wenn mann es mal schnell braucht einfach <code>plot(0:25,1,pch=0:25)</code> ausführen.</p></div>
<p>Alternativ kann man auch Irgendwelche Zeichen verwenden. Also <code>scale_shape_manual(values=<b>c("a","#","?",...)</b>)</code>. Dann sollte jedoch mit <code>geom_point(aes(...), <b>size=2</b>)</code> die Zeichengröße etwas erhöht werden.</p>
<p>Das war es auch schon an Anpassungen, die vorgenommen werden müssen, um solch einen kopierfreundlichen Plot zu erzeugen.</p>

<div class="wp_codebox"><table><tr id="p3916"><td class="line_numbers"><pre>7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code" id="p391code6"><pre class="rsplus" style="font-family:monospace;">g <span style="color: #080;">&lt;-</span> g <span style="color: #080;">+</span> ylab<span style="color: #080;">&#40;</span>label<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Wert&quot;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> 
  <a href="http://google.com/search?btnI=1&q=opts&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">opts</span></a><span style="color: #080;">&#40;</span>legend.<span style="">position</span><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;bottom&quot;</span>, <a href="http://stat.ethz.ch/R-manual/R-patched/library/graphics/html/title.html"><span style="color: #0000FF; font-weight: bold;">title</span></a><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Bodenproben&quot;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> 
  <a href="http://google.com/search?btnI=1&q=guides&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">guides</span></a><span style="color: #080;">&#40;</span>shape <span style="color: #080;">=</span> <a href="http://google.com/search?btnI=1&q=guide_legend&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">guide_legend</span></a><span style="color: #080;">&#40;</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/ncol.html"><span style="color: #0000FF; font-weight: bold;">ncol</span></a> <span style="color: #080;">=</span> <span style="color: #ff0000;">2</span>,title.<span style="">vjust</span><span style="color: #080;">=</span><span style="color: #ff0000;">0.2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
  <a href="http://google.com/search?btnI=1&q=scale_shape_manual&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">scale_shape_manual</span></a><span style="color: #080;">&#40;</span>values<span style="color: #080;">=</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">21</span>,<span style="color: #ff0000;">22</span>,<span style="color: #ff0000;">23</span>,<span style="color: #ff0000;">24</span>,<span style="color: #ff0000;">25</span>,<span style="color: #ff0000;">3</span>,<span style="color: #ff0000;">8</span>,<span style="color: #ff0000;">4</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> 
  <a href="http://google.com/search?btnI=1&q=opts&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">opts</span></a><span style="color: #080;">&#40;</span>legend.<span style="">title</span><span style="color: #080;">=</span><a href="http://google.com/search?btnI=1&q=theme_text&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">theme_text</span></a><span style="color: #080;">&#40;</span>angle<span style="color: #080;">=</span><span style="color: #ff0000;">90</span>, face<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;bold&quot;</span>, size<span style="color: #080;">=</span><span style="color: #ff0000;">11</span><span style="color: #080;">&#41;</span>,
       plot.<span style="">title</span><span style="color: #080;">=</span><a href="http://google.com/search?btnI=1&q=theme_text&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">theme_text</span></a><span style="color: #080;">&#40;</span>face<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;bold&quot;</span>, size<span style="color: #080;">=</span><span style="color: #ff0000;">20</span>,vjust<span style="color: #080;">=</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
       <a href="http://google.com/search?btnI=1&q=scale_x_continuous&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">scale_x_continuous</span></a><span style="color: #080;">&#40;</span>minor_breaks <span style="color: #080;">=</span> <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/function.html"><span style="color: #0000FF; font-weight: bold;">function</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/ceiling.html"><span style="color: #0000FF; font-weight: bold;">ceiling</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">:</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/floor.html"><span style="color: #0000FF; font-weight: bold;">floor</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#125;</span>,
                          breaks <span style="color: #080;">=</span> <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/function.html"><span style="color: #0000FF; font-weight: bold;">function</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/seq.html"><span style="color: #0000FF; font-weight: bold;">seq</span></a><span style="color: #080;">&#40;</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/ceiling.html"><span style="color: #0000FF; font-weight: bold;">ceiling</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/floor.html"><span style="color: #0000FF; font-weight: bold;">floor</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/by.html"><span style="color: #0000FF; font-weight: bold;">by</span></a><span style="color: #080;">=</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>
&nbsp;
g</pre></td></tr></table></div>

<p><img src="http://berlani.de/wp-content/uploads/2012/07/bodenproben_sw.png" alt="" title="bodenproben_sw" width="600" height="700" class="alignnone size-full wp-image-394" /></p>
<h3>Farben und Schwarzweißdrucker</h3>
<p>Ein farbiger Plot kann auch durchaus kopierbar sein. Leider ist das standard ggplot2-Farbschema weder besonders SW-Drucker-freundlich (alle Farben haben die gleiche Helligkeit) noch besonders gut lesbar für Farbenblinde. Wer sich für das Thema interessiert findet <a href="http://wiki.stdout.org/rcookbook/Graphs/Colors%20(ggplot2)/" title="Farben für ggplot2 und Farbenblinde" target="_blank">hier</a> eine gute Informationsgrundlage. Es spricht häufig sicherlich nichts dagegen, sowohl Farben als auch Symbole zu verwenden, indem man in den Aesthetics beides festlegt (<code>aes(shape=xy, color=xy)</code>).</p>
]]></content:encoded>
			<wfw:commentRss>http://berlani.de/2012/07/r-statistik/r/ggplot2-tipps-fur-grafiken-in-schwarz-weis/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ersetzen in regelmäßigen Abständen, bzw. nach einem Schema</title>
		<link>http://berlani.de/2012/07/r-statistik/r/ersetzen-in-regelmasigen-abstanden-bzw-nach-einem-schema/</link>
		<comments>http://berlani.de/2012/07/r-statistik/r/ersetzen-in-regelmasigen-abstanden-bzw-nach-einem-schema/#comments</comments>
		<pubDate>Wed, 25 Jul 2012 08:41:51 +0000</pubDate>
		<dc:creator>Jakob</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[r replace nützlich]]></category>

		<guid isPermaLink="false">http://berlani.de/?p=386</guid>
		<description><![CDATA[Vielleicht hat dieses Codebeispiel keine große Praxisrelevanz, aber dennoch stolpert man vielleicht mal über die Aufgabe. Hier also ein fiktives Beispiel: Es liegt eine sehr einfache Reihe an Messdaten vor, von denen wir wissen, dass sie nur Werktags erhoben wurden. In Wochentagen sähe es also so aus: MO DI MI DO FR MO DI MI [...]]]></description>
			<content:encoded><![CDATA[<p>Vielleicht hat dieses Codebeispiel keine große Praxisrelevanz, aber dennoch stolpert man vielleicht mal über die Aufgabe. Hier also ein fiktives Beispiel:<br />
Es liegt eine sehr einfache Reihe an Messdaten vor, von denen wir wissen, dass sie nur Werktags erhoben wurden. In Wochentagen sähe es also so aus:<br />
<code><br />
MO DI MI DO FR MO DI MI DO FR MO DI..<br />
</code><br />
Nach 5 Messungen ist also eine Lücke von 2 Tagen, die leider nicht im Datensatz selbst ist, die wir aber irgendwie füllen wollen &#8211; vielleicht mit Daten aus den Wochenendsmessungen.<br />
Ein Ersetz-Schema sieht also wie folgt aus, wobei T angibt, dass wir die Daten aus dem Datensatz nehmen wollen, ein F gibt an, dass der alternative Datensatz zu nutzen ist.<br />
<code><br />
T T T T T F F T T T T T F F T T<br />
</code><br />
Am einfachsten nachzuvollziehen geht es in einer While-Schleife. Hier bereits in einer sehr allgemein nutzbaren Funktion. Es gibt einen Vektor mit den Ursprungsdaten und einen mit den Lückenfüllern und natürlich das Schema, wonach der neue Vektor entstehen soll.</p>
<p><span id="more-386"></span></p>

<div class="wp_codebox"><table><tr id="p3868"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
</pre></td><td class="code" id="p386code8"><pre class="rsplus" style="font-family:monospace;">stretchjoin <span style="color: #080;">&lt;-</span> <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/function.html"><span style="color: #0000FF; font-weight: bold;">function</span></a> <span style="color: #080;">&#40;</span>dat, <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/rep.html"><span style="color: #0000FF; font-weight: bold;">rep</span></a>, schema, recycle<span style="color: #080;">=</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/T.html"><span style="color: #0000FF; font-weight: bold;">T</span></a><span style="color: #080;">&#41;</span> <span style="color: #080;">&#123;</span>
  i <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">1</span> <span style="color: #228B22;">#position in dat</span>
  j <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">1</span> <span style="color: #228B22;">#position in </span>
  s <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">1</span> <span style="color: #228B22;">#position in schema</span>
  f <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">1</span> <span style="color: #228B22;">#position in result</span>
  res <span style="color: #080;">&lt;-</span> NULL
  fin <span style="color: #080;">&lt;-</span> FALSE
  <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/while.html"><span style="color: #0000FF; font-weight: bold;">while</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">!</span>fin<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
    <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/if.html"><span style="color: #0000FF; font-weight: bold;">if</span></a><span style="color: #080;">&#40;</span>schema<span style="color: #080;">&#91;</span>s<span style="color: #080;">&#93;</span><span style="color: #080;">==</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/T.html"><span style="color: #0000FF; font-weight: bold;">T</span></a><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
      res<span style="color: #080;">&#91;</span>f<span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> dat<span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span>
      i <span style="color: #080;">&lt;-</span> i<span style="color: #080;">+</span><span style="color: #ff0000;">1</span> <span style="color: #228B22;">#go to next item in dat</span>
    <span style="color: #080;">&#125;</span><span style="color: #0000FF; font-weight: bold;">else</span><span style="color: #080;">&#123;</span>
      res<span style="color: #080;">&#91;</span>f<span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/rep.html"><span style="color: #0000FF; font-weight: bold;">rep</span></a><span style="color: #080;">&#91;</span>j<span style="color: #080;">&#93;</span>
      j <span style="color: #080;">&lt;-</span> j<span style="color: #080;">+</span><span style="color: #ff0000;">1</span> <span style="color: #228B22;">#go to next item in rep</span>
    <span style="color: #080;">&#125;</span>
    s <span style="color: #080;">&lt;-</span> s<span style="color: #080;">+</span><span style="color: #ff0000;">1</span> <span style="color: #228B22;">#go to next item in schema</span>
    f <span style="color: #080;">&lt;-</span> f<span style="color: #080;">+</span><span style="color: #ff0000;">1</span> <span style="color: #228B22;">#go to next item in result</span>
    <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/if.html"><span style="color: #0000FF; font-weight: bold;">if</span></a><span style="color: #080;">&#40;</span>s<span style="color: #080;">&gt;</span>length<span style="color: #080;">&#40;</span>schema<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
      <span style="color: #228B22;">#if reached end of schema, start over </span>
      s <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">1</span>
    <span style="color: #080;">&#125;</span>
    <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/if.html"><span style="color: #0000FF; font-weight: bold;">if</span></a><span style="color: #080;">&#40;</span>j<span style="color: #080;">&gt;</span>length<span style="color: #080;">&#40;</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/rep.html"><span style="color: #0000FF; font-weight: bold;">rep</span></a><span style="color: #080;">&#41;</span> <span style="color: #080;">&amp;</span> recycle<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
      j <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">1</span> <span style="color: #228B22;">#if reached end of replacement vector and recycling allowed, start over</span>
    <span style="color: #080;">&#125;</span><span style="color: #0000FF; font-weight: bold;">else</span> <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/if.html"><span style="color: #0000FF; font-weight: bold;">if</span></a><span style="color: #080;">&#40;</span>schema<span style="color: #080;">&#91;</span>s<span style="color: #080;">&#93;</span><span style="color: #080;">==</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/F.html"><span style="color: #0000FF; font-weight: bold;">F</span></a> <span style="color: #080;">&amp;</span> j<span style="color: #080;">&gt;</span>length<span style="color: #080;">&#40;</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/rep.html"><span style="color: #0000FF; font-weight: bold;">rep</span></a><span style="color: #080;">&#41;</span> <span style="color: #080;">&amp;</span> <span style="color: #080;">!</span>recycle<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
      <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/warning.html"><span style="color: #0000FF; font-weight: bold;">warning</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Reached end of repment vector before end of dat. Set recycle=T!&quot;</span><span style="color: #080;">&#41;</span>
      fin <span style="color: #080;">&lt;-</span> <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/T.html"><span style="color: #0000FF; font-weight: bold;">T</span></a>
    <span style="color: #080;">&#125;</span>
    <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/if.html"><span style="color: #0000FF; font-weight: bold;">if</span></a><span style="color: #080;">&#40;</span>i<span style="color: #080;">&gt;</span>length<span style="color: #080;">&#40;</span>dat<span style="color: #080;">&#41;</span> <span style="color: #080;">&amp;</span> schema<span style="color: #080;">&#91;</span>s<span style="color: #080;">&#93;</span><span style="color: #080;">==</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/T.html"><span style="color: #0000FF; font-weight: bold;">T</span></a><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
      <span style="color: #228B22;">#fin if reached end in dat an next item has to be from dat according to the schema</span>
      fin <span style="color: #080;">&lt;-</span> <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/T.html"><span style="color: #0000FF; font-weight: bold;">T</span></a>
    <span style="color: #080;">&#125;</span>
  <span style="color: #080;">&#125;</span>
  <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/return.html"><span style="color: #0000FF; font-weight: bold;">return</span></a><span style="color: #080;">&#40;</span>res<span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span>
&nbsp;
<span style="color: #228B22;">#</span>
<span style="color: #228B22;"># Examples</span>
<span style="color: #228B22;">#</span>
&nbsp;
x <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #ff0000;">12</span>
y <span style="color: #080;">&lt;-</span> NA
s <span style="color: #080;">&lt;-</span> <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/T.html"><span style="color: #0000FF; font-weight: bold;">T</span></a>,<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/T.html"><span style="color: #0000FF; font-weight: bold;">T</span></a>,<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/T.html"><span style="color: #0000FF; font-weight: bold;">T</span></a>,<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/T.html"><span style="color: #0000FF; font-weight: bold;">T</span></a>,<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/T.html"><span style="color: #0000FF; font-weight: bold;">T</span></a>,<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/F.html"><span style="color: #0000FF; font-weight: bold;">F</span></a>,<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/F.html"><span style="color: #0000FF; font-weight: bold;">F</span></a><span style="color: #080;">&#41;</span>
stretchjoin<span style="color: #080;">&#40;</span>x,y,s,<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/T.html"><span style="color: #0000FF; font-weight: bold;">T</span></a><span style="color: #080;">&#41;</span>
<span style="color: #228B22;">#[1]  1  2  3  4  5 NA NA  6  7  8  9 10 NA NA 11 12</span>
&nbsp;
x <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #ff0000;">15</span>
y <span style="color: #080;">&lt;-</span> <span style="color: #080;">-</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #ff0000;">4</span><span style="color: #080;">&#41;</span>
stretchjoin<span style="color: #080;">&#40;</span>x,y,s,<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/T.html"><span style="color: #0000FF; font-weight: bold;">T</span></a><span style="color: #080;">&#41;</span>
<span style="color: #228B22;">#[1]  1  2  3  4  5 -1 -2  6  7  8  9 10 -3 -4 11 12 13 14 15 -1 -2</span></pre></td></tr></table></div>

<p>Mit den Ergebnissen als Indexvektor kann man dann auch einfach eine neue Matrix erzeugen. Naja ok. Ein wenig tricksen muss man schon:</p>
<pre code="rsplus" colla="+" line="45">
mat1 <- matrix(runif(20), ncol=2)
mat2 <- matrix(-(1:2),ncol=2, nrow=2)
mat_neu <- rbind(mat1,mat2)
i1 <- stretchjoin(1:nrow(mat1), rep=nrow(mat1)+(1:nrow(mat2)) ,schema=c(T,T,T,F,F),recycle=T)
mat_neu <- mat_neu[i1,]
mat_neu
#        [,1]        [,2]
#  [1,]  0.39263009  0.53411873
#  [2,]  0.72839589  0.52903701
#  [3,]  0.82255067  0.01965311
#  [4,] -1.00000000 -1.00000000
#  [5,] -2.00000000 -2.00000000
#  [6,]  0.05864827  0.18895893
#  [7,]  0.38650819  0.23657343
#  [8,]  0.73526049  0.45979191
#  [9,] -1.00000000 -1.00000000
# [10,] -2.00000000 -2.00000000
# [11,]  0.46804821  0.93976568
# [12,]  0.20527319  0.31220657
# [13,]  0.25728071  0.84647837
# [14,] -1.00000000 -1.00000000
# [15,] -2.00000000 -2.00000000
# [16,]  0.33444153  0.93647087
</pre>
<p>Ich bin mir sicher, dass es dafür noch eine schönere Lösung gibt. Vor allem ist diese Lösung bei sehr großen Datensätzen vielleicht nicht die schnellste. Klüger ist es sicherlich direkt einen guten Indexvektor zu erzeugen und nicht mit den Daten zu hantieren. Eine solche Variante wird folgen.</p>
]]></content:encoded>
			<wfw:commentRss>http://berlani.de/2012/07/r-statistik/r/ersetzen-in-regelmasigen-abstanden-bzw-nach-einem-schema/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tipp: nicht-destruktives Arbeiten mit par()</title>
		<link>http://berlani.de/2012/07/r-statistik/r/tipp-nicht-destruktives-arbeiten-mit-par/</link>
		<comments>http://berlani.de/2012/07/r-statistik/r/tipp-nicht-destruktives-arbeiten-mit-par/#comments</comments>
		<pubDate>Thu, 05 Jul 2012 13:07:07 +0000</pubDate>
		<dc:creator>Jakob</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[par]]></category>
		<category><![CDATA[r]]></category>
		<category><![CDATA[tip]]></category>
		<category><![CDATA[tipps]]></category>

		<guid isPermaLink="false">http://berlani.de/?p=380</guid>
		<description><![CDATA[Ein kleiner Tipp, wenn man mal doch mit der in R eingebauten Grafikumgebung arbeiten will und nicht mit ggplot. Hat man mal ein längeres R-Skript und erzeugt darin verschiedene Grafiken, kommt es häufig vor, dass man für einige bestimmte Einstellungen in par() ändern möchte. Insbesondere vermutlich mfrow=c(2,3) um mehrere Plots neben und untereinander in einer [...]]]></description>
			<content:encoded><![CDATA[<p>Ein kleiner Tipp, wenn man mal doch mit der in R eingebauten Grafikumgebung arbeiten will und nicht mit <a href="http://berlani.de/tag/ggplot2/" title="ggplot Artikel" target="_blank">ggplot</a>.<br />
Hat man mal ein längeres R-Skript und erzeugt darin verschiedene Grafiken, kommt es häufig vor, dass man für einige bestimmte Einstellungen in <code>par()</code> ändern möchte. Insbesondere vermutlich <code>mfrow=c(2,3)</code> um mehrere Plots neben und untereinander in einer Grafik zusammenzufügen. Wenn dann die nächsten Grafiken wieder einzeln geplotted werden sollen, muss man wieder mühselig die Parameter von <code>par()</code> zurück setzen.<br />
Am elegantesten geht es wohl so:</p>

<div class="wp_codebox"><table><tr id="p38010"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p380code10"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;"># ...</span>
<span style="color: #228B22;"># irgendwelcher Code</span>
op <span style="color: #080;">&lt;-</span> <a href="http://stat.ethz.ch/R-manual/R-patched/library/graphics/html/par.html"><span style="color: #0000FF; font-weight: bold;">par</span></a><span style="color: #080;">&#40;</span>mfrow<span style="color: #080;">=</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span>,<span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #228B22;">#Einstellen der Grafikparameter</span>
  <span style="color: #228B22;">#2 Plots nebeneinander</span>
  <a href="http://stat.ethz.ch/R-manual/R-patched/library/graphics/html/plot.html"><span style="color: #0000FF; font-weight: bold;">plot</span></a><span style="color: #080;">&#40;</span><a href="http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=stats:runif"><span style="color: #0000FF; font-weight: bold;">runif</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">100</span><span style="color: #080;">&#41;</span>,<a href="http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=stats:runif"><span style="color: #0000FF; font-weight: bold;">runif</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">100</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
  <a href="http://stat.ethz.ch/R-manual/R-patched/library/graphics/html/plot.html"><span style="color: #0000FF; font-weight: bold;">plot</span></a><span style="color: #080;">&#40;</span><a href="http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=stats:runif"><span style="color: #0000FF; font-weight: bold;">runif</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">100</span><span style="color: #080;">&#41;</span>,<a href="http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=stats:runif"><span style="color: #0000FF; font-weight: bold;">runif</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">100</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<a href="http://stat.ethz.ch/R-manual/R-patched/library/graphics/html/par.html"><span style="color: #0000FF; font-weight: bold;">par</span></a><span style="color: #080;">&#40;</span>op<span style="color: #080;">&#41;</span> <span style="color: #228B22;">#Alles auf den Zustand davor zurücksetzen</span>
<a href="http://stat.ethz.ch/R-manual/R-patched/library/graphics/html/plot.html"><span style="color: #0000FF; font-weight: bold;">plot</span></a><span style="color: #080;">&#40;</span><a href="http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=stats:runif"><span style="color: #0000FF; font-weight: bold;">runif</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">100</span><span style="color: #080;">&#41;</span>,<a href="http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=stats:runif"><span style="color: #0000FF; font-weight: bold;">runif</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">100</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #228B22;">#1 Plot alleine</span>
<span style="color: #228B22;">#...</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://berlani.de/2012/07/r-statistik/r/tipp-nicht-destruktives-arbeiten-mit-par/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ggplot2 &#8211; ein umfangreiches Beispiel</title>
		<link>http://berlani.de/2012/06/r-statistik/r/ggplot2-ein-umfangreiches-beispiel/</link>
		<comments>http://berlani.de/2012/06/r-statistik/r/ggplot2-ein-umfangreiches-beispiel/#comments</comments>
		<pubDate>Thu, 21 Jun 2012 22:30:43 +0000</pubDate>
		<dc:creator>Jakob</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[datensatz]]></category>
		<category><![CDATA[finetuning]]></category>
		<category><![CDATA[ggplot]]></category>
		<category><![CDATA[ggplot2]]></category>
		<category><![CDATA[opendata]]></category>

		<guid isPermaLink="false">http://berlani.de/?p=310</guid>
		<description><![CDATA[Das Einführungsbeispiel war ja in Punkto Datenumfang sehr bescheiden. Seine stärke spielt ggplot2 vor allem bei großen Datensätzen mit vielen Merkmalen aus. Insbesondere, wenn man noch nicht weiß wo es hingehen soll. Einen Datensatz entdecken Aus der Umweltprobenbank habe ich mir mal diesen Datensatz zusammengestellt. Ich weiß auch nur, dass es um die Bodenbelastungen an [...]]]></description>
			<content:encoded><![CDATA[<p>Das <a href="http://jakob-r.de/2012/06/r-statistik/r/ggplot-eine-einfuhrung/" title="ggplot – eine Einführung">Einführungsbeispiel</a> war ja in Punkto Datenumfang sehr bescheiden. Seine stärke spielt ggplot2 vor allem bei großen Datensätzen mit vielen Merkmalen aus. Insbesondere, wenn man noch nicht weiß wo es hingehen soll.</p>
<h3>Einen Datensatz entdecken</h3>
<p>Aus der <a href="http://www.umweltprobenbank.de/de/documents/investigations/analytes" target="_blank">Umweltprobenbank</a> habe ich mir mal <a href="http://www.umweltprobenbank.de/de/documents/investigations/results/analytes?analytes=10007&#038;sampling_areas=10093+10092+10088+10055+10044+10040+10016+10008+10006&#038;sampling_years=&#038;specimen_types=10003" title="Beispieldatensatz aus der Umweltprobendatenbank" target="_blank">diesen Datensatz</a> zusammengestellt. Ich weiß auch nur, dass es um die Bodenbelastungen an verschiedenen Orten in Deutschland geht. Einen tieferen Einblick in die Daten gewinnt man mit bloßen Augen nicht so recht.<br />
<div id="attachment_318" class="wp-caption alignnone" style="width: 620px"><a href="http://berlani.de/wp-content/uploads/2012/06/Unbenannt.png" rel="lightbox[310]" title="Excel-Datensatz"><img src="http://berlani.de/wp-content/uploads/2012/06/Unbenannt-610x445.png" alt="" title="Excel-Datensatz" width="610" height="445" class="size-medium wp-image-318" /></a><p class="wp-caption-text">Ein kleiner Einblick mit Excel in den Datensatz</p></div><br />
<span id="more-310"></span><br />
Lesen wir diese Datei nun mit R ein.</p>

<div class="wp_codebox"><table><tr id="p31019"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p310code19"><pre class="rsplus" style="font-family:monospace;">dat <span style="color: #080;">&lt;-</span> <a href="http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=utils:read.table"><span style="color: #0000FF; font-weight: bold;">read.<span style="">table</span></span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;upb_chemical_pollution_data.csv&quot;</span>, <a href="http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=utils:head"><span style="color: #0000FF; font-weight: bold;">head</span></a><span style="color: #080;">=</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/T.html"><span style="color: #0000FF; font-weight: bold;">T</span></a>, sep<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span>, dec<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;,&quot;</span><span style="color: #080;">&#41;</span>
dat <span style="color: #080;">&lt;-</span> dat<span style="color: #080;">&#91;</span>dat$Statistischer.<span style="">Parameter</span><span style="color: #080;">==</span><span style="color: #ff0000;">&quot;Arithmetischer Mittelwert&quot;</span>,<span style="color: #080;">&#93;</span> <span style="color: #228B22;">#Wir interessiern uns nur für die Mittelwerte</span>
<a href="http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=utils:head"><span style="color: #0000FF; font-weight: bold;">head</span></a><span style="color: #080;">&#40;</span>dat<span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>Ein Blick auf den Anfang des Datensatzes verrät, dass R die Zahlen der Spaltennamen um ein vorangestelltes X erweitert hat. Das muss später noch bereinigt werden. Außerdem interessieren wir uns nur für die Zeilen, die den Arithmetischen Mittelwert der Messungen beinhalten (Zeile 2).</p>
<h3>Daten umformen mit <code>melt()</code></h3>
<p>Wichtig für ggplot (wir erinnern uns) ist, dass jeder Punkt des Plots durch eine Zeile im Datensatz repräsentiert wird. Natürlich soll jedes Jahr ein eigenen Datenpunkt darstellen. Dazu muss für jede Zeile des Datensatzes der Bereich mit den Jahreszahlen transponiert werden, sodass die Jahreszahlen dann eine Spalte bilden. Die Werte aus der ursprünglichen Zeile gelten für alle Jahre, also werden sie in die neu entstandenen Zeilen kopiert. Das klingt kompliziert, aber mit der Funktion <code>melt()</code> aus dem Paket <code>reshape()</code> (Hinweis: In älteren Versionen von ggplot2 war <code>melt()</code> noch enthalten) geht es im nu.</p>

<div class="wp_codebox"><table><tr id="p31020"><td class="line_numbers"><pre>5
6
7
8
</pre></td><td class="code" id="p310code20"><pre class="rsplus" style="font-family:monospace;">mdat <span style="color: #080;">&lt;-</span> melt<span style="color: #080;">&#40;</span>dat, measure.<span style="">vars</span><span style="color: #080;">=</span><span style="color: #ff0000;">8</span><span style="color: #080;">:</span><span style="color: #ff0000;">24</span>, 
                  id.<span style="">vars</span><span style="color: #080;">=</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Probenart&quot;</span>,<span style="color: #ff0000;">&quot;Analyt&quot;</span>,<span style="color: #ff0000;">&quot;Messeinheit&quot;</span><span style="color: #080;">&#41;</span>, 
                  variable_name<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Jahr&quot;</span>, na.<span style="">rm</span><span style="color: #080;">=</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/T.html"><span style="color: #0000FF; font-weight: bold;">T</span></a><span style="color: #080;">&#41;</span>
<a href="http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=utils:head"><span style="color: #0000FF; font-weight: bold;">head</span></a><span style="color: #080;">&#40;</span>mdat<span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>In den Spalten 8 bis 24 stehen die Jahre mit ihren Messwerten (<code>measure.vars=8:24</code>. Das sind also die Spalten die in Zeilen transponiert werden sollen. Der Name für die so neu entstandene Spalte ist mit <code>variable_name="Jahr"</code> festgelegt. Die jahresunabhänigen Merkmale die uns interessieren sind gegeben durch <code>id.vars=c("Probenart","Analyt","Messeinheit")</code>. Ist ein Messwert nicht vorhanden, so soll keine Zeile enstehen (<code>na.rm=T</code>).</p>
<blockquote><p>
Hinweis zu <code>melt()</code>: In der Regel genügt die Angabe von <code>id.vars=</code> oder <code>measure.vars=</code>. Es werden dann jeweils die übrigen anderen Spalten als <code>measure.vars</code> bzw. <code>id.vars</code> verwendet. Angaben können sowohl über Spaltenindizes als auch über Spaltennamen erfolgen.
</p></blockquote>
<p>Nun noch schnell das X vor der Jahreszahl entfernen:</p>

<div class="wp_codebox"><table><tr id="p31021"><td class="line_numbers"><pre>9
</pre></td><td class="code" id="p310code21"><pre class="rsplus" style="font-family:monospace;">mdat$Jahr <span style="color: #080;">&lt;-</span> <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/as.numeric.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">numeric</span></span></a><span style="color: #080;">&#40;</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/substr.html"><span style="color: #0000FF; font-weight: bold;">substr</span></a><span style="color: #080;">&#40;</span>mdat$Jahr,<a href="http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=stats:start"><span style="color: #0000FF; font-weight: bold;">start</span></a><span style="color: #080;">=</span><span style="color: #ff0000;">2</span>,<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/stop.html"><span style="color: #0000FF; font-weight: bold;">stop</span></a><span style="color: #080;">=</span><span style="color: #ff0000;">5</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #228B22;">#nur Zeichen 2 bis 5 werden genommen</span></pre></td></tr></table></div>

<p>Nun lassen wir die Magie passieren:</p>

<div class="wp_codebox"><table><tr id="p31022"><td class="line_numbers"><pre>11
12
</pre></td><td class="code" id="p310code22"><pre class="rsplus" style="font-family:monospace;">g <span style="color: #080;">&lt;-</span> <a href="http://google.com/search?btnI=1&q=ggplot&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">ggplot</span></a><span style="color: #080;">&#40;</span>mdat,<a href="http://google.com/search?btnI=1&q=aes&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">aes</span></a><span style="color: #080;">&#40;</span>y<span style="color: #080;">=</span>value,x<span style="color: #080;">=</span>Jahr<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
g  <span style="color: #080;">+</span> <a href="http://google.com/search?btnI=1&q=geom_point&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">geom_point</span></a><span style="color: #080;">&#40;</span><a href="http://google.com/search?btnI=1&q=aes&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">aes</span></a><span style="color: #080;">&#40;</span>color<span style="color: #080;">=</span>Probenart<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p><img src="http://berlani.de/wp-content/uploads/2012/06/bodenproben1.png" alt="" title="Plot 1" width="610" height="700" class="alignnone size-full wp-image-312" /><br />
Gut &#8211; das war sehr basic. Bereits hier erkennt man, dass in Regenwürmern der Wert der Schadstoffkonzentration sehr hoch ist. Sie scheinen sich also gut zu eignen um Schadstoffe im Boden ausfindig zu machen. Interessant! Also das Leute das überhaut machen.<br />
Jetzt stellt sich die Frage: Lässt sich denn ein Trend erkennen? Wie wäre es mit einer polynomiellen Regression?</p>

<div class="wp_codebox"><table><tr id="p31023"><td class="line_numbers"><pre>14
15
</pre></td><td class="code" id="p310code23"><pre class="rsplus" style="font-family:monospace;">g <span style="color: #080;">+</span> <a href="http://google.com/search?btnI=1&q=geom_point&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">geom_point</span></a><span style="color: #080;">&#40;</span><a href="http://google.com/search?btnI=1&q=aes&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">aes</span></a><span style="color: #080;">&#40;</span>color<span style="color: #080;">=</span>Probenart<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> 
    <a href="http://google.com/search?btnI=1&q=stat_smooth&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">stat_smooth</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p><img src="http://berlani.de/wp-content/uploads/2012/06/bodenproben2.png" alt="Polynomielle Regression" title="Plot 2" width="610" height="700" class="alignnone size-full wp-image-313" /><br />
Hat sich der Schadstoffanteil im Boden also seit 1995 verbessert? Das ist alles noch ein wenig undifferenziert. Wir sollten die Regression nach den verschiedenen Schadstoffen unterteilen.</p>

<div class="wp_codebox"><table><tr id="p31024"><td class="line_numbers"><pre>17
18
</pre></td><td class="code" id="p310code24"><pre class="rsplus" style="font-family:monospace;">g <span style="color: #080;">+</span> <a href="http://google.com/search?btnI=1&q=geom_point&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">geom_point</span></a><span style="color: #080;">&#40;</span><a href="http://google.com/search?btnI=1&q=aes&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">aes</span></a><span style="color: #080;">&#40;</span>color<span style="color: #080;">=</span>Probenart<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
    <a href="http://google.com/search?btnI=1&q=stat_smooth&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">stat_smooth</span></a><span style="color: #080;">&#40;</span><a href="http://google.com/search?btnI=1&q=aes&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">aes</span></a><span style="color: #080;">&#40;</span>group<span style="color: #080;">=</span>Analyt, fill<span style="color: #080;">=</span>Analyt<span style="color: #080;">&#41;</span>,color<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;black&quot;</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p><img src="http://berlani.de/wp-content/uploads/2012/06/bodenproben3.png" alt="Polynomielle Regression nach Schadstoffen unterteilt" title="Plot 3" width="610" height="700" class="alignnone size-full wp-image-314" /><br />
Ok &#8211; offensichtlich wurde der Regenwurm genutzt um den Phosphorgehalt in Boden zu untersuchen. Schwefel wurde wohl im Eiinhalt einer Stadttaube untersucht. Das ist aber sehr Vage formuliert. Wie wäre es denn mit verschiedenen Plots für die unterschiedlichen Stoffe? Das geht mit Hilve von <code>facet_wrap()</code>. Durch die Einteilungen kann auch die Gruppierung bei <code>stat_smooth()</code> weglassen werden. Da die Schadstoffkonzentrationen unterschiedlich gelegen sind darf bei jedem Abschnitt die y-Achse frei sein (<code>scales="free_y"</code>). Mit <code>ncol=1</code> wird bewirkt, dass die Plots untereinander erscheinen. Darum habe ich auch nicht <code>scales="free"</code> gewählt, denn so bleiben die Jahre richtig untereinander.</p>

<div class="wp_codebox"><table><tr id="p31025"><td class="line_numbers"><pre>20
21
22
23
</pre></td><td class="code" id="p310code25"><pre class="rsplus" style="font-family:monospace;">g <span style="color: #080;">&lt;-</span> g <span style="color: #080;">+</span> <a href="http://google.com/search?btnI=1&q=geom_point&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">geom_point</span></a><span style="color: #080;">&#40;</span><a href="http://google.com/search?btnI=1&q=aes&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">aes</span></a><span style="color: #080;">&#40;</span>color<span style="color: #080;">=</span>Probenart<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
	 <a href="http://google.com/search?btnI=1&q=stat_smooth&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">stat_smooth</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
         <a href="http://google.com/search?btnI=1&q=facet_wrap&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">facet_wrap</span></a><span style="color: #080;">&#40;</span>~Analyt<span style="color: #080;">+</span>Messeinheit, scales<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;free_y&quot;</span>, <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/ncol.html"><span style="color: #0000FF; font-weight: bold;">ncol</span></a><span style="color: #080;">=</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>
g</pre></td></tr></table></div>

<p><img src="http://berlani.de/wp-content/uploads/2012/06/bodenproben4.png" alt="Grafik unterteilt nach Schadstoffen" title="Plot 4" width="610" height="700" class="alignnone size-full wp-image-315" /><br />
Das ist doch schon viel übersichtlicher. Da ich mit den <i>Geoms</i> in diesem Plot schon recht zufrieden war hab ich alles mal fest im ggplot-Objekt gespeichert. Denn nun widmen wir uns dem Layout.</p>
<h3>Layout eines ggplot-Plots verändern</h3>
<p>Jetzt mache ich es mal kurz: Das Ergebnis, der Code und danach die Erläuterung:<br />
<img src="http://berlani.de/wp-content/uploads/2012/06/bodenproben5.png" alt="ggplot mit verbessertem Layout" title="Plot 5" width="610" height="700" class="alignnone size-full wp-image-316" /></p>

<div class="wp_codebox"><table><tr id="p31026"><td class="line_numbers"><pre>25
26
27
28
29
30
31
</pre></td><td class="code" id="p310code26"><pre class="rsplus" style="font-family:monospace;">g <span style="color: #080;">+</span> ylab<span style="color: #080;">&#40;</span>label<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Wert&quot;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> 
	  <a href="http://google.com/search?btnI=1&q=opts&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">opts</span></a><span style="color: #080;">&#40;</span>legend.<span style="">position</span><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;bottom&quot;</span>, <a href="http://stat.ethz.ch/R-manual/R-patched/library/graphics/html/title.html"><span style="color: #0000FF; font-weight: bold;">title</span></a><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Bodenproben&quot;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> 
	  <a href="http://google.com/search?btnI=1&q=guides&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">guides</span></a><span style="color: #080;">&#40;</span>color <span style="color: #080;">=</span> <a href="http://google.com/search?btnI=1&q=guide_legend&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">guide_legend</span></a><span style="color: #080;">&#40;</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/ncol.html"><span style="color: #0000FF; font-weight: bold;">ncol</span></a> <span style="color: #080;">=</span> <span style="color: #ff0000;">2</span>,title.<span style="">vjust</span><span style="color: #080;">=</span><span style="color: #ff0000;">0.2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
	  <a href="http://google.com/search?btnI=1&q=opts&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">opts</span></a><span style="color: #080;">&#40;</span>legend.<span style="">title</span><span style="color: #080;">=</span><a href="http://google.com/search?btnI=1&q=theme_text&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">theme_text</span></a><span style="color: #080;">&#40;</span>angle<span style="color: #080;">=</span><span style="color: #ff0000;">90</span>, face<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;bold&quot;</span>, size<span style="color: #080;">=</span><span style="color: #ff0000;">11</span><span style="color: #080;">&#41;</span>,
		plot.<span style="">title</span><span style="color: #080;">=</span><a href="http://google.com/search?btnI=1&q=theme_text&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">theme_text</span></a><span style="color: #080;">&#40;</span>face<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;bold&quot;</span>, size<span style="color: #080;">=</span><span style="color: #ff0000;">20</span>,vjust<span style="color: #080;">=</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
	  <a href="http://google.com/search?btnI=1&q=scale_x_continuous&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">scale_x_continuous</span></a><span style="color: #080;">&#40;</span>minor_breaks <span style="color: #080;">=</span> <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/function.html"><span style="color: #0000FF; font-weight: bold;">function</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/ceiling.html"><span style="color: #0000FF; font-weight: bold;">ceiling</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">:</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/floor.html"><span style="color: #0000FF; font-weight: bold;">floor</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#125;</span>,
	                     breaks <span style="color: #080;">=</span> <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/function.html"><span style="color: #0000FF; font-weight: bold;">function</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/seq.html"><span style="color: #0000FF; font-weight: bold;">seq</span></a><span style="color: #080;">&#40;</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/ceiling.html"><span style="color: #0000FF; font-weight: bold;">ceiling</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/floor.html"><span style="color: #0000FF; font-weight: bold;">floor</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/by.html"><span style="color: #0000FF; font-weight: bold;">by</span></a><span style="color: #080;">=</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<ul>
<li><code>25:</code> Der Name der y-Achse wird geändert und es wird eine Überschrift hinzugefügt.</li>
<li><code>26:</code> Wir positionieren die Legende nach unten uns geben ihr einen neuen Titel.</li>
<li><code>27:</code> Die <em>Gestaltungsführung</em> <code>guides()</code> der Farblegende (<code>color=guide_legend()</code>) wird auf 2 Spalten (<code>ncol = 2</code>) geändert und der Titel ein wenig nach oben verschoben (<code>title.vjust=0.2</code>).</li>
<li><code>28:</code> In den ggplot-Optionen (<code>opts()</code>) wird der Titel der Legende um 90° gedreht, auf Schriftgröße 11 und dick gedruckt gesetzt.</li>
<li><code>29:</code> Des weiteren passen wir auch die Schriftart der Überschrift an.</li>
<li><code>30:</code> Das feine x-Achsengitter wird auf ganze Jahre gelegt &#8230;</li>
<li><code>31:</code> und das grobe Gitter auf alle zwei Jahre.</li>
</ul>
<p><strong>Fertig!</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://berlani.de/2012/06/r-statistik/r/ggplot2-ein-umfangreiches-beispiel/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ggplot &#8211; eine Einführung</title>
		<link>http://berlani.de/2012/06/r-statistik/r/ggplot-eine-einfuhrung/</link>
		<comments>http://berlani.de/2012/06/r-statistik/r/ggplot-eine-einfuhrung/#comments</comments>
		<pubDate>Wed, 20 Jun 2012 18:37:33 +0000</pubDate>
		<dc:creator>Jakob</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[einführung]]></category>
		<category><![CDATA[ggplot]]></category>
		<category><![CDATA[ggplot2]]></category>
		<category><![CDATA[grafik]]></category>
		<category><![CDATA[r]]></category>

		<guid isPermaLink="false">http://berlani.de/?p=286</guid>
		<description><![CDATA[Wie funktioniert das mit ggplot? Ich schwärme ja oft von diesem tollen Packet, wie vermutlich fast jeder, der ggplot (genauer ggplot2) kennen und nutzen gelernt hat. Doch für Einsteiger ist die Struktur oft zuerst verwirrend und der Aufwand scheint nicht den Nutzen wert zu sein. Von dem Gegenteil möchte ich euch nun jedoch überzeugen, gerade [...]]]></description>
			<content:encoded><![CDATA[<p>Wie funktioniert das mit ggplot? Ich schwärme ja oft von diesem tollen Packet, wie vermutlich fast jeder, der ggplot (genauer <code>ggplot2</code>) kennen und nutzen gelernt hat. Doch für Einsteiger ist die Struktur oft zuerst verwirrend und der Aufwand scheint nicht den Nutzen wert zu sein. Von dem Gegenteil möchte ich euch nun jedoch überzeugen, gerade auch, weil sich der Aufwand am Ende doch erheblich reduzieren kann.</p>
<blockquote><p>
Die Vorteile von ggplot2:</p>
<ul>
<li>schönere Optik</li>
<li>Grafiken können leicht an neue Bedürfnisse angepasst werden</li>
<li>Exploration von Daten mit wenig Code und bereits <i>druckfertigen</i> Ergebnissen</li>
<li>Erscheinungsbild mit wenig Code sauber veränderbar</li>
<li>Viele bereits mitgelieferte Plot-typen</li>
</ul>
<p>Nachteile:</p>
<ul>
<li>gewisse Einarbeitungszeit</li>
<li>Daten für ggplot2 Vorbereiten manchmal unintuitiv</li>
</ul>
</blockquote>
<p><span id="more-286"></span><br />
Zuerst sollte man verstehen, wie die Daten vorliegen sollten, damit ggplot seine Arbeit zuverlässig tut. Dazu folgender Grundsatz:</p>
<h3>Jeder Datenpunkt eine Zeile!</h3>
<p>In einem data.frame (es muss immer ein data.frame sein) muss jede Zeile einen Datenpunkt in unserem Plot widerspiegeln. Wenn also zu einem Merkmalsträger mehrere Daten vorliegen die auf einer Achse liegen sollen haben wir zunächst einmal ein kleines Problem. Dazu aber später mehr.<br />
Wenn also jede Zeile einen Datenpunkt enthält, welche Spalten soll es denn geben? Hier liegen die großen Stärken von ggplot2: Fast jeder Spalte kann eine &#8220;grafische Interpretation&#8221; zugewiesen werden. Betrachten wir folgendes Beispiel:<br />
<div id="attachment_288" class="wp-caption alignnone" style="width: 552px"><img src="http://berlani.de/wp-content/uploads/2012/06/tabBSP.png" alt="" title="Beispieldatensatz" width="542" height="198" class="size-full wp-image-288" /><p class="wp-caption-text">Hier ein Beispieldatensatz und wie wir ihn darstellen wollen.</p></div><br />
Hier wird schnell klar, dass, wenn wir das mit den Standardmitteln von R umsetzen wollen würden, vor allem das Einfärben ein wenig lästige Codearbeit darstellen würde. Dann bräuchten wir ja auch noch eine Legende und was ist, wenn später noch eine Stadt mit einer anderen Regierung hinzukommen würde. Dann müsste man vermutlich noch mal ein Wenig am Code rumfummeln. Unschön!<br />
Mit ggplot2 geht das natürlich in Sekunden! Trotzdem schauen wir uns es erstmal ganz langsam an:</p>

<div class="wp_codebox"><table><tr id="p28635"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p286code35"><pre class="rsplus" style="font-family:monospace;"><a href="http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=utils:install.packages"><span style="color: #0000FF; font-weight: bold;">install.<span style="">packages</span></span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;ggplot2&quot;</span><span style="color: #080;">&#41;</span> <span style="color: #228B22;"># Installiert das Packet </span>
<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;ggplot2&quot;</span><span style="color: #080;">&#41;</span> <span style="color: #228B22;"># Lädt das Packet</span>
&nbsp;
staedte <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">&quot;Stadt;Einwohner;Fläche in km2;Schulden in Mrd;Regierung
Berlin;3499879;891,85;64,03;SPD und CDU
Dortmund;580444;280,71;2,16;SPD
München;1353186;310,43;0;SPD
Hamburg;1799144;755,264;24,9;SPD
Stuttgart;606588;207,36;2,8;CDU&quot;</span>
&nbsp;
stadtdaten <span style="color: #080;">&lt;-</span> <a href="http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=utils:read.table"><span style="color: #0000FF; font-weight: bold;">read.<span style="">table</span></span></a><span style="color: #080;">&#40;</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/textConnection.html"><span style="color: #0000FF; font-weight: bold;">textConnection</span></a><span style="color: #080;">&#40;</span>staedte<span style="color: #080;">&#41;</span>, sep<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;;&quot;</span>, <a href="http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=utils:head"><span style="color: #0000FF; font-weight: bold;">head</span></a><span style="color: #080;">=</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/T.html"><span style="color: #0000FF; font-weight: bold;">T</span></a>, dec<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;,&quot;</span><span style="color: #080;">&#41;</span>
<a href="http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=utils:head"><span style="color: #0000FF; font-weight: bold;">head</span></a><span style="color: #080;">&#40;</span>stadtdaten<span style="color: #080;">&#41;</span>
<span style="color: #228B22;"># Wir sehen, dass die Spaltennamen keine Leerzeichen mehr enthalten.</span>
<span style="color: #228B22;"># Die Spaltennamen sind gleich noch wichtig.</span></pre></td></tr></table></div>

<p>Bis hierhin alles also recht klar. Nun kommen wir dorthin, wo die Magie geschieht:</p>

<div class="wp_codebox"><table><tr id="p28636"><td class="line_numbers"><pre>16
17
18
19
20
</pre></td><td class="code" id="p286code36"><pre class="rsplus" style="font-family:monospace;">g <span style="color: #080;">&lt;-</span> <a href="http://google.com/search?btnI=1&q=ggplot&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">ggplot</span></a><span style="color: #080;">&#40;</span>stadtdaten, 
            <a href="http://google.com/search?btnI=1&q=aes&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">aes</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>Einwohner, 
                y<span style="color: #080;">=</span>Fläche.<span style="">in</span>.<span style="">km2</span>, 
                size<span style="color: #080;">=</span>Schulden.<span style="">in</span>.<span style="">Mrd</span>, 
                color<span style="color: #080;">=</span>Regierung<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>Was geschieht hier? <code>g</code> wird das neue ggplot-Objekt. Hier kommt alles rein, was wir plotten wollen. Initialisiert wird es stets mit <code>ggplot()</code>. Meistens wird man jedoch noch zwei Parameter angeben: An erster Stelle steht der Datensatz (hier: <code>stadtdaten</code>), aus dem ggplot die Informationen ziehen soll. Das zweite Argument ist die Funktion <code>aes()</code>. Diese wertet jede Zeile des Datensatzes aus und ihr kann wiederum vorgegeben werden, welche Spalte wie dargestellt werden soll. Die Nutzung von <code>aes()</code> stellt auch die Beschriftung der Achsen und der Legende sicher.<br />
Jetzt muss dem ggplot Objekt noch gesagt werden, wie es zu plotten ist. Am sinnvollsten erscheint es die Daten als Punkte darzustellen:</p>

<div class="wp_codebox"><table><tr id="p28637"><td class="line_numbers"><pre>21
22
</pre></td><td class="code" id="p286code37"><pre class="rsplus" style="font-family:monospace;">g <span style="color: #080;">&lt;-</span> g <span style="color: #080;">+</span> <a href="http://google.com/search?btnI=1&q=geom_point&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">geom_point</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
g</pre></td></tr></table></div>

<p><code>geom_point()</code> muss kein weiteres Argument übergeben werden, da bereits alle notwendigen Angaben durch <code>aes()</code> in <code>g</code> stehen. Da <code>g</code> jetzt weiß, wie es zu plotten ist können wir es einfach mit dem Aufruf <code>g</code> plotten.<br />
So sieht das Ergebnis aus:<br />
<img src="http://berlani.de/wp-content/uploads/2012/06/Rplot.png" alt="" title="Einige Städte und ihre Schulden" width="610" height="400" class="alignnone size-full wp-image-302" /><br />
Noch nicht besonders toll. Die Punkte sind viel zu klein und zuerst einmal würden wir gerne wissen welche Stadt nun welcher Punkt ist. Das ist überhaupt kein Problem! <code>geom_text()</code> macht alles.</p>

<div class="wp_codebox"><table><tr id="p28638"><td class="line_numbers"><pre>24
25
</pre></td><td class="code" id="p286code38"><pre class="rsplus" style="font-family:monospace;">g <span style="color: #080;">+</span> <a href="http://google.com/search?btnI=1&q=geom_text&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">geom_text</span></a><span style="color: #080;">&#40;</span><a href="http://google.com/search?btnI=1&q=aes&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">aes</span></a><span style="color: #080;">&#40;</span>label<span style="color: #080;">=</span>Stadt<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
g</pre></td></tr></table></div>

<p>Wir erinnern uns: <code>g</code> wurden bereits gesagt, dass es ein Punkteplot werden soll. Jetzt kommt das so genannte <i>Geom</i> (geometric object) <code>geom_text()</code> hinzu. Das Geom braucht jedoch zwingend noch ein Argument, welches ihm sagt, welcher Text denn jeweils gedruckt werden soll. Dieses regelt wieder <code>aes()</code>.<br />
Doch was ist nun passiert?!<br />
<img src="http://berlani.de/wp-content/uploads/2012/06/Rplot01.png" alt="" title="Städte mit Text" width="610" height="400" class="alignnone size-full wp-image-303" /><br />
Wie bereits erwähnt gilt das ganz am Anfang in <code>ggplot()</code> geschriebene <code>aes()</code> für alle Dinge die zu <code>g</code> hinzugefügt werden. Und so bedient sich auch das <code>geom_text()</code> der Farben und Größen, welche wir anfangs bestimmt haben. Häufig wollen wir das auch. Zumindest für die x und y-Koordinaten und hier vielleicht auch für die Farbe. Also müssen wir die Eigenschaften trennen, je nachdem ob sie mit zum Text oder zu den Punkten gehören sollen und nur die gemeinsamen Eigenschaften schreiben wir in die allgemeinen <i>Aesthetics</i> (<code>aes()</code>).<br />
Also noch mal von vorn:</p>

<div class="wp_codebox"><table><tr id="p28639"><td class="line_numbers"><pre>27
28
29
30
31
32
</pre></td><td class="code" id="p286code39"><pre class="rsplus" style="font-family:monospace;">g2 <span style="color: #080;">&lt;-</span> <a href="http://google.com/search?btnI=1&q=ggplot&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">ggplot</span></a><span style="color: #080;">&#40;</span>stadtdaten,
             <a href="http://google.com/search?btnI=1&q=aes&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">aes</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>Einwohner, 
                 y<span style="color: #080;">=</span>Fläche.<span style="">in</span>.<span style="">km2</span>,
                 color<span style="color: #080;">=</span>Regierung<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
g2 <span style="color: #080;">&lt;-</span> g2 <span style="color: #080;">+</span> <a href="http://google.com/search?btnI=1&q=geom_point&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">geom_point</span></a><span style="color: #080;">&#40;</span><a href="http://google.com/search?btnI=1&q=aes&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">aes</span></a><span style="color: #080;">&#40;</span>size<span style="color: #080;">=</span>Schulden.<span style="">in</span>.<span style="">Mrd</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
g2 <span style="color: #080;">+</span> <a href="http://google.com/search?btnI=1&q=geom_text&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">geom_text</span></a><span style="color: #080;">&#40;</span><a href="http://google.com/search?btnI=1&q=aes&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">aes</span></a><span style="color: #080;">&#40;</span>label<span style="color: #080;">=</span>Stadt<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p><img src="http://berlani.de/wp-content/uploads/2012/06/Rplot02.png" alt="" title="Städte mit gleichen Schriftgrößen" width="610" height="400" class="alignnone size-full wp-image-304" /><br />
Immer noch nicht das Gelbe vom Ei. Die Schrift braucht rechts und links irgendwie mehr Platz. Das geht wie folgt:</p>

<div class="wp_codebox"><table><tr id="p28640"><td class="line_numbers"><pre>34
35
36
</pre></td><td class="code" id="p286code40"><pre class="rsplus" style="font-family:monospace;">g2 <span style="color: #080;">&lt;-</span> g2 <span style="color: #080;">+</span> <a href="http://google.com/search?btnI=1&q=scale_x_continuous&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">scale_x_continuous</span></a><span style="color: #080;">&#40;</span>expand<span style="color: #080;">=</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0.2</span>,<span style="color: #ff0000;">0</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
           <a href="http://google.com/search?btnI=1&q=scale_y_continuous&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">scale_y_continuous</span></a><span style="color: #080;">&#40;</span>expand<span style="color: #080;">=</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span>,<span style="color: #ff0000;">60</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
g2 <span style="color: #080;">+</span> <a href="http://google.com/search?btnI=1&q=geom_text&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">geom_text</span></a><span style="color: #080;">&#40;</span><a href="http://google.com/search?btnI=1&q=aes&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">aes</span></a><span style="color: #080;">&#40;</span>label<span style="color: #080;">=</span>Stadt<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>Nun haben wir die x-Achse links und rechts um 20% ihrer ursprünglichen Breite erweitert. Die y-Achse geht nun 60 Einheiten weiter nach oben und unten. Man kann in <code>expand()</code> auch beides Kombinieren. Die erste Zahl ist ein Faktor und die zweite eine  Konstante.<br />
<img src="http://berlani.de/wp-content/uploads/2012/06/Rplot03.png" alt="" title="Städte mit größerem Plotrand" width="610" height="400" class="alignnone size-full wp-image-305" /><br />
Immer noch nicht perfekt. Der Text sollte nicht so auf den Punkten kleben.</p>

<div class="wp_codebox"><table><tr id="p28641"><td class="line_numbers"><pre>38
</pre></td><td class="code" id="p286code41"><pre class="rsplus" style="font-family:monospace;">g2 <span style="color: #080;">+</span> <a href="http://google.com/search?btnI=1&q=geom_text&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">geom_text</span></a><span style="color: #080;">&#40;</span><a href="http://google.com/search?btnI=1&q=aes&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">aes</span></a><span style="color: #080;">&#40;</span>label<span style="color: #080;">=</span>Stadt<span style="color: #080;">&#41;</span>,vjust<span style="color: #080;">=-</span><span style="color: #ff0000;">0.6</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>Wichtig ist zu beachten, dass <code>vjust</code> und <code>hjust</code> <i>offiziell</i> nur die Werte 0,0.5,1 unterstützt. Mehr Möglichkeiten zum modifizieren des Text-Geoms findet man auf der <a href="http://had.co.nz/ggplot2/geom_text.html" target="_blank"><em>offiziellen</em> ggplot2 Hilfe</a>.<br />
<img src="http://berlani.de/wp-content/uploads/2012/06/Rplot04.png" alt="" title="Städte mit schönem Text" width="610" height="400" class="alignnone size-full wp-image-306" /><br />
So sieht es doch schon fast brauchbar aus. Jetzt wäre es jedoch super, wenn die Skalen etwas schöner wären und überhaupt alles:</p>

<div class="wp_codebox"><table><tr id="p28642"><td class="line_numbers"><pre>40
41
42
43
</pre></td><td class="code" id="p286code42"><pre class="rsplus" style="font-family:monospace;">g2 <span style="color: #080;">&lt;-</span> g2 <span style="color: #080;">+</span> <a href="http://google.com/search?btnI=1&q=scale_y_continuous&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">scale_y_continuous</span></a><span style="color: #080;">&#40;</span>trans<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;log2&quot;</span>, expand<span style="color: #080;">=</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span>,<span style="color: #ff0000;">0.22</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
  <span style="color: #228B22;">#mit trans funktioniert expand etwas merkwürdig, neuanpassung notwendig</span>
           <a href="http://google.com/search?btnI=1&q=scale_size_continuous&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">scale_size_continuous</span></a><span style="color: #080;">&#40;</span>trans<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;sqrt&quot;</span>,<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/range.html"><span style="color: #0000FF; font-weight: bold;">range</span></a><span style="color: #080;">=</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">3</span>,<span style="color: #ff0000;">10</span><span style="color: #080;">&#41;</span>, breaks<span style="color: #080;">=</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span>,<span style="color: #ff0000;">10</span>,<span style="color: #ff0000;">30</span>,<span style="color: #ff0000;">60</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
g2 <span style="color: #080;">+</span> <a href="http://google.com/search?btnI=1&q=geom_text&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">geom_text</span></a><span style="color: #080;">&#40;</span><a href="http://google.com/search?btnI=1&q=aes&as_sitesearch=http://had.co.nz/ggplot2/"><span style="color: #8C00FF; font-weight: bold:">aes</span></a><span style="color: #080;">&#40;</span>label<span style="color: #080;">=</span>Stadt,vjust<span style="color: #080;">=-</span><span style="color: #ff0000;">0.5</span><span style="color: #080;">-</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/sqrt.html"><span style="color: #0000FF; font-weight: bold;">sqrt</span></a><span style="color: #080;">&#40;</span>Schulden.<span style="">in</span>.<span style="">Mrd</span><span style="color: #080;">/</span><span style="color: #ff0000;">80</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>Mit <code>trans=""</code> können die Achsen transformiert werden. Das Schöne ist, dass auch die Koordinatengitter sich anpasst, sodass die Transformation auf den ersten Blick sichtbar ist. Mögliche Transformationen sind u.a: <i>log, log2, sqrt, exp, log10</i>.<br />
Mit <code>scale_size_continuous()</code> wird die Größenskala angepasst. <code>trans="log"</code> kann hier nicht verwendet werden, da München nicht verschuldet ist. Mit <code>range=c(3,10)</code> wird festgelegt, dass der kleinste Punkt die Größe 3 und der größte die Größe 10 haben soll. <code>breaks=c(0,10,30,60)</code> lässt uns die Möglichkeit manuell anzugeben, welche Größen in die Legende gepackt werden. Oft liefert da die Automatik von ggplot nicht die besten Ergebnisse.<br />
Ein kleiner Trick ist auch in <code>geom_text()</code> enthalten: Wichtig ist zu merken, dass bei ggplot fast alle Plottingeigenschaften sowohl in den <em>Aesthetics</em> als auch außerhalb stehen können. Stehen sie Außerhalb gelten sie für alle Elemente des <em>Geoms</em> pauschal und werden nicht in die Legende übernommen. Innerhalb von <code>aes()</code> werden sie für jede Zeile in dem Datensatz angewendet. Wenn man dies Verinnerlicht hat man schon eine der wichtigsten ggplot-Eigenschaften verstanden und kann damit gut arbeiten. In diesem Beispiel heben wir so den Text abhängig von der jeweiligen Punktgröße (die Schulden) weiter über die ursprüngliche Position.<br />
<img src="http://berlani.de/wp-content/uploads/2012/06/Rplot05.png" alt="" title="Fertiges Beispiel" width="610" height="400" class="alignnone size-full wp-image-307" /></p>
<p>Auch wenn das jetzt viel Text war, hoffe ich, dass ihr die Grundsätze von ggplot2 verstanden habt und somit auch die sehr hilfreichen und zahlreichen Beispiele auf der <a href="http://had.co.nz/ggplot2/" title="ggplot2 " target="_blank">offiziellen Seite</a> besser nachvollziehen könnt.</p>
]]></content:encoded>
			<wfw:commentRss>http://berlani.de/2012/06/r-statistik/r/ggplot-eine-einfuhrung/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Porto</title>
		<link>http://berlani.de/2012/06/foto/porto-2/</link>
		<comments>http://berlani.de/2012/06/foto/porto-2/#comments</comments>
		<pubDate>Sat, 16 Jun 2012 19:11:42 +0000</pubDate>
		<dc:creator>Jakob</dc:creator>
				<category><![CDATA[Fotografie]]></category>
		<category><![CDATA[angler]]></category>
		<category><![CDATA[foto]]></category>
		<category><![CDATA[porto]]></category>

		<guid isPermaLink="false">http://berlani.de/?p=294</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p><a href="http://berlani.de/wp-content/uploads/2012/06/P1000414.jpg" rel="lightbox[294]" title="Angler in Porto"><img src="http://berlani.de/wp-content/uploads/2012/06/P1000414-610x813.jpg" alt="" title="Angler in Porto" width="610" height="813" class="alignnone size-medium wp-image-295" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://berlani.de/2012/06/foto/porto-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>R: Berechnung in Variable speichern und gleichzeitig Ausgeben</title>
		<link>http://berlani.de/2012/06/r-statistik/r/r-berechnung-in-variable-speichern-und-gleichzeitig-ausgeben/</link>
		<comments>http://berlani.de/2012/06/r-statistik/r/r-berechnung-in-variable-speichern-und-gleichzeitig-ausgeben/#comments</comments>
		<pubDate>Mon, 11 Jun 2012 07:05:04 +0000</pubDate>
		<dc:creator>Jakob</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[hilfreich]]></category>
		<category><![CDATA[nützlich]]></category>
		<category><![CDATA[r]]></category>

		<guid isPermaLink="false">http://berlani.de/?p=264</guid>
		<description><![CDATA[Wieder ein kleiner Trick für den R-Alltag. Häufig will man eine Berechnung durchführen, das Ergebnis in einer Variable speichern und auch das Ergebnis gleich mal sehen. Zum Beispiel ist es ja nicht schlecht bei der Anpassung eines (linearen) Modells gleich mal die geschätzten Parameter zu sehen, bevor man weiter damit arbeitet. > (lm.trees]]></description>
			<content:encoded><![CDATA[<p>Wieder ein kleiner Trick für den  R-Alltag. Häufig will man eine Berechnung durchführen, das Ergebnis in einer Variable speichern und auch das Ergebnis gleich mal sehen. Zum Beispiel ist es ja nicht schlecht bei der Anpassung eines (linearen) Modells gleich mal die geschätzten Parameter zu sehen, bevor man weiter damit arbeitet.<br />
<code><br />
> (lm.trees <- lm(Height~.,data=trees))</p>
<p>Call:<br />
lm(formula = Height ~ ., data = trees)</p>
<p>Coefficients:<br />
(Intercept)        Girth       Volume<br />
    83.2958      -1.8615       0.5756<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://berlani.de/2012/06/r-statistik/r/r-berechnung-in-variable-speichern-und-gleichzeitig-ausgeben/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>apply() hoch 3</title>
		<link>http://berlani.de/2012/06/r-statistik/r/apply-hoch-3/</link>
		<comments>http://berlani.de/2012/06/r-statistik/r/apply-hoch-3/#comments</comments>
		<pubDate>Thu, 07 Jun 2012 14:26:09 +0000</pubDate>
		<dc:creator>Jakob</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[apply]]></category>
		<category><![CDATA[basics]]></category>
		<category><![CDATA[nützlich]]></category>
		<category><![CDATA[r]]></category>

		<guid isPermaLink="false">http://berlani.de/?p=259</guid>
		<description><![CDATA[Nach dem Beitrag zu lapply() und sapply() möchte ich hier nun das etwas mächtigere (?) apply() vorstellen, was man in R wohl kaum missen will. Ich werde kurz zeigen wie mit apply() Matrizen und data.frames zeilen- sowie spaltenweise Ausgewertet werden können. Nach dieser sehr leichten Übung widmen wir uns einem etwas trickreicherem Beispiel, in dem [...]]]></description>
			<content:encoded><![CDATA[<p>Nach dem <a href="http://berlani.de/2012/05/r-statistik/r/lapply-und-sapply-der-einstieg-in-die-apply-funktionen/" title="lapply() und sapply() – Der Einstieg in die apply-Funktionen.">Beitrag zu <code>lapply()</code> und <code>sapply()</code></a> möchte ich hier nun das etwas mächtigere (?) <code>apply()</code> vorstellen, was man in R wohl kaum missen will.</p>
<p>Ich werde kurz zeigen wie mit <code>apply()</code> Matrizen und data.frames zeilen- sowie spaltenweise Ausgewertet werden können. Nach dieser sehr leichten Übung widmen wir uns einem etwas trickreicherem Beispiel, in dem wir mehrere gleichartige Tabellen (also Matrizen bzw. data.frames) der gleichen Zeilen- und Spaltenanzahl vorliegen haben. Hier möchten wir die Informationen aus immer den gleichen Zellen zusammenfassen.<br />
<span id="more-259"></span></p>
<h3>apply auf einer Matrix</h3>
<p>Wer <code>lapply()</code> verstanden hat, der wird auch hiermit kein Problem haben.<br />
<code>apply(X, MARGIN, FUN, ...)</code><br />
<code>X</code> ist eine Matrix oder ein data.frame<br />
<code>MARGIN</code> gibt an, ob jede <b>Zeile <code>MARGIN=1</code></b> transponiert als Vektor der Funktion übergeben werden soll oder jede <b>Spalte <code>MARGIN=2</code></b> an die Funktion (<code>FUN</code>) übergeben wird. Es gibt noch weitere Möglichkeiten &#8211; dazu später.</p>
<p>Einfache Beispiele:</p>

<div class="wp_codebox"><table><tr id="p25950"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p259code50"><pre class="rsplus" style="font-family:monospace;"><a href="http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=datasets:USPersonalExpenditure"><span style="color: #CC9900; font-weight: bold;">USPersonalExpenditure</span></a>
<span style="color: #228B22;">#                      1940   1945  1950 1955  1960</span>
<span style="color: #228B22;">#Food and Tobacco    22.200 44.500 59.60 73.2 86.80</span>
<span style="color: #228B22;">#Household Operation 10.500 15.500 29.00 36.5 46.20</span>
<span style="color: #228B22;">#Medical and Health   3.530  5.760  9.71 14.0 21.10</span>
<span style="color: #228B22;">#Personal Care        1.040  1.980  2.45  3.4  5.40</span>
<span style="color: #228B22;">#Private Education    0.341  0.974  1.80  2.6  3.6</span>
&nbsp;
<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/apply.html"><span style="color: #0000FF; font-weight: bold;">apply</span></a><span style="color: #080;">&#40;</span><a href="http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=datasets:USPersonalExpenditure"><span style="color: #CC9900; font-weight: bold;">USPersonalExpenditure</span></a>,<span style="color: #ff0000;">2</span>,<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/sum.html"><span style="color: #0000FF; font-weight: bold;">sum</span></a><span style="color: #080;">&#41;</span>
<span style="color: #228B22;">#  1940    1945    1950    1955    1960 </span>
<span style="color: #228B22;">#37.611  68.714 102.560 129.700 163.140</span></pre></td></tr></table></div>

<p>Aus den Ausgaben der US-Bürger in Milliarden USD in den verschiedenen Kategorien, haben wir jetzt also berechnet, wie viele Milliarden sie in den Kategorien zusammen ausgegeben Haben. </p>
<p>Finden wir nun heraus, wie die Steigerung zum Vorjahr ausfällt. Dazu müssen wir die Daten natürlich Zeilenweise auswerten.</p>

<div class="wp_codebox"><table><tr id="p25951"><td class="line_numbers"><pre>13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code" id="p259code51"><pre class="rsplus" style="font-family:monospace;"><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/apply.html"><span style="color: #0000FF; font-weight: bold;">apply</span></a><span style="color: #080;">&#40;</span><a href="http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=datasets:USPersonalExpenditure"><span style="color: #CC9900; font-weight: bold;">USPersonalExpenditure</span></a>,<span style="color: #ff0000;">1</span>,<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/function.html"><span style="color: #0000FF; font-weight: bold;">function</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
	n <span style="color: #080;">&lt;-</span> <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/length.html"><span style="color: #0000FF; font-weight: bold;">length</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span>
	ratio <span style="color: #080;">&lt;-</span> x<span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">:</span>n<span style="color: #080;">&#93;</span><span style="color: #080;">/</span>x<span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #080;">&#40;</span>n<span style="color: #080;">-</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span>
	res <span style="color: #080;">&lt;-</span> <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'1940'</span><span style="color: #080;">=</span>NA,<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/round.html"><span style="color: #0000FF; font-weight: bold;">round</span></a><span style="color: #080;">&#40;</span>ratio,<span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span>
<span style="color: #228B22;">#      Food and Tobacco Household Operation Medical and Health Personal Care Private Education</span>
<span style="color: #228B22;"># 1940               NA                  NA                 NA            NA                NA</span>
<span style="color: #228B22;"># 1945             2.00                1.48               1.63          1.90              2.86</span>
<span style="color: #228B22;"># 1950             1.34                1.87               1.69          1.24              1.85</span>
<span style="color: #228B22;"># 1955             1.23                1.26               1.44          1.39              1.44</span>
<span style="color: #228B22;"># 1960             1.19                1.27               1.51          1.59              1.40</span></pre></td></tr></table></div>

<p>Hier sieht man auch sehr gut, dass <code>apply()</code> die Funktionsergebnisse (hier Vektoren) wieder schön in einer Matrix zusammenfasst, wenn es dazu in der Lage ist.</p>
<h3>apply() hoch 3</h3>
<p>Schauen wir uns einmal so eine &#8220;dreidimensionale&#8221; Tabelle an.</p>

<div class="wp_codebox"><table><tr id="p25952"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p259code52"><pre class="rsplus" style="font-family:monospace;"><a href="http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=datasets:HairEyeColor"><span style="color: #CC9900; font-weight: bold;">HairEyeColor</span></a>
<span style="color: #228B22;"># , , Sex = Male</span>
<span style="color: #228B22;"># </span>
<span style="color: #228B22;"># Eye</span>
<span style="color: #228B22;"># Hair    Brown Blue Hazel Green</span>
<span style="color: #228B22;"># Black    32   11    10     3</span>
<span style="color: #228B22;"># Brown    53   50    25    15</span>
<span style="color: #228B22;"># Red      10   10     7     7</span>
<span style="color: #228B22;"># Blond     3   30     5     8</span>
<span style="color: #228B22;"># </span>
<span style="color: #228B22;"># , , Sex = Female</span>
<span style="color: #228B22;"># </span>
<span style="color: #228B22;"># Eye</span>
<span style="color: #228B22;"># Hair    Brown Blue Hazel Green</span>
<span style="color: #228B22;"># Black    36    9     5     2</span>
<span style="color: #228B22;"># Brown    66   34    29    14</span>
<span style="color: #228B22;"># Red      16    7     7     7</span>
<span style="color: #228B22;"># Blond     4   64     5     8</span></pre></td></tr></table></div>

<p>Auch hier funktioniert <code>apply()</code> wie geschmiert.<br />
Wie oft kommt welche Haarfarbe vor?</p>

<div class="wp_codebox"><table><tr id="p25953"><td class="line_numbers"><pre>20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
</pre></td><td class="code" id="p259code53"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">#Wie oft kommt welche Haarfarbe vor?</span>
<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/apply.html"><span style="color: #0000FF; font-weight: bold;">apply</span></a><span style="color: #080;">&#40;</span><a href="http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=datasets:HairEyeColor"><span style="color: #CC9900; font-weight: bold;">HairEyeColor</span></a>,<span style="color: #ff0000;">1</span>,<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/sum.html"><span style="color: #0000FF; font-weight: bold;">sum</span></a><span style="color: #080;">&#41;</span>
<span style="color: #228B22;">#Black Brown   Red Blond </span>
<span style="color: #228B22;">#  108   286    71   127 </span>
&nbsp;
<span style="color: #228B22;">#Wie oft welche Augenfarbe?</span>
<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/apply.html"><span style="color: #0000FF; font-weight: bold;">apply</span></a><span style="color: #080;">&#40;</span><a href="http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=datasets:HairEyeColor"><span style="color: #CC9900; font-weight: bold;">HairEyeColor</span></a>,<span style="color: #ff0000;">2</span>,<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/sum.html"><span style="color: #0000FF; font-weight: bold;">sum</span></a><span style="color: #080;">&#41;</span>
<span style="color: #228B22;">#Brown  Blue Hazel Green </span>
<span style="color: #228B22;">#  220   215    93    64</span>
&nbsp;
<span style="color: #228B22;">#Die Tabelle für beide Geschlechter zusammengefasst:</span>
<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/apply.html"><span style="color: #0000FF; font-weight: bold;">apply</span></a><span style="color: #080;">&#40;</span><a href="http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=datasets:HairEyeColor"><span style="color: #CC9900; font-weight: bold;">HairEyeColor</span></a>,<span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #ff0000;">2</span>,<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/sum.html"><span style="color: #0000FF; font-weight: bold;">sum</span></a><span style="color: #080;">&#41;</span>
<span style="color: #228B22;">#        Eye</span>
<span style="color: #228B22;"># Hair    Brown Blue Hazel Green</span>
<span style="color: #228B22;"># Black    68   20    15     5</span>
<span style="color: #228B22;"># Brown   119   84    54    29</span>
<span style="color: #228B22;"># Red      26   17    14    14</span>
<span style="color: #228B22;"># Blond     7   94    10    16</span></pre></td></tr></table></div>

<p>Wieso <code>MARGIN=c(1,2)</code>? Man kann sich das ganz einfach so vorstellen: Für die Erzeugung des Vektors, welcher an die Funktion übergeben wird, bleibt jeweils die Zeile (1) und die Spalte (2) fest. In der dritten Dimension ist also &#8220;frei&#8221;. Im zweidimensionalen Fall ist es genau so.</p>
<p>Wenn man es verinnerlicht hat, sollte man sich auch denken können, was folgende Aufrufe erzeugen? Was denkst du?</p>

<div class="wp_codebox"><table><tr id="p25954"><td class="line_numbers"><pre>38
39
</pre></td><td class="code" id="p259code54"><pre class="rsplus" style="font-family:monospace;"><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/apply.html"><span style="color: #0000FF; font-weight: bold;">apply</span></a><span style="color: #080;">&#40;</span><a href="http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=datasets:HairEyeColor"><span style="color: #CC9900; font-weight: bold;">HairEyeColor</span></a>,<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span>,<span style="color: #ff0000;">3</span><span style="color: #080;">&#41;</span>,<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/sum.html"><span style="color: #0000FF; font-weight: bold;">sum</span></a><span style="color: #080;">&#41;</span>
<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/apply.html"><span style="color: #0000FF; font-weight: bold;">apply</span></a><span style="color: #080;">&#40;</span><a href="http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=datasets:HairEyeColor"><span style="color: #CC9900; font-weight: bold;">HairEyeColor</span></a>,<span style="color: #ff0000;">2</span><span style="color: #080;">:</span><span style="color: #ff0000;">3</span>,<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/sum.html"><span style="color: #0000FF; font-weight: bold;">sum</span></a><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>Außer für solch einfache Beispiele kann das auch von Nutzen sein um evtl. die Varianz eines Wertes in einer Kontingenztafel zu berechnen, wenn mehrere solcher Tafeln vorliegen. So kann es vorkommen, dass man mehrere Kontingenztafel simuliert und sich dann für die Streuung der Werte in den einzelnen Zellen interessiert.</p>
<h3>Wie erzeugt man solch dreidimensionale Tabellen?</h3>
<p></strong><br />
Ganz einfach z.B. mit <code>table()</code>:</p>

<div class="wp_codebox"><table><tr id="p25955"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p259code55"><pre class="rsplus" style="font-family:monospace;"><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/table.html"><span style="color: #0000FF; font-weight: bold;">table</span></a><span style="color: #080;">&#40;</span><a href="http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=datasets:mtcars"><span style="color: #CC9900; font-weight: bold;">mtcars</span></a><span style="color: #080;">&#91;</span>,<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;am&quot;</span>,<span style="color: #ff0000;">&quot;gear&quot;</span>,<span style="color: #ff0000;">&quot;cyl&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>Etwas komplizierter, eine kleine Simulationsstudie zu Vierfeldertafeln.</p>

<div class="wp_codebox"><table><tr id="p25956"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code" id="p259code56"><pre class="rsplus" style="font-family:monospace;">ktafeln <span style="color: #080;">&lt;-</span> <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/replicate.html"><span style="color: #0000FF; font-weight: bold;">replicate</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1000</span>, <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/table.html"><span style="color: #0000FF; font-weight: bold;">table</span></a><span style="color: #080;">&#40;</span><a href="http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=stats:runif"><span style="color: #0000FF; font-weight: bold;">runif</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">10</span><span style="color: #080;">&#41;</span><span style="color: #080;">&gt;</span><span style="color: #ff0000;">0.5</span>,<a href="http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=stats:runif"><span style="color: #0000FF; font-weight: bold;">runif</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">10</span><span style="color: #080;">&#41;</span><span style="color: #080;">&gt;</span><span style="color: #ff0000;">0.5</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
&nbsp;
tafArray <span style="color: #080;">&lt;-</span> <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/function.html"><span style="color: #0000FF; font-weight: bold;">function</span></a><span style="color: #080;">&#40;</span>taflist<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
	<span style="color: #228B22;">#Konvertiert eine Liste von Matrizen mit gleichen Dimensionen (AxB) ein einen 3D-Array</span>
	<span style="color: #228B22;">#Baut die 3te Dimension nacheinander auf.</span>
	dimension <span style="color: #080;">&lt;-</span> <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/dim.html"><span style="color: #0000FF; font-weight: bold;">dim</span></a><span style="color: #080;">&#40;</span>taflist<span style="color: #080;">&#91;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span> <span style="color: #228B22;">#Die Dimension ist jetzt also (AxBx1)</span>
	Array <span style="color: #080;">&lt;-</span> <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/array.html"><span style="color: #0000FF; font-weight: bold;">array</span></a><span style="color: #080;">&#40;</span>taflist<span style="color: #080;">&#91;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span>, <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/dim.html"><span style="color: #0000FF; font-weight: bold;">dim</span></a><span style="color: #080;">=</span>dimension<span style="color: #080;">&#41;</span>
	<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/for.html"><span style="color: #0000FF; font-weight: bold;">for</span></a><span style="color: #080;">&#40;</span>i <span style="color: #0000FF; font-weight: bold;">in</span> <span style="color: #ff0000;">2</span><span style="color: #080;">:</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/length.html"><span style="color: #0000FF; font-weight: bold;">length</span></a><span style="color: #080;">&#40;</span>taflist<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
		dimension <span style="color: #080;">&lt;-</span> dimension <span style="color: #080;">+</span> <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span>,<span style="color: #ff0000;">0</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>
		<span style="color: #228B22;">#Füge neue Matrix an (AxBxi)</span>
		Array <span style="color: #080;">&lt;-</span> <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/array.html"><span style="color: #0000FF; font-weight: bold;">array</span></a><span style="color: #080;">&#40;</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span>Array,taflist<span style="color: #080;">&#91;</span><span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>,<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/dim.html"><span style="color: #0000FF; font-weight: bold;">dim</span></a><span style="color: #080;">=</span>dimension<span style="color: #080;">&#41;</span>
	<span style="color: #080;">&#125;</span>
	<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/return.html"><span style="color: #0000FF; font-weight: bold;">return</span></a><span style="color: #080;">&#40;</span>Array<span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span>
&nbsp;
matrix3d <span style="color: #080;">&lt;-</span> tafArray<span style="color: #080;">&#40;</span>ktafeln<span style="color: #080;">&#41;</span>
res <span style="color: #080;">&lt;-</span> <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/apply.html"><span style="color: #0000FF; font-weight: bold;">apply</span></a><span style="color: #080;">&#40;</span>matrix3d,<span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #ff0000;">2</span>,<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/function.html"><span style="color: #0000FF; font-weight: bold;">function</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span> <a href="http://rgm2.lab.nig.ac.jp/RGM2/func.php?rd_id=stats:quantile"><span style="color: #0000FF; font-weight: bold;">quantile</span></a><span style="color: #080;">&#40;</span>x, probs<span style="color: #080;">=</span><a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0.05</span>,<span style="color: #ff0000;">0.95</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
res <span style="color: #228B22;">#nicht so übersichtlich</span>
<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/aperm.html"><span style="color: #0000FF; font-weight: bold;">aperm</span></a><span style="color: #080;">&#40;</span>res,<a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">2</span>,<span style="color: #ff0000;">3</span>,<span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #228B22;">#Stellt die Quantile an letzte Stelle der Dimensionen</span>
<span style="color: #228B22;">#, , 5%</span>
<span style="color: #228B22;">#</span>
<span style="color: #228B22;">#     [,1] [,2]</span>
<span style="color: #228B22;">#[1,]    0    1</span>
<span style="color: #228B22;">#[2,]    0    0</span>
<span style="color: #228B22;">#</span>
<span style="color: #228B22;">#, , 95%</span>
<span style="color: #228B22;">#</span>
<span style="color: #228B22;">#     [,1] [,2]</span>
<span style="color: #228B22;">#[1,]    5    5</span>
<span style="color: #228B22;">#[2,]    5    5</span></pre></td></tr></table></div>

<p>Das sind natürlich keine Konfidenzintervalle an denen sich etwas testen ließe. Nur eine Spielerei und zur Verdeutlichung.</p>
]]></content:encoded>
			<wfw:commentRss>http://berlani.de/2012/06/r-statistik/r/apply-hoch-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Kleiner R-Helferling: example()</title>
		<link>http://berlani.de/2012/06/r-statistik/r/kleiner-r-helferling-example/</link>
		<comments>http://berlani.de/2012/06/r-statistik/r/kleiner-r-helferling-example/#comments</comments>
		<pubDate>Sun, 03 Jun 2012 18:17:24 +0000</pubDate>
		<dc:creator>Jakob</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[beispiele]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[hilfreich]]></category>
		<category><![CDATA[r]]></category>

		<guid isPermaLink="false">http://berlani.de/?p=256</guid>
		<description><![CDATA[Hui. Dass es diese Funktion gibt hätte ich ja nicht gedacht. Sicherlich seid ihr schon über die Beispiele in der R-Hilfe gestolpert und habt sie auch mal ausgeführt um zu gucken was passiert. Copy&#038;Paste? Pah! Mit example() geht&#8217;s auch schneller. Was kann man nochmal mit predict.glm() machen? Mit example(predict.glm) findest du es heraus. Funktioniert auch [...]]]></description>
			<content:encoded><![CDATA[<p>Hui. Dass es diese Funktion gibt hätte ich ja nicht gedacht. Sicherlich seid ihr schon über die Beispiele in der R-Hilfe gestolpert und habt sie auch mal ausgeführt um zu gucken was passiert. Copy&#038;Paste? Pah! Mit <code>example()</code> geht&#8217;s auch schneller. Was kann man nochmal mit <code>predict.glm()</code> machen? Mit <code>example(predict.glm)</code> findest du es heraus. Funktioniert auch ganz gut für viele Beispieldatensätze <code>example(chickwts)</code>.<br />
<span id="more-256"></span><br />
Welche Beispieldatensätze gibt es in R?<br />
<code>library(help="datasets")</code></p>
]]></content:encoded>
			<wfw:commentRss>http://berlani.de/2012/06/r-statistik/r/kleiner-r-helferling-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dänemark</title>
		<link>http://berlani.de/2012/06/foto/247/</link>
		<comments>http://berlani.de/2012/06/foto/247/#comments</comments>
		<pubDate>Sat, 02 Jun 2012 23:07:56 +0000</pubDate>
		<dc:creator>Jakob</dc:creator>
				<category><![CDATA[Fotografie]]></category>
		<category><![CDATA[analog]]></category>
		<category><![CDATA[dänemark]]></category>
		<category><![CDATA[düne]]></category>
		<category><![CDATA[sand]]></category>

		<guid isPermaLink="false">http://berlani.de/?p=247</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p><a href="http://berlani.de/wp-content/uploads/2012/06/0015.jpg" rel="lightbox[247]" title="0015"><img src="http://berlani.de/wp-content/uploads/2012/06/0015-610x406.jpg" alt="" title="0015" width="610" height="406" class="alignnone size-medium wp-image-248" /></a><a href="http://berlani.de/wp-content/uploads/2012/06/0018.jpg" rel="lightbox[247]" title="0018"><img src="http://berlani.de/wp-content/uploads/2012/06/0018-610x406.jpg" alt="" title="0018" width="610" height="406" class="alignnone size-medium wp-image-249" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://berlani.de/2012/06/foto/247/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
