Newer
Older
<span id="cb40-14"><a href="#cb40-14" aria-hidden="true" tabindex="-1"></a>fakeDfbyDistrict<span class="sc">$</span>pred_se <span class="ot"><-</span> predFakeDfbyDistrict<span class="sc">$</span>se.fit</span></code></pre></div>
<p>Anschließend können wir das ganze dann mit <code>ggplot</code> plotten:</p>
<div class="sourceCode" id="cb41"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb41-1"><a href="#cb41-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(fakeDfbyDistrict,</span>
<span id="cb41-2"><a href="#cb41-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">x =</span> trstprt, </span>
<span id="cb41-3"><a href="#cb41-3" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> pred,</span>
<span id="cb41-4"><a href="#cb41-4" aria-hidden="true" tabindex="-1"></a> <span class="at">color =</span> district,</span>
<span id="cb41-5"><a href="#cb41-5" aria-hidden="true" tabindex="-1"></a> <span class="at">shape =</span> district</span>
<span id="cb41-6"><a href="#cb41-6" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb41-7"><a href="#cb41-7" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb41-8"><a href="#cb41-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_line</span>() <span class="sc">+</span></span>
<span id="cb41-9"><a href="#cb41-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">ylab</span>(<span class="st">"Predicted value of Trust in Parliament"</span>) <span class="sc">+</span></span>
<span id="cb41-10"><a href="#cb41-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">xlab</span>(<span class="st">"Trust in Parties"</span>) <span class="sc">+</span></span>
<span id="cb41-11"><a href="#cb41-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">"Linear relationship between trstprl and trstprt (others constant)"</span>,</span>
<span id="cb41-12"><a href="#cb41-12" aria-hidden="true" tabindex="-1"></a> <span class="at">color =</span> <span class="st">"Distrikte"</span></span>
<span id="cb41-13"><a href="#cb41-13" aria-hidden="true" tabindex="-1"></a> ) </span></code></pre></div>
<p><img src="ggplot-advanced_files/figure-html/ggplot-districts-1.png" width="576" style="display: block; margin: auto;" /> Somit haben wir die Regressionslinie zwischen <code>trstprl</code> und <code>trstprt</code> unter Konstanthalten von Alter, Zufriedenheit mit der Demokratie für Personen nach Distrikten dargestellt.</p>
<p>Oftmals möchte man aber eine Linie besonders hervorheben und zum Beispiel die Datenpunkte des hervorgehobenen Distrikts einzeichnen. Prüfe den Code zu dem vorherigen und überlege, was an welchen Stellen geändert wurde!</p>
<div class="sourceCode" id="cb42"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb42-1"><a href="#cb42-1" aria-hidden="true" tabindex="-1"></a>fakeDistrict12 <span class="ot"><-</span> fakeDfbyDistrict <span class="sc">%>%</span> </span>
<span id="cb42-2"><a href="#cb42-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(district <span class="sc">==</span> <span class="st">"Distrikt 12"</span>) <span class="sc">%>%</span> </span>
<span id="cb42-3"><a href="#cb42-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(<span class="sc">-</span>district)</span>
<span id="cb42-4"><a href="#cb42-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb42-5"><a href="#cb42-5" aria-hidden="true" tabindex="-1"></a>district12 <span class="ot"><-</span> pss <span class="sc">%>%</span> </span>
<span id="cb42-6"><a href="#cb42-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(district <span class="sc">==</span> <span class="st">"Distrikt 12"</span>) <span class="sc">%>%</span> </span>
<span id="cb42-7"><a href="#cb42-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(<span class="sc">-</span>district)</span>
<span id="cb42-8"><a href="#cb42-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb42-9"><a href="#cb42-9" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(fakeDistrict12,</span>
<span id="cb42-10"><a href="#cb42-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">x =</span> trstprt, </span>
<span id="cb42-11"><a href="#cb42-11" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> pred</span>
<span id="cb42-12"><a href="#cb42-12" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb42-13"><a href="#cb42-13" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb42-14"><a href="#cb42-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_line</span>(<span class="at">color =</span> <span class="st">"tomato"</span>) <span class="sc">+</span> </span>
<span id="cb42-15"><a href="#cb42-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">data =</span> district12,</span>
<span id="cb42-16"><a href="#cb42-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">y =</span> trstprl),</span>
<span id="cb42-17"><a href="#cb42-17" aria-hidden="true" tabindex="-1"></a> <span class="at">color =</span> <span class="st">"tomato"</span>,</span>
<span id="cb42-18"><a href="#cb42-18" aria-hidden="true" tabindex="-1"></a> <span class="at">position =</span> <span class="st">"jitter"</span>,</span>
<span id="cb42-19"><a href="#cb42-19" aria-hidden="true" tabindex="-1"></a> <span class="at">size =</span> <span class="fl">0.7</span>,</span>
<span id="cb42-20"><a href="#cb42-20" aria-hidden="true" tabindex="-1"></a> <span class="at">alpha =</span> <span class="fl">0.5</span></span>
<span id="cb42-21"><a href="#cb42-21" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span> </span>
<span id="cb42-22"><a href="#cb42-22" aria-hidden="true" tabindex="-1"></a> <span class="fu">ylab</span>(<span class="st">"Predicted value of Trust in Parliament"</span>) <span class="sc">+</span></span>
<span id="cb42-23"><a href="#cb42-23" aria-hidden="true" tabindex="-1"></a> <span class="fu">xlab</span>(<span class="st">"Trust in Parties"</span>) <span class="sc">+</span></span>
<span id="cb42-24"><a href="#cb42-24" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">"Linear relationship between trstprl and trstprt (others constant)"</span>,</span>
<span id="cb42-25"><a href="#cb42-25" aria-hidden="true" tabindex="-1"></a> <span class="at">lty =</span> <span class="st">"Distrikte"</span>,</span>
<span id="cb42-26"><a href="#cb42-26" aria-hidden="true" tabindex="-1"></a> <span class="at">caption =</span> <span class="st">"Highlighted District 12."</span></span>
<span id="cb42-27"><a href="#cb42-27" aria-hidden="true" tabindex="-1"></a> )</span></code></pre></div>
<p><img src="ggplot-advanced_files/figure-html/ggplot-districts2-1.png" width="576" style="display: block; margin: auto;" /></p>
<p>Wir könnten zusätzlich auch noch die anderen Datenpunkte in das Bild einfügen. Man sollte aber immer aufpassen, dass ein Plot nicht zu unübersichtlich wird.</p>
<div class="sourceCode" id="cb43"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb43-1"><a href="#cb43-1" aria-hidden="true" tabindex="-1"></a>districts <span class="ot"><-</span> pss <span class="sc">%>%</span> </span>
<span id="cb43-2"><a href="#cb43-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(district <span class="sc">!=</span> <span class="st">"Distrikt 12"</span>) <span class="sc">%>%</span> </span>
<span id="cb43-3"><a href="#cb43-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(<span class="sc">-</span>district)</span>
<span id="cb43-4"><a href="#cb43-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb43-5"><a href="#cb43-5" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(fakeDistrict12,</span>
<span id="cb43-6"><a href="#cb43-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">x =</span> trstprt, </span>
<span id="cb43-7"><a href="#cb43-7" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> pred</span>
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
<span id="cb43-8"><a href="#cb43-8" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb43-9"><a href="#cb43-9" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb43-10"><a href="#cb43-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_line</span>(<span class="at">color =</span> <span class="st">"tomato"</span>) <span class="sc">+</span> </span>
<span id="cb43-11"><a href="#cb43-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">data =</span> district12,</span>
<span id="cb43-12"><a href="#cb43-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">y =</span> trstprl),</span>
<span id="cb43-13"><a href="#cb43-13" aria-hidden="true" tabindex="-1"></a> <span class="at">color =</span> <span class="st">"tomato"</span>,</span>
<span id="cb43-14"><a href="#cb43-14" aria-hidden="true" tabindex="-1"></a> <span class="at">position =</span> <span class="st">"jitter"</span>,</span>
<span id="cb43-15"><a href="#cb43-15" aria-hidden="true" tabindex="-1"></a> <span class="at">size =</span> <span class="fl">0.7</span>,</span>
<span id="cb43-16"><a href="#cb43-16" aria-hidden="true" tabindex="-1"></a> <span class="at">alpha =</span> <span class="fl">0.85</span></span>
<span id="cb43-17"><a href="#cb43-17" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span> </span>
<span id="cb43-18"><a href="#cb43-18" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">data =</span> districts,</span>
<span id="cb43-19"><a href="#cb43-19" aria-hidden="true" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">y =</span> trstprl),</span>
<span id="cb43-20"><a href="#cb43-20" aria-hidden="true" tabindex="-1"></a> <span class="at">color =</span> <span class="st">"darkgray"</span>,</span>
<span id="cb43-21"><a href="#cb43-21" aria-hidden="true" tabindex="-1"></a> <span class="at">position =</span> <span class="st">"jitter"</span>,</span>
<span id="cb43-22"><a href="#cb43-22" aria-hidden="true" tabindex="-1"></a> <span class="at">size =</span> <span class="fl">0.7</span>,</span>
<span id="cb43-23"><a href="#cb43-23" aria-hidden="true" tabindex="-1"></a> <span class="at">alpha =</span> <span class="fl">0.3</span></span>
<span id="cb43-24"><a href="#cb43-24" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span> </span>
<span id="cb43-25"><a href="#cb43-25" aria-hidden="true" tabindex="-1"></a> <span class="fu">ylab</span>(<span class="st">"Predicted value of Trust in Parliament"</span>) <span class="sc">+</span></span>
<span id="cb43-26"><a href="#cb43-26" aria-hidden="true" tabindex="-1"></a> <span class="fu">xlab</span>(<span class="st">"Trust in Parties"</span>) <span class="sc">+</span></span>
<span id="cb43-27"><a href="#cb43-27" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">"Linear relationship between trstprl and trstprt (others constant)"</span>,</span>
<span id="cb43-28"><a href="#cb43-28" aria-hidden="true" tabindex="-1"></a> <span class="at">lty =</span> <span class="st">"Distrikte"</span>,</span>
<span id="cb43-29"><a href="#cb43-29" aria-hidden="true" tabindex="-1"></a> <span class="at">caption =</span> <span class="st">"Highlighted District 12."</span></span>
<span id="cb43-30"><a href="#cb43-30" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb43-31"><a href="#cb43-31" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_color_manual</span>(<span class="at">values =</span> <span class="fu">c</span>(<span class="st">"darkgray"</span>, </span>
<span id="cb43-32"><a href="#cb43-32" aria-hidden="true" tabindex="-1"></a> <span class="st">"darkgray"</span>, </span>
<span id="cb43-33"><a href="#cb43-33" aria-hidden="true" tabindex="-1"></a> <span class="st">"darkgray"</span>, </span>
<span id="cb43-34"><a href="#cb43-34" aria-hidden="true" tabindex="-1"></a> <span class="st">"darkgray"</span>, </span>
<span id="cb43-35"><a href="#cb43-35" aria-hidden="true" tabindex="-1"></a> <span class="st">"tomato"</span></span>
<span id="cb43-36"><a href="#cb43-36" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb43-37"><a href="#cb43-37" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb43-38"><a href="#cb43-38" aria-hidden="true" tabindex="-1"></a> <span class="fu">guides</span>(<span class="at">color =</span> <span class="st">"none"</span>)</span></code></pre></div>
<p><img src="ggplot-advanced_files/figure-html/ggplot-districts3-1.png" width="576" style="display: block; margin: auto;" /></p>
<div id="koeffizienten-darstellen" class="section level3">
<h3>Koeffizienten darstellen</h3>
<p>Wenn wir aber das Modell grafisch darstellen wollen, also die Koeffizienten (anstatt oder zusätzlich zu einer Tabelle), hilft das <em>package</em> <code>dotwhisker</code>. Mithilfe dieses <em>packages</em> können Objekte aus <code>lm()</code>-Funktionen direkt geplottet werden.</p>
<p>Zuerst installieren und laden wir das Paket:</p>
<div class="sourceCode" id="cb44"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb44-1"><a href="#cb44-1" aria-hidden="true" tabindex="-1"></a><span class="fu">install.packages</span>(<span class="st">"dotwhisker"</span>)</span>
<span id="cb44-2"><a href="#cb44-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb44-3"><a href="#cb44-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(<span class="st">"dotwhisker"</span>)</span></code></pre></div>
<p>Anschließend ruft man die Funktion <code>dwplot()</code> auf, die die Koeffizienten grafisch darstellt. Dies ist ebenfalls ein <code>ggplot</code>, so dass man es im Anschluss beliebig bearbeiten kann.</p>
<div class="sourceCode" id="cb45"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb45-1"><a href="#cb45-1" aria-hidden="true" tabindex="-1"></a><span class="fu">dwplot</span>(model3)</span></code></pre></div>
<p><img src="ggplot-advanced_files/figure-html/coefplot-1.png" width="576" style="display: block; margin: auto;" /></p>
<p>Das sieht noch etwas unschön aus und wir bearbeiten dies nun in <code>ggplot</code>: Wir fügen bei <strong>0</strong> eine Linie ein (Signifikanz), wir ändern die Achsenbeschriftung auf der y-Achese und wir ändern die Skala auf der x-Achse und fügen Titel ein.</p>
<div class="sourceCode" id="cb46"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb46-1"><a href="#cb46-1" aria-hidden="true" tabindex="-1"></a><span class="fu">dwplot</span>(model3) <span class="sc">+</span></span>
<span id="cb46-2"><a href="#cb46-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_vline</span>(<span class="at">xintercept =</span> <span class="dv">0</span>,</span>
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
<span id="cb46-3"><a href="#cb46-3" aria-hidden="true" tabindex="-1"></a> <span class="at">linetype =</span> <span class="st">"dashed"</span></span>
<span id="cb46-4"><a href="#cb46-4" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb46-5"><a href="#cb46-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_discrete</span>(<span class="at">labels =</span> <span class="fu">rev</span>(<span class="fu">c</span>(<span class="st">"Trust Parties"</span>, </span>
<span id="cb46-6"><a href="#cb46-6" aria-hidden="true" tabindex="-1"></a> <span class="st">"Age"</span>, </span>
<span id="cb46-7"><a href="#cb46-7" aria-hidden="true" tabindex="-1"></a> <span class="st">"Satisfaction w/ Democracy"</span>,</span>
<span id="cb46-8"><a href="#cb46-8" aria-hidden="true" tabindex="-1"></a> <span class="st">"District 5"</span>, </span>
<span id="cb46-9"><a href="#cb46-9" aria-hidden="true" tabindex="-1"></a> <span class="st">"District 7"</span>, </span>
<span id="cb46-10"><a href="#cb46-10" aria-hidden="true" tabindex="-1"></a> <span class="st">"District 10"</span>, </span>
<span id="cb46-11"><a href="#cb46-11" aria-hidden="true" tabindex="-1"></a> <span class="st">"District 12"</span></span>
<span id="cb46-12"><a href="#cb46-12" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb46-13"><a href="#cb46-13" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb46-14"><a href="#cb46-14" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span> </span>
<span id="cb46-15"><a href="#cb46-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">breaks =</span> <span class="fu">seq</span>(<span class="sc">-</span><span class="dv">2</span>, </span>
<span id="cb46-16"><a href="#cb46-16" aria-hidden="true" tabindex="-1"></a> <span class="dv">1</span>,</span>
<span id="cb46-17"><a href="#cb46-17" aria-hidden="true" tabindex="-1"></a> <span class="fl">0.2</span></span>
<span id="cb46-18"><a href="#cb46-18" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb46-19"><a href="#cb46-19" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb46-20"><a href="#cb46-20" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">"Lin. Regression on Trust in Parliament (ref: District 1)"</span>,</span>
<span id="cb46-21"><a href="#cb46-21" aria-hidden="true" tabindex="-1"></a> <span class="at">caption =</span> <span class="st">"Data: Panem Social Survey."</span></span>
<span id="cb46-22"><a href="#cb46-22" aria-hidden="true" tabindex="-1"></a> )</span></code></pre></div>
<p><img src="ggplot-advanced_files/figure-html/coefplot2-1.png" width="576" style="display: block; margin: auto;" /></p>
<div id="das-wars" class="section level4">
<h4>Das war’s!</h4>
<p>Übungsaufgaben findet ihr als <code>task ggplot advanced</code> in der Cloud.</p>
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
</div>
</div>
<br><br>
<hr />
<center>
<p style="font-size:10px">
Copyright: <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/"><img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png" /></a>
<br/>Benedikt Philipp Kleer, Justus-Liebig-Universität Gießen.
</p>
</center>
</div>
</div>
</div>
<script>
// add bootstrap table styles to pandoc tables
function bootstrapStylePandocTables() {
$('tr.odd').parent('tbody').parent('table').addClass('table table-condensed');
}
$(document).ready(function () {
bootstrapStylePandocTables();
});
</script>
<!-- tabsets -->
<script>
$(document).ready(function () {
window.buildTabsets("TOC");
});
$(document).ready(function () {
$('.tabset-dropdown > .nav-tabs > li').click(function () {
$(this).parent().toggleClass('nav-tabs-open');
});
});
</script>
<!-- code folding -->
<script>
$(document).ready(function () {
// temporarily add toc-ignore selector to headers for the consistency with Pandoc
$('.unlisted.unnumbered').addClass('toc-ignore')
// move toc-ignore selectors from section div to header
$('div.section.toc-ignore')
.removeClass('toc-ignore')
.children('h1,h2,h3,h4,h5').addClass('toc-ignore');
// establish options
var options = {
selectors: "h1,h2,h3",
theme: "bootstrap3",
context: '.toc-content',
hashGenerator: function (text) {
return text.replace(/[.\\/?&!#<>]/g, '').replace(/\s/g, '_');
},
ignoreSelector: ".toc-ignore",
scrollTo: 0
};
options.showAndHide = true;
options.smoothScroll = true;
// tocify
var toc = $("#TOC").tocify(options).data("toc-tocify");
});
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>