Skip to content

Commit

Permalink
minor update
Browse files Browse the repository at this point in the history
  • Loading branch information
spsanderson committed Oct 11, 2023
1 parent c3eb1e9 commit 52d394b
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions _freeze/posts/2023-10-11/index/execute-results/html.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"hash": "c5e833719d12d4b1ea2cd9a2871ad36b",
"hash": "4d9ef6f499b1f07cefe0853d38e8d793",
"result": {
"markdown": "---\ntitle: \"Horizontal Legends in Base R\"\nauthor: \"Steven P. Sanderson II, MPH\"\ndate: \"2023-10-11\"\ncategories: [rtip, viz]\n---\n\n\n# Introduction\n\nCreating a horizontal legend in base R can be a useful skill when you want to label multiple categories in a plot without taking up too much vertical space. In this blog post, we'll explore various methods to create horizontal legends in R and provide examples with clear explanations.\n\n# Why Do We Need Horizontal Legends?\n\nVertical legends are great for smaller plots, but in larger visualizations, they can become a space-consuming eyesore. Horizontal legends, on the other hand, allow you to neatly label categories without cluttering the plot area. They are especially useful when you have many categories to label.\n\n# Using the `legend` Function\n\nThe most straightforward way to create a horizontal legend in base R is by using the `legend` function. Here's a simple example:\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Create a sample plot\nplot(1:5, col = 1:5, pch = 19)\n\n# Add a horizontal legend\nlegend(\"top\", legend = c(\"A\", \"B\", \"C\", \"D\", \"E\"), \n fill = 1:5, horiz = TRUE, x.intersp = 0.2)\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-1-1.png){width=672}\n:::\n:::\n\n\nIn this code, we first create a basic scatter plot and then use the `legend` function to add a legend at the top of the plot (`\"top\"`). The `horiz = TRUE` argument specifies a horizontal legend.\n\n# Customizing the Horizontal Legend\n\nYou can further customize the horizontal legend to match your preferences. Here are some common parameters:\n\n- `x.intersp` controls the horizontal spacing between legend elements.\n- `inset` adjusts the distance of the legend from the plot.\n- `title` adds a title to the legend.\n\n```R\n# Customize the horizontal legend\nlegend(\"top\", legend = c(\"A\", \"B\", \"C\", \"D\", \"E\"), \n fill = 1:5, horiz = TRUE, x.intersp = 0.2, inset = 0.02, \n title = \"Categories\")\n```\n\n# Adding Multiple Horizontal Legends\n\nIn some cases, you might need multiple horizontal legends in a single plot. You can achieve this by specifying different locations for each legend.\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Create a sample plot\nplot(1:5, col = 1:5, pch = 19)\n\n# Add two horizontal legends\nlegend(\"top\", legend = c(\"A\", \"B\", \"C\"), \n fill = 1:3, horiz = TRUE, x.intersp = 0.2, inset = 0.02,\n title = \"Top Legend\")\nlegend(\"bottom\", legend = c(\"D\", \"E\"), \n fill = 4:5, horiz = TRUE, x.intersp = 0.2, inset = 0.02,\n title = \"Bottom Legend\")\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-2-1.png){width=672}\n:::\n:::\n\n\nIn this example, we add two horizontal legends at the top and bottom of the plot, each with its set of labels and colors.\n\n# Encourage Readers to Experiment\n\nCreating horizontal legends in base R is a versatile skill that you can use in various data visualization projects. I encourage you to experiment with different plot types, colors, and parameters to create the perfect horizontal legend for your specific needs. Don't be afraid to get creative and tailor your legends to make your plots more informative and visually appealing.\n\nBy following these simple steps and experimenting with your own plots, you'll be able to master the art of horizontal legends in R. So go ahead and give it a try! Your future visualizations will thank you for the extra clarity and elegance that horizontal legends provide. Happy coding!",
"markdown": "---\ntitle: \"Horizontal Legends in Base R\"\nauthor: \"Steven P. Sanderson II, MPH\"\ndate: \"2023-10-11\"\ncategories: [rtip, viz]\n---\n\n\n# Introduction\n\nCreating a horizontal legend in base R can be a useful skill when you want to label multiple categories in a plot without taking up too much vertical space. In this blog post, we'll explore various methods to create horizontal legends in R and provide examples with clear explanations.\n\n# Why Do We Need Horizontal Legends?\n\nVertical legends are great for smaller plots, but in larger visualizations, they can become a space-consuming eyesore. Horizontal legends, on the other hand, allow you to neatly label categories without cluttering the plot area. They are especially useful when you have many categories to label.\n\n# Using the `legend` Function\n\nThe most straightforward way to create a horizontal legend in base R is by using the `legend` function. Here's a simple example:\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Create a sample plot\nplot(1:5, col = 1:5, pch = 19)\n\n# Add a horizontal legend\nlegend(\"top\", legend = c(\"A\", \"B\", \"C\", \"D\", \"E\"), \n fill = 1:5, horiz = TRUE, x.intersp = 0.2)\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-1-1.png){width=672}\n:::\n:::\n\n\nIn this code, we first create a basic scatter plot and then use the `legend` function to add a legend at the top of the plot (`\"top\"`). The `horiz = TRUE` argument specifies a horizontal legend.\n\n# Customizing the Horizontal Legend\n\nYou can further customize the horizontal legend to match your preferences. Here are some common parameters:\n\n- `x.intersp` controls the horizontal spacing between legend elements.\n- `inset` adjusts the distance of the legend from the plot.\n- `title` adds a title to the legend.\n\n```R\n# Customize the horizontal legend\nlegend(\"top\", legend = c(\"A\", \"B\", \"C\", \"D\", \"E\"), \n fill = 1:5, horiz = TRUE, x.intersp = 0.2, inset = 0.02, \n title = \"Categories\")\n```\n\n# Adding Multiple Horizontal Legends\n\nIn some cases, you might need multiple horizontal legends in a single plot. You can achieve this by specifying different locations for each legend.\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Create a sample plot\nplot(1:5, col = 1:5, pch = 19)\n\n# Add two horizontal legends\nlegend(\"top\", legend = c(\"A\", \"B\", \"C\"), \n fill = 1:3, horiz = TRUE, x.intersp = 0.2, inset = 0.02,\n title = \"Top Legend\")\nlegend(\"bottom\", legend = c(\"D\", \"E\"), \n fill = 4:5, horiz = TRUE, x.intersp = 0.2, inset = 0.02,\n title = \"Bottom Legend\")\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-2-1.png){width=672}\n:::\n:::\n\n\nIn this example, we add two horizontal legends at the top and bottom of the plot, each with its set of labels and colors.\n\n# Experiment\n\nCreating horizontal legends in base R is a versatile skill that you can use in various data visualization projects. I encourage you to experiment with different plot types, colors, and parameters to create the perfect horizontal legend for your specific needs. Don't be afraid to get creative and tailor your legends to make your plots more informative and visually appealing.\n\nBy following these simple steps and experimenting with your own plots, you'll be able to master the art of horizontal legends in R. So go ahead and give it a try! Your future visualizations will thank you for the extra clarity and elegance that horizontal legends provide. Happy coding!",
"supporting": [
"index_files"
],
Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ <h5 class="quarto-listing-category-title">Categories</h5><div class="quarto-list

<div class="quarto-listing quarto-listing-container-default" id="listing-listing">
<div class="list quarto-listing-default">
<div class="quarto-post image-right" data-index="0" data-categories="rtip,viz" data-listing-date-sort="1696996800000" data-listing-file-modified-sort="1697026517063" data-listing-date-modified-sort="NaN" data-listing-reading-time-sort="3">
<div class="quarto-post image-right" data-index="0" data-categories="rtip,viz" data-listing-date-sort="1696996800000" data-listing-file-modified-sort="1697026575419" data-listing-date-modified-sort="NaN" data-listing-reading-time-sort="3">
<div class="thumbnail">
<p><a href="./posts/2023-10-11/index.html"> <p class="card-img-top"><img src="posts\2023-10-11\index_files\figure-html\unnamed-chunk-1-1.png" class="thumbnail-image card-img"/></p> </a></p>
</div>
Expand Down
4 changes: 2 additions & 2 deletions docs/index.xml
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,8 @@ font-style: inherit;">"Bottom Legend"</span>)</span></code></pre></div>
</div>
<p>In this example, we add two horizontal legends at the top and bottom of the plot, each with its set of labels and colors.</p>
</section>
<section id="encourage-readers-to-experiment" class="level1">
<h1>Encourage Readers to Experiment</h1>
<section id="experiment" class="level1">
<h1>Experiment</h1>
<p>Creating horizontal legends in base R is a versatile skill that you can use in various data visualization projects. I encourage you to experiment with different plot types, colors, and parameters to create the perfect horizontal legend for your specific needs. Don’t be afraid to get creative and tailor your legends to make your plots more informative and visually appealing.</p>
<p>By following these simple steps and experimenting with your own plots, you’ll be able to master the art of horizontal legends in R. So go ahead and give it a try! Your future visualizations will thank you for the extra clarity and elegance that horizontal legends provide. Happy coding!</p>
Expand Down
4 changes: 2 additions & 2 deletions docs/posts/2023-10-11/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ <h1>Adding Multiple Horizontal Legends</h1>
</div>
<p>In this example, we add two horizontal legends at the top and bottom of the plot, each with its set of labels and colors.</p>
</section>
<section id="encourage-readers-to-experiment" class="level1">
<h1>Encourage Readers to Experiment</h1>
<section id="experiment" class="level1">
<h1>Experiment</h1>
<p>Creating horizontal legends in base R is a versatile skill that you can use in various data visualization projects. I encourage you to experiment with different plot types, colors, and parameters to create the perfect horizontal legend for your specific needs. Don’t be afraid to get creative and tailor your legends to make your plots more informative and visually appealing.</p>
<p>By following these simple steps and experimenting with your own plots, you’ll be able to master the art of horizontal legends in R. So go ahead and give it a try! Your future visualizations will thank you for the extra clarity and elegance that horizontal legends provide. Happy coding!</p>

Expand Down
2 changes: 1 addition & 1 deletion docs/search.json
Original file line number Diff line number Diff line change
Expand Up @@ -3616,6 +3616,6 @@
"href": "posts/2023-10-11/index.html",
"title": "Horizontal Legends in Base R",
"section": "",
"text": "Introduction\nCreating a horizontal legend in base R can be a useful skill when you want to label multiple categories in a plot without taking up too much vertical space. In this blog post, we’ll explore various methods to create horizontal legends in R and provide examples with clear explanations.\n\n\nWhy Do We Need Horizontal Legends?\nVertical legends are great for smaller plots, but in larger visualizations, they can become a space-consuming eyesore. Horizontal legends, on the other hand, allow you to neatly label categories without cluttering the plot area. They are especially useful when you have many categories to label.\n\n\nUsing the legend Function\nThe most straightforward way to create a horizontal legend in base R is by using the legend function. Here’s a simple example:\n\n# Create a sample plot\nplot(1:5, col = 1:5, pch = 19)\n\n# Add a horizontal legend\nlegend(\"top\", legend = c(\"A\", \"B\", \"C\", \"D\", \"E\"), \n fill = 1:5, horiz = TRUE, x.intersp = 0.2)\n\n\n\n\nIn this code, we first create a basic scatter plot and then use the legend function to add a legend at the top of the plot (\"top\"). The horiz = TRUE argument specifies a horizontal legend.\n\n\nCustomizing the Horizontal Legend\nYou can further customize the horizontal legend to match your preferences. Here are some common parameters:\n\nx.intersp controls the horizontal spacing between legend elements.\ninset adjusts the distance of the legend from the plot.\ntitle adds a title to the legend.\n\n# Customize the horizontal legend\nlegend(\"top\", legend = c(\"A\", \"B\", \"C\", \"D\", \"E\"), \n fill = 1:5, horiz = TRUE, x.intersp = 0.2, inset = 0.02, \n title = \"Categories\")\n\n\nAdding Multiple Horizontal Legends\nIn some cases, you might need multiple horizontal legends in a single plot. You can achieve this by specifying different locations for each legend.\n\n# Create a sample plot\nplot(1:5, col = 1:5, pch = 19)\n\n# Add two horizontal legends\nlegend(\"top\", legend = c(\"A\", \"B\", \"C\"), \n fill = 1:3, horiz = TRUE, x.intersp = 0.2, inset = 0.02,\n title = \"Top Legend\")\nlegend(\"bottom\", legend = c(\"D\", \"E\"), \n fill = 4:5, horiz = TRUE, x.intersp = 0.2, inset = 0.02,\n title = \"Bottom Legend\")\n\n\n\n\nIn this example, we add two horizontal legends at the top and bottom of the plot, each with its set of labels and colors.\n\n\nEncourage Readers to Experiment\nCreating horizontal legends in base R is a versatile skill that you can use in various data visualization projects. I encourage you to experiment with different plot types, colors, and parameters to create the perfect horizontal legend for your specific needs. Don’t be afraid to get creative and tailor your legends to make your plots more informative and visually appealing.\nBy following these simple steps and experimenting with your own plots, you’ll be able to master the art of horizontal legends in R. So go ahead and give it a try! Your future visualizations will thank you for the extra clarity and elegance that horizontal legends provide. Happy coding!"
"text": "Introduction\nCreating a horizontal legend in base R can be a useful skill when you want to label multiple categories in a plot without taking up too much vertical space. In this blog post, we’ll explore various methods to create horizontal legends in R and provide examples with clear explanations.\n\n\nWhy Do We Need Horizontal Legends?\nVertical legends are great for smaller plots, but in larger visualizations, they can become a space-consuming eyesore. Horizontal legends, on the other hand, allow you to neatly label categories without cluttering the plot area. They are especially useful when you have many categories to label.\n\n\nUsing the legend Function\nThe most straightforward way to create a horizontal legend in base R is by using the legend function. Here’s a simple example:\n\n# Create a sample plot\nplot(1:5, col = 1:5, pch = 19)\n\n# Add a horizontal legend\nlegend(\"top\", legend = c(\"A\", \"B\", \"C\", \"D\", \"E\"), \n fill = 1:5, horiz = TRUE, x.intersp = 0.2)\n\n\n\n\nIn this code, we first create a basic scatter plot and then use the legend function to add a legend at the top of the plot (\"top\"). The horiz = TRUE argument specifies a horizontal legend.\n\n\nCustomizing the Horizontal Legend\nYou can further customize the horizontal legend to match your preferences. Here are some common parameters:\n\nx.intersp controls the horizontal spacing between legend elements.\ninset adjusts the distance of the legend from the plot.\ntitle adds a title to the legend.\n\n# Customize the horizontal legend\nlegend(\"top\", legend = c(\"A\", \"B\", \"C\", \"D\", \"E\"), \n fill = 1:5, horiz = TRUE, x.intersp = 0.2, inset = 0.02, \n title = \"Categories\")\n\n\nAdding Multiple Horizontal Legends\nIn some cases, you might need multiple horizontal legends in a single plot. You can achieve this by specifying different locations for each legend.\n\n# Create a sample plot\nplot(1:5, col = 1:5, pch = 19)\n\n# Add two horizontal legends\nlegend(\"top\", legend = c(\"A\", \"B\", \"C\"), \n fill = 1:3, horiz = TRUE, x.intersp = 0.2, inset = 0.02,\n title = \"Top Legend\")\nlegend(\"bottom\", legend = c(\"D\", \"E\"), \n fill = 4:5, horiz = TRUE, x.intersp = 0.2, inset = 0.02,\n title = \"Bottom Legend\")\n\n\n\n\nIn this example, we add two horizontal legends at the top and bottom of the plot, each with its set of labels and colors.\n\n\nExperiment\nCreating horizontal legends in base R is a versatile skill that you can use in various data visualization projects. I encourage you to experiment with different plot types, colors, and parameters to create the perfect horizontal legend for your specific needs. Don’t be afraid to get creative and tailor your legends to make your plots more informative and visually appealing.\nBy following these simple steps and experimenting with your own plots, you’ll be able to master the art of horizontal legends in R. So go ahead and give it a try! Your future visualizations will thank you for the extra clarity and elegance that horizontal legends provide. Happy coding!"
}
]
4 changes: 2 additions & 2 deletions docs/sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</url>
<url>
<loc>https://www.spsanderson.com/steveondata/index.html</loc>
<lastmod>2023-10-11T12:15:42.556Z</lastmod>
<lastmod>2023-10-11T12:16:40.933Z</lastmod>
</url>
<url>
<loc>https://www.spsanderson.com/steveondata/posts/rtip-2023-04-06/index.html</loc>
Expand Down Expand Up @@ -478,6 +478,6 @@
</url>
<url>
<loc>https://www.spsanderson.com/steveondata/posts/2023-10-11/index.html</loc>
<lastmod>2023-10-11T12:15:50.286Z</lastmod>
<lastmod>2023-10-11T12:16:48.977Z</lastmod>
</url>
</urlset>
2 changes: 1 addition & 1 deletion posts/2023-10-11/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ legend("bottom", legend = c("D", "E"),

In this example, we add two horizontal legends at the top and bottom of the plot, each with its set of labels and colors.

# Encourage Readers to Experiment
# Experiment

Creating horizontal legends in base R is a versatile skill that you can use in various data visualization projects. I encourage you to experiment with different plot types, colors, and parameters to create the perfect horizontal legend for your specific needs. Don't be afraid to get creative and tailor your legends to make your plots more informative and visually appealing.

Expand Down

0 comments on commit 52d394b

Please sign in to comment.