{"id":4285,"date":"2023-07-12T07:14:31","date_gmt":"2023-07-12T07:14:31","guid":{"rendered":"https:\/\/statorials.org\/it\/geom_abline-r\/"},"modified":"2023-07-12T07:14:31","modified_gmt":"2023-07-12T07:14:31","slug":"geom_abline-r","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/geom_abline-r\/","title":{"rendered":"Come utilizzare geom_abline per aggiungere linee rette in ggplot2"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Puoi utilizzare la funzione <strong>geom_abline()<\/strong> e altre funzioni <strong>geom<\/strong> simili per aggiungere linee rette ai grafici in ggplot2.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Ecco i modi pi\u00f9 comuni per utilizzare queste funzioni:<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Metodo 1: utilizzare geom_abline() per aggiungere una linea con pendenza e intersezione<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong>ggplot(df, aes(x, y)) +\n  geom_point() +\n  geom_abline(slope= <span style=\"color: #008000;\">3<\/span> , intercept= <span style=\"color: #008000;\">15<\/span> )\n<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\"><strong>Metodo 2: utilizzare geom_vline() per aggiungere una linea verticale<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>ggplot(df, aes(x=xvar, y=yvar)) +\n    geom_point() +\n    geom_vline(xintercept= <span style=\"color: #008000;\">5<\/span> )<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><strong>Metodo 3: utilizzare geom_hline() per aggiungere una linea orizzontale<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>ggplot(df, aes(x=xvar, y=yvar)) +\n    geom_point() +\n    geom_hline(yintercept= <span style=\"color: #008000;\">25<\/span> )<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><strong>Metodo 4: utilizzare geom_smooth() per aggiungere una linea di regressione<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>ggplot(df, aes(x=xvar, y=yvar)) +\n    geom_point() +\n    geom_smooth(method=' <span style=\"color: #ff0000;\">lm<\/span> ')<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Gli esempi seguenti mostrano come utilizzare nella pratica ciascuno di questi metodi con il seguente frame di dati in R:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#create data frame<\/span>\ndf &lt;- data. <span style=\"color: #3366ff;\">frame<\/span> (x=c(1, 2, 3, 3, 5, 7, 9),\n                 y=c(8, 14, 18, 25, 29, 33, 25))\n\n<span style=\"color: #008080;\">#view data frame\n<\/span>df\n\n  xy\n1 1 8\n2 2 14\n3 3 18\n4 3 25\n5 5 29\n6 7 33\n7 9 25<\/strong><\/pre>\n<h2> <span style=\"color: #000000;\"><strong>Esempio 1: utilizzare geom_abline() per aggiungere una linea con pendenza e intersezione<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come utilizzare <strong>geom_abline()<\/strong> per aggiungere una linea retta a un grafico a dispersione con una pendenza pari a 3 e un&#8217;intercetta y pari a 15:<\/span> <\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">library<\/span> (ggplot2)\n\n<span style=\"color: #008080;\">#create scatterplot and add straight line with specific slope and intercept\n<\/span>ggplot(df, aes(x=x, y=y)) +\n  geom_point() +\n  geom_abline(slope= <span style=\"color: #008000;\">3<\/span> , intercept= <span style=\"color: #008000;\">15<\/span> )<\/strong> <\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-33751\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/geom1.jpg\" alt=\"geom_abline in ggplot2\" width=\"492\" height=\"489\" srcset=\"\" sizes=\"\"><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Esempio 2: utilizzare geom_vline() per aggiungere una linea verticale<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come utilizzare <strong>geom_vline()<\/strong> per aggiungere una linea verticale a un grafico a dispersione in x=5:<\/span> <\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">library<\/span> (ggplot2)\n\n<span style=\"color: #008080;\">#create scatterplot and add vertical line at x=5\n<\/span>ggplot(df, aes(x=x, y=y)) +\n  geom_point() +\n  geom_vline(xintercept= <span style=\"color: #008000;\">5<\/span> )<\/strong> <\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-33752\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/geom2.jpg\" alt=\"funzione geom_vline nell'esempio R\" width=\"495\" height=\"495\" srcset=\"\" sizes=\"\"><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Esempio 3: utilizzare geom_hline() per aggiungere una linea orizzontale<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come utilizzare <strong>geom_hline()<\/strong> per aggiungere una linea orizzontale a un grafico a dispersione in y=25:<\/span> <\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">library<\/span> (ggplot2)\n\n<span style=\"color: #008080;\">#create scatterplot and add horizontal line at y=25\n<\/span>ggplot(df, aes(x=x, y=y)) +\n  geom_point() +\n  geom_hline(yintercept= <span style=\"color: #008000;\">25<\/span> )<\/strong> <\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-33753\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/geom3.jpg\" alt=\"esempio geom_hline in ggplot\" width=\"485\" height=\"489\" srcset=\"\" sizes=\"\"><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Esempio 4: utilizzare geom_smooth() per aggiungere una linea di regressione<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come utilizzare <strong>geom_smooth()<\/strong> per aggiungere una linea di regressione adattata a un grafico a dispersione:<\/span> <\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">library<\/span> (ggplot2)\n\n<span style=\"color: #008080;\">#create scatterplot and add fitted regression line\n<\/span>ggplot(df, aes(x=x, y=y)) +\n  geom_point() +\n  geom_smooth(method=' <span style=\"color: #ff0000;\">lm<\/span> ', se= <span style=\"color: #008000;\">FALSE<\/span> )<\/strong> <\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-33754\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/geom4.jpg\" alt=\"geom_smooth per aggiungere la linea di regressione nell'esempio ggplot2\" width=\"488\" height=\"488\" srcset=\"\" sizes=\"\"><\/p>\n<p> <span style=\"color: #000000;\"><strong>Nota<\/strong> : l&#8217;argomento <strong>se=FALSE<\/strong> indica a ggplot2 di non visualizzare le linee ombreggiate per le stime dell&#8217;errore standard.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Risorse addizionali<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">I seguenti tutorial spiegano come eseguire altre operazioni comunemente utilizzate in ggplot2:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/it\/spessore-della-linea-ggplot\/\" target=\"_blank\" rel=\"noopener\">Come regolare lo spessore della linea in ggplot2<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/imposta-i-limiti-dellasse-ggplot2\/\" target=\"_blank\" rel=\"noopener\">Come impostare i limiti degli assi in ggplot2<\/a><br \/> Come rimuovere la griglia in ggplot2<br \/> <a href=\"https:\/\/statorials.org\/it\/colore-di-sfondo-di-ggplot\/\" target=\"_blank\" rel=\"noopener\">Come cambiare il colore di sfondo in ggplot2<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Puoi utilizzare la funzione geom_abline() e altre funzioni geom simili per aggiungere linee rette ai grafici in ggplot2. Ecco i modi pi\u00f9 comuni per utilizzare queste funzioni: Metodo 1: utilizzare geom_abline() per aggiungere una linea con pendenza e intersezione ggplot(df, aes(x, y)) + geom_point() + geom_abline(slope= 3 , intercept= 15 ) Metodo 2: utilizzare geom_vline() [&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>Come utilizzare geom_abline per aggiungere linee rette in ggplot2 - Stology<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come utilizzare geom_abline() per aggiungere linee rette ai grafici creati utilizzando ggplot2 in R, inclusi esempi.\" \/>\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\/it\/geom_abline-r\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come utilizzare geom_abline per aggiungere linee rette in ggplot2 - Stology\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come utilizzare geom_abline() per aggiungere linee rette ai grafici creati utilizzando ggplot2 in R, inclusi esempi.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/geom_abline-r\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-12T07:14:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/geom1.jpg\" \/>\n<meta name=\"author\" content=\"Benjamin anderson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Benjamin anderson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minuti\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/it\/geom_abline-r\/\",\"url\":\"https:\/\/statorials.org\/it\/geom_abline-r\/\",\"name\":\"Come utilizzare geom_abline per aggiungere linee rette in ggplot2 - Stology\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-12T07:14:31+00:00\",\"dateModified\":\"2023-07-12T07:14:31+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come utilizzare geom_abline() per aggiungere linee rette ai grafici creati utilizzando ggplot2 in R, inclusi esempi.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/geom_abline-r\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/geom_abline-r\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/geom_abline-r\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come utilizzare geom_abline per aggiungere linee rette in ggplot2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/statorials.org\/it\/#website\",\"url\":\"https:\/\/statorials.org\/it\/\",\"name\":\"Statorials\",\"description\":\"La tua guida all&#039;alfabetizzazione statistica!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/statorials.org\/it\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"it-IT\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\",\"name\":\"Benjamin anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"it-IT\",\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"contentUrl\":\"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"caption\":\"Benjamin anderson\"},\"description\":\"Ciao, sono Benjamin, un professore di statistica in pensione diventato insegnante dedicato di Statorials. Con una vasta esperienza e competenza nel campo della statistica, sono ansioso di condividere le mie conoscenze per potenziare gli studenti attraverso Statorials. Scopri di pi\u00f9\",\"sameAs\":[\"https:\/\/statorials.org\/it\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Come utilizzare geom_abline per aggiungere linee rette in ggplot2 - Stology","description":"Questo tutorial spiega come utilizzare geom_abline() per aggiungere linee rette ai grafici creati utilizzando ggplot2 in R, inclusi esempi.","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\/it\/geom_abline-r\/","og_locale":"it_IT","og_type":"article","og_title":"Come utilizzare geom_abline per aggiungere linee rette in ggplot2 - Stology","og_description":"Questo tutorial spiega come utilizzare geom_abline() per aggiungere linee rette ai grafici creati utilizzando ggplot2 in R, inclusi esempi.","og_url":"https:\/\/statorials.org\/it\/geom_abline-r\/","og_site_name":"Statorials","article_published_time":"2023-07-12T07:14:31+00:00","og_image":[{"url":"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/geom1.jpg"}],"author":"Benjamin anderson","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Benjamin anderson","Est. reading time":"2 minuti"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/it\/geom_abline-r\/","url":"https:\/\/statorials.org\/it\/geom_abline-r\/","name":"Come utilizzare geom_abline per aggiungere linee rette in ggplot2 - Stology","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-12T07:14:31+00:00","dateModified":"2023-07-12T07:14:31+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come utilizzare geom_abline() per aggiungere linee rette ai grafici creati utilizzando ggplot2 in R, inclusi esempi.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/geom_abline-r\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/geom_abline-r\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/geom_abline-r\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come utilizzare geom_abline per aggiungere linee rette in ggplot2"}]},{"@type":"WebSite","@id":"https:\/\/statorials.org\/it\/#website","url":"https:\/\/statorials.org\/it\/","name":"Statorials","description":"La tua guida all&#039;alfabetizzazione statistica!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/statorials.org\/it\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"it-IT"},{"@type":"Person","@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae","name":"Benjamin anderson","image":{"@type":"ImageObject","inLanguage":"it-IT","@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/image\/","url":"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","contentUrl":"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","caption":"Benjamin anderson"},"description":"Ciao, sono Benjamin, un professore di statistica in pensione diventato insegnante dedicato di Statorials. Con una vasta esperienza e competenza nel campo della statistica, sono ansioso di condividere le mie conoscenze per potenziare gli studenti attraverso Statorials. Scopri di pi\u00f9","sameAs":["https:\/\/statorials.org\/it"]}]}},"yoast_meta":{"yoast_wpseo_title":"","yoast_wpseo_metadesc":"","yoast_wpseo_canonical":""},"_links":{"self":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/4285"}],"collection":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/comments?post=4285"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/4285\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=4285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=4285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=4285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}