{"id":3792,"date":"2023-07-15T12:43:29","date_gmt":"2023-07-15T12:43:29","guid":{"rendered":"https:\/\/statorials.org\/pt\/grafico-residual-ggplot2\/"},"modified":"2023-07-15T12:43:29","modified_gmt":"2023-07-15T12:43:29","slug":"grafico-residual-ggplot2","status":"publish","type":"post","link":"https:\/\/statorials.org\/pt\/grafico-residual-ggplot2\/","title":{"rendered":"Como criar um gr\u00e1fico residual em ggplot2 (com exemplo)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\"><strong>Os gr\u00e1ficos de res\u00edduos<\/strong> s\u00e3o usados para avaliar se os <a href=\"https:\/\/statorials.org\/pt\/residuo\/\" target=\"_blank\" rel=\"noopener\">res\u00edduos<\/a> de um modelo de regress\u00e3o s\u00e3o normalmente distribu\u00eddos e se apresentam ou n\u00e3o <a href=\"https:\/\/statorials.org\/pt\/regressao-de-heterocedasticidade\/\" target=\"_blank\" rel=\"noopener\">heterocedasticidade<\/a> .<\/span><\/p>\n<p> <span style=\"color: #000000;\">Para criar um gr\u00e1fico residual em ggplot2, voc\u00ea pode usar a seguinte sintaxe b\u00e1sica:<\/span><\/p>\n<pre style=\"background-color: #e5e5e5; font-size: 15px;\"> <strong><span style=\"color: #008000;\">library<\/span> (ggplot2)\n\nggplot(model, aes(x = .fitted, y = .resid)) +\n  geom_point() +\n  geom_hline(yintercept = <span style=\"color: #008000;\">0<\/span> )\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">O exemplo a seguir mostra como usar essa sintaxe na pr\u00e1tica.<\/span><\/p>\n<h2> <strong>Exemplo: Criando um gr\u00e1fico residual em ggplot2<\/strong><\/h2>\n<p> <span style=\"color: #000000;\">Para este exemplo, usaremos o conjunto de dados <a href=\"https:\/\/statorials.org\/pt\/conjunto-de-dados-mtcars-r\/\" target=\"_blank\" rel=\"noopener\">mtcars<\/a> integrado ao R:<\/span><\/p>\n<pre style=\"background-color: #e5e5e5; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#view first six rows of mtcars dataset<\/span>\nhead(mtcars)\n                   mpg cyl disp hp drat wt qsec vs am gear carb\nMazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4\nMazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4\nDatsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1\nHornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1\nHornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2\nValiant 18.1 6 225 105 2.76 3,460 20.22 1 0 3 1<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Primeiro, ajustaremos um modelo de regress\u00e3o usando <strong>mpg<\/strong> como vari\u00e1vel de resposta e <b>qsec<\/b> como vari\u00e1vel preditora:<\/span><\/p>\n<pre style=\"background-color: #e5e5e5; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#fit regression model<\/span>\nmodel &lt;- lm(mpg ~ qsec, data=mtcars)<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">A seguir, usaremos a seguinte sintaxe para criar um gr\u00e1fico residual em ggplot2:<\/span> <\/p>\n<pre style=\"background-color: #e5e5e5; font-size: 15px;\"> <strong><span style=\"color: #008000;\">library<\/span> (ggplot2)\n\n<span style=\"color: #008080;\">#create residual plot\n<\/span>ggplot(model, aes(x = .fitted, y = .resid)) +\n  geom_point() +\n  geom_hline(yintercept = <span style=\"color: #008000;\">0<\/span> )<\/strong> <\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-30786\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/ajustement1.jpg\" alt=\"gr\u00e1fico residual em ggplot2\" width=\"569\" height=\"440\" srcset=\"\" sizes=\"auto, \"><\/p>\n<p> <span style=\"color: #000000;\">O eixo x exibe os valores ajustados e o eixo y exibe os res\u00edduos.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Os res\u00edduos parecem estar espalhados aleatoriamente em torno de zero, sem nenhum padr\u00e3o claro, indicando que a suposi\u00e7\u00e3o de homocedasticidade \u00e9 satisfeita.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Em outras palavras, os <a href=\"https:\/\/statorials.org\/pt\/como-interpretar-coeficientes-de-regressao\/\" target=\"_blank\" rel=\"noopener\">coeficientes<\/a> do modelo de regress\u00e3o devem ser confi\u00e1veis e n\u00e3o precisamos realizar nenhuma <a href=\"https:\/\/statorials.org\/pt\/transformar-dados-em-r\/\" target=\"_blank\" rel=\"noopener\">transforma\u00e7\u00e3o<\/a> nos dados.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Observe tamb\u00e9m que poder\u00edamos usar a fun\u00e7\u00e3o <strong>labs()<\/strong> para adicionar um t\u00edtulo e r\u00f3tulos de eixo ao gr\u00e1fico residual:<\/span> <\/p>\n<pre style=\"background-color: #e5e5e5; font-size: 15px;\"> <strong><span style=\"color: #008000;\">library<\/span> (ggplot2)\n\n<span style=\"color: #008080;\">#create residual plot with title and axis labels\n<\/span>ggplot(model, aes(x = .fitted, y = .resid)) +\n  geom_point() +\n  geom_hline(yintercept = <span style=\"color: #008000;\">0<\/span> ) +\n  labs(title=' <span style=\"color: #ff0000;\">Residual vs. Fitted Values Plot<\/span> ', x=' <span style=\"color: #ff0000;\">Fitted Values<\/span> ', y=' <span style=\"color: #ff0000;\">Residuals<\/span> ')<\/strong> <\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-30787\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/ajustement2.jpg\" alt=\"ggplot2 plotando res\u00edduos versus valores ajustados com r\u00f3tulos de eixo\" width=\"532\" height=\"406\" srcset=\"\" sizes=\"auto, \"><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Recursos adicionais<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Os tutoriais a seguir explicam como realizar outras tarefas comuns em R:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/pt\/residuos-padronizados-em-r\/\" target=\"_blank\" rel=\"noopener\">Como calcular res\u00edduos padronizados em R<\/a><br \/> <a href=\"https:\/\/statorials.org\/pt\/residuos-em-r-estudantil\/\" target=\"_blank\" rel=\"noopener\">Como calcular res\u00edduos estudantis em R<\/a><br \/> <a href=\"https:\/\/statorials.org\/pt\/histograma-de-residuos-em-r\/\" target=\"_blank\" rel=\"noopener\">Como criar um histograma de res\u00edduos em R<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Os gr\u00e1ficos de res\u00edduos s\u00e3o usados para avaliar se os res\u00edduos de um modelo de regress\u00e3o s\u00e3o normalmente distribu\u00eddos e se apresentam ou n\u00e3o heterocedasticidade . Para criar um gr\u00e1fico residual em ggplot2, voc\u00ea pode usar a seguinte sintaxe b\u00e1sica: library (ggplot2) ggplot(model, aes(x = .fitted, y = .resid)) + geom_point() + geom_hline(yintercept = 0 [&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":[],"class_list":["post-3792","post","type-post","status-publish","format-standard","hentry","category-guia"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Como criar um gr\u00e1fico residual no ggplot2 (com exemplo) - Statorials<\/title>\n<meta name=\"description\" content=\"Este tutorial explica como criar um gr\u00e1fico residual em ggplot2, com um exemplo.\" \/>\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\/pt\/grafico-residual-ggplot2\/\" \/>\n<meta property=\"og:locale\" content=\"pt_PT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Como criar um gr\u00e1fico residual no ggplot2 (com exemplo) - Statorials\" \/>\n<meta property=\"og:description\" content=\"Este tutorial explica como criar um gr\u00e1fico residual em ggplot2, com um exemplo.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/pt\/grafico-residual-ggplot2\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-15T12:43:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/ajustement1.jpg\" \/>\n<meta name=\"author\" content=\"Dr. benjamim anderson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Escrito por\" \/>\n\t<meta name=\"twitter:data1\" content=\"Dr. benjamim anderson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Tempo estimado de leitura\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutos\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/pt\/grafico-residual-ggplot2\/\",\"url\":\"https:\/\/statorials.org\/pt\/grafico-residual-ggplot2\/\",\"name\":\"Como criar um gr\u00e1fico residual no ggplot2 (com exemplo) - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/pt\/#website\"},\"datePublished\":\"2023-07-15T12:43:29+00:00\",\"dateModified\":\"2023-07-15T12:43:29+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666\"},\"description\":\"Este tutorial explica como criar um gr\u00e1fico residual em ggplot2, com um exemplo.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/pt\/grafico-residual-ggplot2\/#breadcrumb\"},\"inLanguage\":\"pt-PT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/pt\/grafico-residual-ggplot2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/pt\/grafico-residual-ggplot2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Lar\",\"item\":\"https:\/\/statorials.org\/pt\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Como criar um gr\u00e1fico residual em ggplot2 (com exemplo)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/statorials.org\/pt\/#website\",\"url\":\"https:\/\/statorials.org\/pt\/\",\"name\":\"Statorials\",\"description\":\"O seu guia para a literacia estat\u00edstica!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/statorials.org\/pt\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"pt-PT\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666\",\"name\":\"Dr. benjamim anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"pt-PT\",\"@id\":\"https:\/\/statorials.org\/pt\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/statorials.org\/pt\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"contentUrl\":\"https:\/\/statorials.org\/pt\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"caption\":\"Dr. benjamim anderson\"},\"description\":\"Ol\u00e1, sou Benjamin, um professor aposentado de estat\u00edstica que se tornou professor dedicado na Statorials. Com vasta experi\u00eancia e conhecimento na \u00e1rea de estat\u00edstica, estou empenhado em compartilhar meu conhecimento para capacitar os alunos por meio de Statorials. Saber mais\",\"sameAs\":[\"https:\/\/statorials.org\/pt\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Como criar um gr\u00e1fico residual no ggplot2 (com exemplo) - Statorials","description":"Este tutorial explica como criar um gr\u00e1fico residual em ggplot2, com um exemplo.","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\/pt\/grafico-residual-ggplot2\/","og_locale":"pt_PT","og_type":"article","og_title":"Como criar um gr\u00e1fico residual no ggplot2 (com exemplo) - Statorials","og_description":"Este tutorial explica como criar um gr\u00e1fico residual em ggplot2, com um exemplo.","og_url":"https:\/\/statorials.org\/pt\/grafico-residual-ggplot2\/","og_site_name":"Statorials","article_published_time":"2023-07-15T12:43:29+00:00","og_image":[{"url":"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/ajustement1.jpg"}],"author":"Dr. benjamim anderson","twitter_card":"summary_large_image","twitter_misc":{"Escrito por":"Dr. benjamim anderson","Tempo estimado de leitura":"2 minutos"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/pt\/grafico-residual-ggplot2\/","url":"https:\/\/statorials.org\/pt\/grafico-residual-ggplot2\/","name":"Como criar um gr\u00e1fico residual no ggplot2 (com exemplo) - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/pt\/#website"},"datePublished":"2023-07-15T12:43:29+00:00","dateModified":"2023-07-15T12:43:29+00:00","author":{"@id":"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666"},"description":"Este tutorial explica como criar um gr\u00e1fico residual em ggplot2, com um exemplo.","breadcrumb":{"@id":"https:\/\/statorials.org\/pt\/grafico-residual-ggplot2\/#breadcrumb"},"inLanguage":"pt-PT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/pt\/grafico-residual-ggplot2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/pt\/grafico-residual-ggplot2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Lar","item":"https:\/\/statorials.org\/pt\/"},{"@type":"ListItem","position":2,"name":"Como criar um gr\u00e1fico residual em ggplot2 (com exemplo)"}]},{"@type":"WebSite","@id":"https:\/\/statorials.org\/pt\/#website","url":"https:\/\/statorials.org\/pt\/","name":"Statorials","description":"O seu guia para a literacia estat\u00edstica!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/statorials.org\/pt\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"pt-PT"},{"@type":"Person","@id":"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666","name":"Dr. benjamim anderson","image":{"@type":"ImageObject","inLanguage":"pt-PT","@id":"https:\/\/statorials.org\/pt\/#\/schema\/person\/image\/","url":"https:\/\/statorials.org\/pt\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","contentUrl":"https:\/\/statorials.org\/pt\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","caption":"Dr. benjamim anderson"},"description":"Ol\u00e1, sou Benjamin, um professor aposentado de estat\u00edstica que se tornou professor dedicado na Statorials. Com vasta experi\u00eancia e conhecimento na \u00e1rea de estat\u00edstica, estou empenhado em compartilhar meu conhecimento para capacitar os alunos por meio de Statorials. Saber mais","sameAs":["https:\/\/statorials.org\/pt"]}]}},"yoast_meta":{"yoast_wpseo_title":"","yoast_wpseo_metadesc":"","yoast_wpseo_canonical":""},"_links":{"self":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/posts\/3792","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/comments?post=3792"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/posts\/3792\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/media?parent=3792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/categories?post=3792"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/tags?post=3792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}