{"id":4202,"date":"2023-07-12T21:55:51","date_gmt":"2023-07-12T21:55:51","guid":{"rendered":"https:\/\/statorials.org\/it\/riga-media-ggplot-per-gruppo\/"},"modified":"2023-07-12T21:55:51","modified_gmt":"2023-07-12T21:55:51","slug":"riga-media-ggplot-per-gruppo","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/riga-media-ggplot-per-gruppo\/","title":{"rendered":"Come tracciare la linea media per gruppo in ggplot2"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Puoi utilizzare la seguente sintassi di base per tracciare una linea media per gruppo in ggplot2:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#calculate mean points value by team\n<\/span>mean_team &lt;- df %&gt;% group_by(team) %&gt;% summarise(mean_pts=mean(points))\n \n<span style=\"color: #008080;\">#create scatterplot of assists vs points with mean line of points by team\n<\/span>ggplot(df, aes(x=assists, y=points)) +\n  geom_point(aes(color=team)) +\n  geom_hline(data=mean_team, aes(yintercept=mean_pts, col=team))\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Questo particolare esempio crea un grafico a dispersione degli assist vs. <strong>assist<\/strong> variabili. <strong>punti<\/strong> , quindi aggiunge una linea per rappresentare il valore medio <strong>dei punti<\/strong> raggruppati per la variabile <strong>squadra<\/strong> .<\/span><\/p>\n<p> <span style=\"color: #000000;\">L&#8217;esempio seguente mostra come utilizzare questa sintassi nella pratica.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Esempio: tracciare la linea media per gruppo in ggplot2<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Supponiamo di avere il seguente frame di dati in R che contiene informazioni sui punti e sugli assist dei giocatori di basket di tre squadre diverse:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008080;\">#create data frame\n<\/span>df &lt;- data. <span style=\"color: #3366ff;\">frame<\/span> (team=rep(c(' <span style=\"color: #ff0000;\">A<\/span> ', ' <span style=\"color: #ff0000;\">B<\/span> ', ' <span style=\"color: #ff0000;\">C<\/span> '), each= <span style=\"color: #008000;\">5<\/span> ),\n                 assists=c(2, 4, 4, 5, 6, 6, 7, 7,\n                           8, 9, 7, 8, 13, 14, 12),\n                 dots=c(8, 8, 9, 9, 10, 9, 12, 13,\n                          14, 15, 14, 14, 16, 19, 22))\n\n<span style=\"color: #008080;\">#view data frame\n<\/span>df\n\n   team assists points\n1 to 2 8\n2 to 4 8\n3 to 4 9\n4 to 5 9\n5 to 6 10\n6 B 6 9\n7 B 7 12\n8 B 7 13\n9 B 8 14\n10 B 9 15\n11 C 7 14\n12 C 8 14\n13 C 13 16\n14 C 14 19\n15 C 12 22\n<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">Possiamo utilizzare il seguente codice per creare un grafico a dispersione degli <strong>assist<\/strong> vs. variabili. <strong>punti<\/strong> , quindi aggiungi una linea per rappresentare il valore medio <strong>dei punti<\/strong> raggruppati per la variabile <strong>squadra<\/strong> .<\/span> <\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008000;\">library<\/span> (dplyr)\n<span style=\"color: #008000;\">library<\/span> (ggplot2)\n\n<span style=\"color: #008080;\">#calculate mean points value by team\n<span style=\"color: #000000;\">mean_team &lt;- df %&gt;% group_by(team) %&gt;% summarise(mean_pts=mean(points))\n \n<span style=\"color: #008080;\">#create scatterplot of assists vs points with mean line of points by team\n<\/span>ggplot(df, aes(x=assists, y=points)) +\n  geom_point(aes(color=team)) +\n  geom_hline(data=mean_team, aes(yintercept=mean_pts, col=team))<\/span><\/span><\/strong><\/span> <\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-33241\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/ligne-moyenne1.jpg\" alt=\"riga media ggplot per gruppo\" width=\"598\" height=\"465\" srcset=\"\" sizes=\"\"><\/p>\n<p> <span style=\"color: #000000;\">Le tre linee sono codificate a colori per visualizzare il valore del punto medio di ciascuna squadra.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Possiamo visualizzare il data frame <strong>Mean_Team<\/strong> che abbiamo creato per vedere i valori effettivi dei punti medi per ciascuna squadra:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008080;\">#view mean points value by team\n<span style=\"color: #000000;\">mean_team<\/span>\n\n<\/span>`summarise()` ungrouping output (override with `.groups` argument)\n# A tibble: 3 x 2\n  team mean_pts\n      \n1 to 8.8\n2 B 12.6\n3 C 17<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">Dal risultato possiamo vedere:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\">Il valore medio dei punti dei giocatori della squadra A \u00e8 <strong>8,8.<\/strong><\/span><\/li>\n<li> <span style=\"color: #000000;\">Il valore medio dei punti dei giocatori della squadra B \u00e8 <strong>12,6.<\/strong><\/span><\/li>\n<li> <span style=\"color: #000000;\">Il valore medio dei punti dei giocatori della squadra C \u00e8 <strong>17.<\/strong><\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Questi valori corrispondono alle posizioni delle linee sull&#8217;asse y del grafico a dispersione che abbiamo creato.<\/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 attivit\u00e0 comuni in R:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/it\/istogramma-della-frequenza-relativa-r\/\" target=\"_blank\" rel=\"noopener\">Come creare un istogramma di frequenza relativo in R<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/boxplot-multipli-r\/\" target=\"_blank\" rel=\"noopener\">Come tracciare pi\u00f9 boxplot in un singolo grafico in R<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/come-tracciare-piu-linee-di-dati-in-serie-in-un-grafico-in-r\/\" target=\"_blank\" rel=\"noopener\">Come tracciare pi\u00f9 linee in un grafico in R<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Puoi utilizzare la seguente sintassi di base per tracciare una linea media per gruppo in ggplot2: #calculate mean points value by team mean_team &lt;- df %&gt;% group_by(team) %&gt;% summarise(mean_pts=mean(points)) #create scatterplot of assists vs points with mean line of points by team ggplot(df, aes(x=assists, y=points)) + geom_point(aes(color=team)) + geom_hline(data=mean_team, aes(yintercept=mean_pts, col=team)) Questo particolare esempio crea [&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 tracciare la linea media per gruppo in ggplot2 \u2013 Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come tracciare una linea media per gruppo in ggplot2, con un esempio.\" \/>\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\/riga-media-ggplot-per-gruppo\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come tracciare la linea media per gruppo in ggplot2 \u2013 Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come tracciare una linea media per gruppo in ggplot2, con un esempio.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/riga-media-ggplot-per-gruppo\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-12T21:55:51+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/ligne-moyenne1.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\/riga-media-ggplot-per-gruppo\/\",\"url\":\"https:\/\/statorials.org\/it\/riga-media-ggplot-per-gruppo\/\",\"name\":\"Come tracciare la linea media per gruppo in ggplot2 \u2013 Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-12T21:55:51+00:00\",\"dateModified\":\"2023-07-12T21:55:51+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come tracciare una linea media per gruppo in ggplot2, con un esempio.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/riga-media-ggplot-per-gruppo\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/riga-media-ggplot-per-gruppo\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/riga-media-ggplot-per-gruppo\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come tracciare la linea media per gruppo 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 tracciare la linea media per gruppo in ggplot2 \u2013 Statorials","description":"Questo tutorial spiega come tracciare una linea media per gruppo in ggplot2, con un esempio.","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\/riga-media-ggplot-per-gruppo\/","og_locale":"it_IT","og_type":"article","og_title":"Come tracciare la linea media per gruppo in ggplot2 \u2013 Statorials","og_description":"Questo tutorial spiega come tracciare una linea media per gruppo in ggplot2, con un esempio.","og_url":"https:\/\/statorials.org\/it\/riga-media-ggplot-per-gruppo\/","og_site_name":"Statorials","article_published_time":"2023-07-12T21:55:51+00:00","og_image":[{"url":"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/ligne-moyenne1.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\/riga-media-ggplot-per-gruppo\/","url":"https:\/\/statorials.org\/it\/riga-media-ggplot-per-gruppo\/","name":"Come tracciare la linea media per gruppo in ggplot2 \u2013 Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-12T21:55:51+00:00","dateModified":"2023-07-12T21:55:51+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come tracciare una linea media per gruppo in ggplot2, con un esempio.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/riga-media-ggplot-per-gruppo\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/riga-media-ggplot-per-gruppo\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/riga-media-ggplot-per-gruppo\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come tracciare la linea media per gruppo 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\/4202"}],"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=4202"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/4202\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=4202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=4202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=4202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}