{"id":471,"date":"2023-07-29T19:19:54","date_gmt":"2023-07-29T19:19:54","guid":{"rendered":"https:\/\/statorials.org\/de\/heatmap-r-ggplot2\/"},"modified":"2023-07-29T19:19:54","modified_gmt":"2023-07-29T19:19:54","slug":"heatmap-r-ggplot2","status":"publish","type":"post","link":"https:\/\/statorials.org\/de\/heatmap-r-ggplot2\/","title":{"rendered":"So erstellen sie eine heatmap in r mit ggplot2"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">In diesem Tutorial wird erl\u00e4utert, wie Sie mit ggplot2 eine Heatmap in R erstellen.<\/span><\/p>\n<h3> <strong><span style=\"color: #000000;\">Beispiel: Erstellen einer Heatmap in R<\/span><\/strong><\/h3>\n<p> <span style=\"color: #000000;\">Um eine Heatmap zu erstellen, verwenden wir den integrierten R-Datensatz <strong>mtcars<\/strong> .<\/span><\/p>\n<pre style=\"background-color: #e5e5e5; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#view first six rows of <em>mtcars\n<\/em><\/span>head(mtcars)\n\n# mpg cyl disp hp drat wt qsec vs am gear carb\n#Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4\n#Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4\n#Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1\n#Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1\n#Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2\n#Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Derzeit liegt <strong>mtcars<\/strong> in einem breiten Format vor, aber wir m\u00fcssen es in ein langes Format \u00fcberf\u00fchren, um die Heatmap zu erstellen.<\/span><\/p>\n<pre style=\"background-color: #e5e5e5; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#load <em>reshape2<\/em> package to use melt() function<\/span>\nlibrary(reshape2)\n\n<span style=\"color: #008080;\">#melt mtcars into long format<\/span>\nmelt_mtcars &lt;- melt(mtcars)\n\n<span style=\"color: #008080;\">#add column for car name<\/span>\nmelt_mtcars$car &lt;- rep(row.names(mtcars), 11)\n\n<span style=\"color: #008080;\">#view first six rows of <em>melt_mtcars<\/em><\/span>\nhead(melt_mtcars)\n\n# variable value char\n#1 mpg 21.0 Mazda RX4\n#2 mpg 21.0 Mazda RX4 Wag\n#3 mpg 22.8 Datsun 710\n#4 mpg 21.4 Hornet 4 Drive\n#5 mpg 18.7 Hornet Sportabout\n#6 mpg 18.1 Valiant<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Wir k\u00f6nnen den folgenden Code verwenden, um die Heatmap in ggplot2 zu erstellen:<\/span><\/p>\n<pre style=\"background-color: #e5e5e5; font-size: 15px;\"> <strong>library(ggplot2)\n\nggplot(melt_mtcars, aes(variable, char)) +\n  geom_tile(aes(fill = value),<\/strong> <strong>color = \"white\") +\n  scale_fill_gradient(low = \"white\",<\/strong> <strong>high = \"red\")<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Da die Werte von <em>disp<\/em> viel gr\u00f6\u00dfer sind als die Werte aller anderen Variablen im Datenrahmen, ist es leider schwierig, die Farbvariation der anderen Variablen zu erkennen.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Eine M\u00f6glichkeit, dieses Problem zu l\u00f6sen, besteht darin, die Werte jeder Variablen mithilfe der Funktion <strong>rescale()<\/strong> im Paket scales() und der Funktion <strong>ddply()<\/strong> im Paket plyr() von 0 auf 1 neu zu skalieren:<\/span><\/p>\n<pre style=\"background-color: #e5e5e5; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#load libraries<\/span>\nlibrary(plyr)\nlibrary(scales)\n\n<span style=\"color: #008080;\">#rescale values for all variables in melted data frame<\/span>\nmelt_mtcars &lt;- ddply(melt_mtcars, .(variable), transform, rescale = rescale(value))\n\n<span style=\"color: #008080;\">#create heatmap using rescaled values<\/span>\nggplot(melt_mtcars, aes(variable, char)) +\n  geom_tile(aes(fill = rescale), color = \"white\") +\n  scale_fill_gradient(low = \"white\", high = \"red\")\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Wir k\u00f6nnen die Heatmap-Farben auch \u00e4ndern, indem wir die im Argument \u201escale_fill_gradient()\u201c verwendeten Farben \u00e4ndern:<\/span><\/p>\n<pre style=\"background-color: #e5e5e5; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#create heatmap using blue color scale\n<\/span>ggplot(melt_mtcars, aes(variable, char)) +\n  geom_tile(aes(fill = rescale), color = \"white\") +\n  scale_fill_gradient(low = \"white\", high = \"steelblue\")<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Beachten Sie, dass die Heatmap derzeit nach Fahrzeugnamen kategorisiert ist. Stattdessen k\u00f6nnten wir die Heatmap nach den Werten einer der Variablen wie <em>mpg<\/em> ordnen, indem wir den folgenden Code verwenden:<\/span><\/p>\n<pre style=\"background-color: #e5e5e5; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#define car name as a new column, then order by <em>mpg<\/em> descending\n<\/span>mtcars$car &lt;- row.names(mtcars)\nmtcars$car &lt;- with(mtcars, reorder(car, mpg))\n\n<span style=\"color: #008080;\">#melt mtcars into long format\n<\/span>melt_mtcars &lt;- melt(mtcars)\n\n<span style=\"color: #008080;\">#rescale values for all variables in melted data frame\n<\/span>melt_mtcars &lt;- ddply(melt_mtcars, .(variable), transform, rescale = rescale(value))\n\n<span style=\"color: #008080;\">#create heatmap using rescaled values\n<\/span>ggplot(melt_mtcars, aes(variable, char)) +\n  geom_tile(aes(fill = rescale), color = \"white\") +\n  scale_fill_gradient(low = \"white\", high = \"steelblue\")<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Um die Heatmap durch Erh\u00f6hen <em>von mpg<\/em> zu sortieren, verwenden Sie einfach <strong>-mpg<\/strong> im reorder()-Argument:<\/span><\/p>\n<pre style=\"background-color: #e5e5e5; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#define car name as a new column, then order by mpg descending\n<\/span>mtcars$car &lt;- row.names(mtcars)\nmtcars$car &lt;- with(mtcars, reorder(car, <span style=\"color: #800080;\">-mpg<\/span> ))\n\n<span style=\"color: #008080;\">#melt mtcars into long format\n<\/span>melt_mtcars &lt;- melt(mtcars)\n\n<span style=\"color: #008080;\">#rescale values for all variables in melted data frame\n<\/span>melt_mtcars &lt;- ddply(melt_mtcars, .(variable), transform, rescale = rescale(value))\n\n<span style=\"color: #008080;\">#create heatmap using rescaled values\n<\/span>ggplot(melt_mtcars, aes(variable, char)) +\n  geom_tile(aes(fill = rescale), color = \"white\") +\n  scale_fill_gradient(low = \"white\", high = \"steelblue\")<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Schlie\u00dflich k\u00f6nnen wir die Beschriftungen der x- und y-Achse sowie die Legende entfernen, wenn uns das Aussehen nicht gef\u00e4llt, indem wir die Argumente labs() und theme() verwenden:<\/span><\/p>\n<pre style=\"background-color: #e5e5e5; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#create heatmap with no axis labels or legend\n<\/span>ggplot(melt_mtcars, aes(variable, char)) +\n  geom_tile(aes(fill = rescale), color = \"white\") +\n  scale_fill_gradient(low = \"white\", high = \"steelblue\") +\n  <span style=\"color: #800080;\">labs(x = \"\", y = \"\") +\n  theme(legend.position = \"none\")<\/span><\/strong><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In diesem Tutorial wird erl\u00e4utert, wie Sie mit ggplot2 eine Heatmap in R erstellen. Beispiel: Erstellen einer Heatmap in R Um eine Heatmap zu erstellen, verwenden wir den integrierten R-Datensatz mtcars . #view first six rows of mtcars head(mtcars) # mpg cyl disp hp drat wt qsec vs am gear carb #Mazda RX4 21.0 6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>So erstellen Sie eine W\u00e4rmekarte in R mit ggplot2 \u2013 Statorials<\/title>\n<meta name=\"description\" content=\"In diesem Tutorial wird erl\u00e4utert, wie Sie mit ggplot2 eine Heatmap in R erstellen.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/statorials.org\/de\/heatmap-r-ggplot2\/\" \/>\n<meta property=\"og:locale\" content=\"de_DE\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"So erstellen Sie eine W\u00e4rmekarte in R mit ggplot2 \u2013 Statorials\" \/>\n<meta property=\"og:description\" content=\"In diesem Tutorial wird erl\u00e4utert, wie Sie mit ggplot2 eine Heatmap in R erstellen.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/de\/heatmap-r-ggplot2\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-29T19:19:54+00:00\" \/>\n<meta name=\"author\" content=\"Dr. Benjamin Anderson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Verfasst von\" \/>\n\t<meta name=\"twitter:data1\" content=\"Dr. Benjamin Anderson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Gesch\u00e4tzte Lesezeit\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 Minuten\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/de\/heatmap-r-ggplot2\/\",\"url\":\"https:\/\/statorials.org\/de\/heatmap-r-ggplot2\/\",\"name\":\"So erstellen Sie eine W\u00e4rmekarte in R mit ggplot2 \u2013 Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/de\/#website\"},\"datePublished\":\"2023-07-29T19:19:54+00:00\",\"dateModified\":\"2023-07-29T19:19:54+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/de\/#\/schema\/person\/ec75c4d6365f2708f8a0ad3a42121aa0\"},\"description\":\"In diesem Tutorial wird erl\u00e4utert, wie Sie mit ggplot2 eine Heatmap in R erstellen.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/de\/heatmap-r-ggplot2\/#breadcrumb\"},\"inLanguage\":\"de-DE\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/de\/heatmap-r-ggplot2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/de\/heatmap-r-ggplot2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Heim\",\"item\":\"https:\/\/statorials.org\/de\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"So erstellen sie eine heatmap in r mit ggplot2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/statorials.org\/de\/#website\",\"url\":\"https:\/\/statorials.org\/de\/\",\"name\":\"Statorials\",\"description\":\"Ihr Leitfaden f\u00fcr statistische Kompetenz !\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/statorials.org\/de\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"de-DE\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/statorials.org\/de\/#\/schema\/person\/ec75c4d6365f2708f8a0ad3a42121aa0\",\"name\":\"Dr. Benjamin Anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"de-DE\",\"@id\":\"https:\/\/statorials.org\/de\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/statorials.org\/de\/wp-content\/uploads\/2023\/11\/Benjamin-Anderson-96x96.jpg\",\"contentUrl\":\"https:\/\/statorials.org\/de\/wp-content\/uploads\/2023\/11\/Benjamin-Anderson-96x96.jpg\",\"caption\":\"Dr. Benjamin Anderson\"},\"description\":\"Hallo, ich bin Benjamin, ein pensionierter Statistikprofessor, der sich zum engagierten Statorials-Lehrer entwickelt hat. Mit umfassender Erfahrung und Fachwissen auf dem Gebiet der Statistik bin ich bestrebt, mein Wissen zu teilen, um Studenten durch Statorials zu bef\u00e4higen. Mehr wissen\",\"sameAs\":[\"https:\/\/statorials.org\/de\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"So erstellen Sie eine W\u00e4rmekarte in R mit ggplot2 \u2013 Statorials","description":"In diesem Tutorial wird erl\u00e4utert, wie Sie mit ggplot2 eine Heatmap in R erstellen.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/statorials.org\/de\/heatmap-r-ggplot2\/","og_locale":"de_DE","og_type":"article","og_title":"So erstellen Sie eine W\u00e4rmekarte in R mit ggplot2 \u2013 Statorials","og_description":"In diesem Tutorial wird erl\u00e4utert, wie Sie mit ggplot2 eine Heatmap in R erstellen.","og_url":"https:\/\/statorials.org\/de\/heatmap-r-ggplot2\/","og_site_name":"Statorials","article_published_time":"2023-07-29T19:19:54+00:00","author":"Dr. Benjamin Anderson","twitter_card":"summary_large_image","twitter_misc":{"Verfasst von":"Dr. Benjamin Anderson","Gesch\u00e4tzte Lesezeit":"3 Minuten"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/de\/heatmap-r-ggplot2\/","url":"https:\/\/statorials.org\/de\/heatmap-r-ggplot2\/","name":"So erstellen Sie eine W\u00e4rmekarte in R mit ggplot2 \u2013 Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/de\/#website"},"datePublished":"2023-07-29T19:19:54+00:00","dateModified":"2023-07-29T19:19:54+00:00","author":{"@id":"https:\/\/statorials.org\/de\/#\/schema\/person\/ec75c4d6365f2708f8a0ad3a42121aa0"},"description":"In diesem Tutorial wird erl\u00e4utert, wie Sie mit ggplot2 eine Heatmap in R erstellen.","breadcrumb":{"@id":"https:\/\/statorials.org\/de\/heatmap-r-ggplot2\/#breadcrumb"},"inLanguage":"de-DE","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/de\/heatmap-r-ggplot2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/de\/heatmap-r-ggplot2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Heim","item":"https:\/\/statorials.org\/de\/"},{"@type":"ListItem","position":2,"name":"So erstellen sie eine heatmap in r mit ggplot2"}]},{"@type":"WebSite","@id":"https:\/\/statorials.org\/de\/#website","url":"https:\/\/statorials.org\/de\/","name":"Statorials","description":"Ihr Leitfaden f\u00fcr statistische Kompetenz !","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/statorials.org\/de\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"de-DE"},{"@type":"Person","@id":"https:\/\/statorials.org\/de\/#\/schema\/person\/ec75c4d6365f2708f8a0ad3a42121aa0","name":"Dr. Benjamin Anderson","image":{"@type":"ImageObject","inLanguage":"de-DE","@id":"https:\/\/statorials.org\/de\/#\/schema\/person\/image\/","url":"https:\/\/statorials.org\/de\/wp-content\/uploads\/2023\/11\/Benjamin-Anderson-96x96.jpg","contentUrl":"https:\/\/statorials.org\/de\/wp-content\/uploads\/2023\/11\/Benjamin-Anderson-96x96.jpg","caption":"Dr. Benjamin Anderson"},"description":"Hallo, ich bin Benjamin, ein pensionierter Statistikprofessor, der sich zum engagierten Statorials-Lehrer entwickelt hat. Mit umfassender Erfahrung und Fachwissen auf dem Gebiet der Statistik bin ich bestrebt, mein Wissen zu teilen, um Studenten durch Statorials zu bef\u00e4higen. Mehr wissen","sameAs":["https:\/\/statorials.org\/de"]}]}},"yoast_meta":{"yoast_wpseo_title":"","yoast_wpseo_metadesc":"","yoast_wpseo_canonical":""},"_links":{"self":[{"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/posts\/471"}],"collection":[{"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/comments?post=471"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/posts\/471\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/media?parent=471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/categories?post=471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/tags?post=471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}