{"id":474,"date":"2023-07-29T19:13:06","date_gmt":"2023-07-29T19:13:06","guid":{"rendered":"https:\/\/statorials.org\/it\/bump-plot-in-r-utilizzando-ggplot2\/"},"modified":"2023-07-29T19:13:06","modified_gmt":"2023-07-29T19:13:06","slug":"bump-plot-in-r-utilizzando-ggplot2","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/bump-plot-in-r-utilizzando-ggplot2\/","title":{"rendered":"Come creare facilmente una trama in rilievo in r utilizzando ggplot2"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Un <strong>grafico a rilievo<\/strong> \u00e8 un tipo di grafico che mostra la classifica dei diversi gruppi nel tempo invece dei valori assoluti per enfatizzare l&#8217;ordine dei gruppi piuttosto che la quantit\u00e0 di cambiamento.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Questo tutorial spiega come creare facilmente un grafico a rilievo in R utilizzando ggplot2.<\/span><\/p>\n<h2> <strong><span style=\"color: #000000;\">Esempio: creazione di una grafica in rilievo<\/span><\/strong><\/h2>\n<p> <span style=\"color: #000000;\">Per creare un grafico a impatto in R, dobbiamo prima caricare due pacchetti: <strong>dplyr<\/strong> e <strong>ggplot2<\/strong> :<\/span><\/p>\n<pre style=\"background-color: #e5e5e5; font-size: 15px;\"> <strong>library(ggplot2) <span style=\"color: #008080;\">#for creating bump chart<\/span>\nlibrary(dplyr) <span style=\"color: #008080;\">#for manipulating data<\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Successivamente, creeremo alcuni dati con cui lavorare:<\/span><\/p>\n<pre style=\"background-color: #e5e5e5; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#set the seed to make this example reproducible<\/span>\nset.seed(10)\n\ndata &lt;- data.frame(team = rep(LETTERS[1:5], each = 10),\n                   random_num = runif(50),\n                   day = rep(1:10, 5))\n\ndata &lt;- data %&gt;%\n  group_by(day) %&gt;%\n  arrange(day, desc(random_num), team) %&gt;% \n  mutate(rank = row_number()) %&gt;%\n  A group()\n\nhead(data)\n\n# team random_num day rank          \n#1 C 0.865 1 1\n#2 B 0.652 1 2\n#3 D 0.536 1 3\n#4 A 0.507 1 4\n#5 E 0.275 1 5\n#6 C 0.615 2 1<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Questo database mostra semplicemente la \u201cclassifica\u201d di cinque diverse squadre in un periodo di 10 giorni.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Possiamo utilizzare ggplot2 per creare un grafico dei progressi per visualizzare la classifica di ciascuna squadra durante ogni giorno in questo periodo:<\/span><\/p>\n<pre style=\"background-color: #e5e5e5; font-size: 15px;\"> <strong>ggplot(data, aes(x = day, y = rank, group = team)) +\n  geom_line(aes(color = team, alpha = 1), size = 2) +\n  geom_point(aes(color = team, alpha = 1), size = 4) +\n  scale_y_reverse(breaks = 1:nrow(data))<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Questo grafico a sbalzo mostra i dati nel formato desiderato, ma \u00e8 piuttosto brutto. Con alcune modifiche estetiche possiamo rendere questo dipinto molto migliore.<\/span><\/p>\n<h2> <strong><span style=\"color: #000000;\">Stilizza la grafica del rilievo<\/span><\/strong><\/h2>\n<p> <span style=\"color: #000000;\">Per migliorare l&#8217;aspetto del grafico, possiamo utilizzare il seguente tema creato da <a href=\"https:\/\/dominikkoch.github.io\/Bump-Chart\/\" target=\"_blank\" rel=\"noopener\">Dominik Koch<\/a> :<\/span><\/p>\n<pre style=\"background-color: #e5e5e5; font-size: 15px;\"> <strong>my_theme &lt;- function() {\n\n  # Colors\n  color.background = \"white\"\n  color.text = \"#22211d\"\n\n  # Begin construction of chart\n  theme_bw(base_size=15) +\n\n    # Format background colors\n    theme(panel.background = element_rect(fill=color.background,\n                                          color=color.background)) +\n    theme(plot.background = element_rect(fill=color.background,\n                                          color=color.background)) +\n    theme(panel.border = element_rect(color=color.background)) +\n    theme(strip.background = element_rect(fill=color.background,\n                                          color=color.background)) +\n\n    # Format the grid\n    theme(panel.grid.major.y = element_blank()) +\n    theme(panel.grid.minor.y = element_blank()) +\n    theme(axis.ticks = element_blank()) +\n\n    # Format the legend\n    theme(legend.position = \"none\") +\n\n    # Format title and axis labels\n    theme(plot.title = element_text(color=color.text, size=20, face = \"bold\")) +\n    theme(axis.title.x = element_text(size=14, color=\"black\", face = \"bold\")) +\n    theme(axis.title.y = element_text(size=14, color=\"black\", face = \"bold\",\n                                          vjust=1.25)) +\n    theme(axis.text.x = element_text(size=10, vjust=0.5, hjust=0.5,\n                                          color = color.text)) +\n    theme(axis.text.y = element_text(size=10, color = color.text)) +\n    theme(strip.text = element_text(face = \"bold\")) +\n\n    # Plot margins\n    theme(plot.margin = unit(c(0.35, 0.2, 0.3, 0.35), \"cm\"))\n}<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Creeremo nuovamente il grafico a sbalzo, ma questa volta rimuoveremo la legenda, aggiungeremo alcune etichette al grafico e utilizzeremo il tema definito nel codice sopra:<\/span><\/p>\n<pre style=\"background-color: #e5e5e5; font-size: 15px;\"> <strong>ggplot(data, aes(x = as.factor(day), y = rank, group = team)) +\n  geom_line(aes(color = team, alpha = 1), size = 2) +\n  geom_point(aes(color = team, alpha = 1), size = 4) +\n  geom_point(color = \"#FFFFFF\", size = 1) +\n  scale_y_reverse(breaks = 1:nrow(data)) + \n  scale_x_discrete(breaks = 1:10) +\n  theme(legend.position = 'none') +\n  geom_text(data = data %&gt;% filter(day == \"1\"),\n            aes(label = team, x = 0.5), hjust = .5,\n            fontface = \"bold\", color = \"#888888\", size = 4) +\n  geom_text(data = data %&gt;% filter(day == \"10\"),\n            aes(label = team, x = 10.5), hjust = 0.5,\n            fontface = \"bold\", color = \"#888888\", size = 4) +\n  labs(x = 'Day', y = 'Rank', title = 'Team Ranking by Day') +\n  my_theme()<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Possiamo anche evidenziare facilmente una delle righe aggiungendo un argomento <strong>scale_color_manual()<\/strong> . Ad esempio, nel codice seguente, rendiamo la linea della squadra A viola e tutte le altre linee grigie:<\/span><\/p>\n<pre style=\"background-color: #e5e5e5; font-size: 15px;\"> <strong>ggplot(data, aes(x = as.factor(day), y = rank, group = team)) +\n  geom_line(aes(color = team, alpha = 1), size = 2) +\n  geom_point(aes(color = team, alpha = 1), size = 4) +\n  geom_point(color = \"#FFFFFF\", size = 1) +\n  scale_y_reverse(breaks = 1:nrow(data)) + \n  scale_x_discrete(breaks = 1:10) +\n  theme(legend.position = 'none') +\n  geom_text(data = data %&gt;% filter(day == \"1\"),\n            aes(label = team, x = 0.5), hjust = .5,\n            fontface = \"bold\", color = \"#888888\", size = 4) +\n  geom_text(data = data %&gt;% filter(day == \"10\"),\n            aes(label = team, x = 10.5), hjust = 0.5,\n            fontface = \"bold\", color = \"#888888\", size = 4) +\n  labs(x = 'Day', y = 'Rank', title = 'Team Ranking by Day') +\n  my_theme() <span style=\"color: #800080;\">+\n  scale_color_manual(values = c('purple', 'grey', 'grey', 'grey', 'grey'))<\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Potremmo anche evidenziare pi\u00f9 righe se volessimo:<\/span><\/p>\n<pre style=\"background-color: #e5e5e5; font-size: 15px;\"> <strong>ggplot(data, aes(x = as.factor(day), y = rank, group = team)) +\n  geom_line(aes(color = team, alpha = 1), size = 2) +\n  geom_point(aes(color = team, alpha = 1), size = 4) +\n  geom_point(color = \"#FFFFFF\", size = 1) +\n  scale_y_reverse(breaks = 1:nrow(data)) + \n  scale_x_discrete(breaks = 1:10) +\n  theme(legend.position = 'none') +\n  geom_text(data = data %&gt;% filter(day == \"1\"),\n            aes(label = team, x = 0.5), hjust = .5,\n            fontface = \"bold\", color = \"#888888\", size = 4) +\n  geom_text(data = data %&gt;% filter(day == \"10\"),\n            aes(label = team, x = 10.5), hjust = 0.5,\n            fontface = \"bold\", color = \"#888888\", size = 4) +\n  labs(x = 'Day', y = 'Rank', title = 'Team Ranking by Day') +\n  my_theme() <span style=\"color: #800080;\">+\n  scale_color_manual(values = c('purple', 'steelblue', 'grey', 'grey', 'grey'))<\/span><\/strong><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Un grafico a rilievo \u00e8 un tipo di grafico che mostra la classifica dei diversi gruppi nel tempo invece dei valori assoluti per enfatizzare l&#8217;ordine dei gruppi piuttosto che la quantit\u00e0 di cambiamento. Questo tutorial spiega come creare facilmente un grafico a rilievo in R utilizzando ggplot2. Esempio: creazione di una grafica in rilievo Per [&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 facilmente un grafico a rilievo in R utilizzando ggplot2 - Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come creare facilmente un grafico a rilievo in R utilizzando ggplot2.\" \/>\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\/bump-plot-in-r-utilizzando-ggplot2\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come creare facilmente un grafico a rilievo in R utilizzando ggplot2 - Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come creare facilmente un grafico a rilievo in R utilizzando ggplot2.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/bump-plot-in-r-utilizzando-ggplot2\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-29T19:13:06+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\/bump-plot-in-r-utilizzando-ggplot2\/\",\"url\":\"https:\/\/statorials.org\/it\/bump-plot-in-r-utilizzando-ggplot2\/\",\"name\":\"Come creare facilmente un grafico a rilievo in R utilizzando ggplot2 - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-29T19:13:06+00:00\",\"dateModified\":\"2023-07-29T19:13:06+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come creare facilmente un grafico a rilievo in R utilizzando ggplot2.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/bump-plot-in-r-utilizzando-ggplot2\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/bump-plot-in-r-utilizzando-ggplot2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/bump-plot-in-r-utilizzando-ggplot2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come creare facilmente una trama in rilievo in r utilizzando 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 creare facilmente un grafico a rilievo in R utilizzando ggplot2 - Statorials","description":"Questo tutorial spiega come creare facilmente un grafico a rilievo in R utilizzando ggplot2.","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\/bump-plot-in-r-utilizzando-ggplot2\/","og_locale":"it_IT","og_type":"article","og_title":"Come creare facilmente un grafico a rilievo in R utilizzando ggplot2 - Statorials","og_description":"Questo tutorial spiega come creare facilmente un grafico a rilievo in R utilizzando ggplot2.","og_url":"https:\/\/statorials.org\/it\/bump-plot-in-r-utilizzando-ggplot2\/","og_site_name":"Statorials","article_published_time":"2023-07-29T19:13:06+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\/bump-plot-in-r-utilizzando-ggplot2\/","url":"https:\/\/statorials.org\/it\/bump-plot-in-r-utilizzando-ggplot2\/","name":"Come creare facilmente un grafico a rilievo in R utilizzando ggplot2 - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-29T19:13:06+00:00","dateModified":"2023-07-29T19:13:06+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come creare facilmente un grafico a rilievo in R utilizzando ggplot2.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/bump-plot-in-r-utilizzando-ggplot2\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/bump-plot-in-r-utilizzando-ggplot2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/bump-plot-in-r-utilizzando-ggplot2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come creare facilmente una trama in rilievo in r utilizzando 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\/474"}],"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=474"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/474\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=474"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=474"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=474"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}