{"id":481,"date":"2023-07-29T18:32:36","date_gmt":"2023-07-29T18:32:36","guid":{"rendered":"https:\/\/statorials.org\/it\/grafica-lecca-lecca-r\/"},"modified":"2023-07-29T18:32:36","modified_gmt":"2023-07-29T18:32:36","slug":"grafica-lecca-lecca-r","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/grafica-lecca-lecca-r\/","title":{"rendered":"Come creare un grafico lollipop in r"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Simile a un grafico a barre, un <strong>grafico lecca-lecca<\/strong> \u00e8 utile per confrontare i valori quantitativi di una variabile categoriale. Invece di utilizzare le barre, un grafico lecca-lecca utilizza linee con cerchi alla fine per rappresentare valori quantitativi.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Un grafico lecca-lecca \u00e8 un ottimo modo per confrontare pi\u00f9 categorie riducendo al minimo la quantit\u00e0 di colore sul grafico e concentrando l&#8217;attenzione del lettore sui valori effettivi sul grafico anzich\u00e9 sulle linee o altri elementi grafici sul grafico. Molte persone considerano anche il tagliere per lecca-lecca esteticamente gradevole.<\/span><\/p>\n<p> <span style=\"color: #000000;\">In questo tutorial, eseguiremo i passaggi necessari per creare il seguente grafico lecca-lecca:<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Esempio: grafico Lollipop in R<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Per questo esempio, utilizzeremo il set di dati R integrato <strong>di 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<\/strong><\/pre>\n<h3><\/h3>\n<h2> <span style=\"color: #000000;\"><strong>Una tabella lecca-lecca di base<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Il codice seguente illustra come <strong>&nbsp;<\/strong> per creare un grafico lecca-lecca per confrontare i <em>mpg<\/em> (miglia per gallone) di ciascuna delle 32 auto nel set di dati.<\/span><\/p>\n<p> <span style=\"color: #000000;\">I nomi delle auto sono definiti nei nomi delle righe del set di dati, quindi creiamo prima una nuova colonna nel set di dati che contiene questi nomi di righe.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Successivamente, carichiamo la libreria <strong>ggplot2<\/strong> , che utilizzeremo per creare il grafico lollipop.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Con ggplot2 utilizziamo <strong>geom_segment<\/strong> per creare le linee sulla trama. Impostiamo i valori x iniziale e finale rispettivamente come <em>0<\/em> e <em>mpg<\/em> . Impostiamo i valori y iniziale e finale come <em>char<\/em> :<\/span><\/p>\n<pre style=\"background-color: #e5e5e5; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#create new column for car names<\/span>\nmtcars$car &lt;- row.names(mtcars)\n\n<span style=\"color: #008080;\">#load <em>ggplot2<\/em> library<\/span>\nlibrary(ggplot2)\n\n<span style=\"color: #008080;\">#create lollipop chart<\/span>\nggplot(mtcars, aes(x = mpg, y = car)) +\n        geom_segment(aes(x = 0, y = car, xend = mpg, yend = car)) +\n        geom_point()<\/strong><\/pre>\n<h2> <span style=\"color: #000000;\"><strong>Aggiunta di etichette<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Possiamo anche aggiungere etichette al grafico utilizzando gli argomenti <strong>label<\/strong> e <strong>geom_text<\/strong> :<\/span><\/p>\n<pre style=\"background-color: #e5e5e5; font-size: 15px;\"> <strong>ggplot(mtcars, aes(x = mpg, y = car, <span style=\"color: #800080;\">label = mpg<\/span> )) +\n        geom_segment(aes(x = 0, y = car, xend = mpg, yend = car)) +\n        geom_point() <span style=\"color: #800080;\">+\n        geom_text(nudge_x = 1.5)<\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Oppure invece di posizionare le etichette alla fine di ogni riga, potremmo posizionarle all&#8217;interno dei cerchi stessi ingrandendo i cerchi e cambiando il colore del carattere dell&#8217;etichetta in bianco:<\/span><\/p>\n<pre style=\"background-color: #e5e5e5; font-size: 15px;\"> <strong>ggplot(mtcars, aes(x = mpg, y = car, label = mpg)) +\n        geom_segment(aes(x = 0, y = car, xend = mpg, yend = car)) +\n        geom_point( <span style=\"color: #800080;\">size = 7<\/span> ) +\n        geom_text( <span style=\"color: #800080;\">color = 'white', size = 2<\/span> )<\/strong><\/pre>\n<h2> <span style=\"color: #000000;\"><strong>Confronta i valori con una media<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Possiamo anche utilizzare un grafico lecca-lecca per confrontare i valori con un numero specifico. Ad esempio, possiamo trovare il valore medio <em>di mpg<\/em> nel set di dati e quindi confrontare <em>il valore mpg<\/em> di ciascuna auto con la media.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Il codice seguente utilizza la libreria <strong>dplyr<\/strong> per trovare il valore medio <em>di mpg<\/em> e quindi disporre le auto in ordine crescente <em>di mpg<\/em> :<\/span><\/p>\n<pre style=\"background-color: #e5e5e5; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#load library <em>dplyr\n<\/em><\/span>library(dplyr)\n\n<span style=\"color: #008080;\">#find mean value of <em>mpg<\/em> and arrange cars in order <em>by mpg<\/em> descending<\/span>\nmtcars_new &lt;- mtcars %&gt;%\n                arrange(mpg) %&gt;%\n                mutate(mean_mpg = mean(mpg),\n                       flag = ifelse(mpg - mean_mpg &gt; 0, TRUE, FALSE),\n                       car = factor(car, levels = .$car))\n\n<span style=\"color: #008080;\">#view first six rows of <em>mtcars_new\n<\/em><span style=\"color: #000000;\">head(mtcars_new)\n<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Quindi il codice seguente crea il grafico lecca-lecca impostando il colore del cerchio in modo che sia uguale al valore <em>del flag<\/em> (in questo caso TRUE o FALSE) e il valore x iniziale per ciascuna auto sia uguale al valore medio <em>mpg<\/em> .<\/span><\/p>\n<pre style=\"background-color: #e5e5e5; font-size: 15px;\"> <strong>ggplot(mtcars_new, aes(x = mpg, y = car, <span style=\"color: #800080;\">color = flag<\/span> )) +\n        geom_segment(aes( <span style=\"color: #800080;\">x = mean_mpg<\/span> , y = car, xend = mpg, yend = car)) +\n        geom_point()<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Utilizzando questo tipo di tavolozza di colori, possiamo facilmente determinare quali auto hanno <em>un mpg<\/em> inferiore e superiore rispetto alla media del set di dati.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Per impostazione predefinita, R utilizza il blu e il rosso come colori del grafico. Tuttavia, possiamo utilizzare tutti i colori che desideriamo utilizzando l&#8217;argomento <strong>scale_color_manual<\/strong> :<\/span><\/p>\n<pre style=\"background-color: #e5e5e5; font-size: 15px;\"> <strong>ggplot(mtcars_new, aes(x = mpg, y = car, color = flag)) +\n        geom_segment(aes(x = mean_mpg, y = car, xend = mpg, yend = car)) +\n        geom_point() <span style=\"color: #800080;\">+\n        scale_color_manual(values = c(\"purple\", \"blue\"))<\/span><\/strong><\/pre>\n<h2> <strong><span style=\"color: #000000;\"><span style=\"color: #000000;\">Cambia l&#8217;estetica del grafico<\/span><\/span><\/strong><\/h2>\n<p> <span style=\"color: #000000;\">Infine, possiamo utilizzare le ampie funzionalit\u00e0 di <strong>ggplot2<\/strong> per modificare ulteriormente l&#8217;estetica del grafico e creare un prodotto finale dall&#8217;aspetto professionale:<\/span><\/p>\n<pre style=\"background-color: #e5e5e5; font-size: 15px;\"> <strong>ggplot(mtcars_new, aes(x = mpg, y = car, color = flag)) +\n        geom_segment(aes(x = mean_mpg, y = car, xend = mpg, yend = car), color = \"grey\") +\n        geom_point() +\n        annotate(\"text\", x = 27, y = 20, label = \"Above Average\", color = \"#00BFC4\", size = 3, hjust = -0.1, vjust = .75) +\n        annotate(\"text\", x = 27, y = 17, label = \"Below Average\", color = \"#F8766D\", size = 3, hjust = -0.1, vjust = -.1) +\n        geom_segment(aes(x = 26.5, xend = 26.5, y = 19, yend = 23),\n                     arrow = arrow(length = unit(0.2,\"cm\")), color = \"#00BFC4\") +\n        geom_segment(aes(x = 26.5, xend = 26.5, y = 18, yend = 14),\n                     arrow = arrow(length = unit(0.2,\"cm\")), color = \"#F8766D\") +\n        labs(title = \"Miles per Gallon by Car\") +\n        theme_minimal() +\n        theme(axis.title = element_blank(),\n              panel.grid.minor = element_blank(),\n              legend.position = \"none\",\n              text = element_text(family = \"Georgia\"),\n              axis.text.y = element_text(size = 8),\n              plot.title = element_text(size = 20, margin = margin(b = 10), hjust = 0),\n              plot.subtitle = element_text(size = 12, color = \"darkslategrey\", margin = margin(b = 25, l = -25)),\n              plot.caption = element_text(size = 8, margin = margin(t = 10), color = \"grey70\", hjust = 0))<\/strong><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Simile a un grafico a barre, un grafico lecca-lecca \u00e8 utile per confrontare i valori quantitativi di una variabile categoriale. Invece di utilizzare le barre, un grafico lecca-lecca utilizza linee con cerchi alla fine per rappresentare valori quantitativi. Un grafico lecca-lecca \u00e8 un ottimo modo per confrontare pi\u00f9 categorie riducendo al minimo la quantit\u00e0 di [&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 creare un grafico lecca-lecca in R - Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come creare facilmente un grafico lecca-lecca in R.\" \/>\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\/grafica-lecca-lecca-r\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come creare un grafico lecca-lecca in R - Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come creare facilmente un grafico lecca-lecca in R.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/grafica-lecca-lecca-r\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-29T18:32:36+00:00\" \/>\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=\"4 minuti\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/it\/grafica-lecca-lecca-r\/\",\"url\":\"https:\/\/statorials.org\/it\/grafica-lecca-lecca-r\/\",\"name\":\"Come creare un grafico lecca-lecca in R - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-29T18:32:36+00:00\",\"dateModified\":\"2023-07-29T18:32:36+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come creare facilmente un grafico lecca-lecca in R.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/grafica-lecca-lecca-r\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/grafica-lecca-lecca-r\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/grafica-lecca-lecca-r\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come creare un grafico lollipop in r\"}]},{\"@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 creare un grafico lecca-lecca in R - Statorials","description":"Questo tutorial spiega come creare facilmente un grafico lecca-lecca in R.","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\/grafica-lecca-lecca-r\/","og_locale":"it_IT","og_type":"article","og_title":"Come creare un grafico lecca-lecca in R - Statorials","og_description":"Questo tutorial spiega come creare facilmente un grafico lecca-lecca in R.","og_url":"https:\/\/statorials.org\/it\/grafica-lecca-lecca-r\/","og_site_name":"Statorials","article_published_time":"2023-07-29T18:32:36+00:00","author":"Benjamin anderson","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Benjamin anderson","Est. reading time":"4 minuti"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/it\/grafica-lecca-lecca-r\/","url":"https:\/\/statorials.org\/it\/grafica-lecca-lecca-r\/","name":"Come creare un grafico lecca-lecca in R - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-29T18:32:36+00:00","dateModified":"2023-07-29T18:32:36+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come creare facilmente un grafico lecca-lecca in R.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/grafica-lecca-lecca-r\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/grafica-lecca-lecca-r\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/grafica-lecca-lecca-r\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come creare un grafico lollipop in r"}]},{"@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\/481"}],"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=481"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/481\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}