Skip to content

Commit

Permalink
fix post
Browse files Browse the repository at this point in the history
  • Loading branch information
spsanderson committed Oct 6, 2023
1 parent a9415be commit f1a60d2
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 248 deletions.
4 changes: 2 additions & 2 deletions _freeze/posts/2023-10-06/index/execute-results/html.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"hash": "ce57656e81c644f7ed021508c30dd18b",
"hash": "c2ec9943cb4567122d99ade9d2171a5f",
"result": {
"markdown": "---\ntitle: \"Title: Mastering Legends in R: Drawing Them Outside the Plot\"\nauthor: \"Steven P. Sanderson II, MPH\"\ndate: \"2023-10-06\"\ncategories: [rtip, viz]\n---\n\n\nOf course, Steve! I'd be delighted to help you write an engaging blog post about drawing a legend outside of a plot in base R. It's a handy skill for R programmers, and I'm sure your readers will appreciate the guidance.\n\n---\n\n# Introduction\n\nLegends are an essential part of data visualization. They help us understand the meaning behind the colors and shapes in our plots. But what if your legend is too big or clutters your plot? Fear not, fellow R enthusiast! In this blog post, we'll explore how to draw a legend outside of a plot using base R, with a step-by-step example that's easy to follow.\n\n# Why Draw Legends Outside the Plot?\n\nDrawing legends outside the plot is particularly useful when you have a lot of categories or when your legend is taking up too much space inside your plot. By moving it outside, you can make your plot cleaner and more aesthetically pleasing.\n\n# Examples\n\n## Step 1: Create Your Data\n\nLet's start by creating some sample data. We'll use a simple scatterplot to demonstrate how to draw a legend outside of the plot. Imagine we have data on two different species of flowers, and we want to distinguish them with different colors.\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Sample data\nset.seed(123)\ndata <- data.frame(\n x = rnorm(20),\n y = rnorm(20),\n species = rep(c(\"A\", \"B\"), each = 10)\n)\n```\n:::\n\n\n## Step 2: Create the Plot\n\nNext, let's create a scatterplot of our data using the `plot()` function.\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Create the scatterplot\nplot(\n data$x, data$y,\n pch = ifelse(data$species == \"A\", 16, 17),\n col = ifelse(data$species == \"A\", \"red\", \"blue\"),\n main = \"Scatterplot with Legend Outside\"\n)\n\n# create margin around plot\npar(mar = c(3, 3, 3, 8), xpd = TRUE)\n\n# Draw the legend outside the plot with inset\nlegend(\n \"topright\", # Position of the legend\n legend = c(\"Species A\", \"Species B\"), # Legend labels\n pch = c(16, 17), # Point shapes\n col = c(\"red\", \"blue\"), # Colors\n bty = \"n\", # No box around the legend\n inset = c(-0.1, 0) # Adjust the inset (move it to the left\n)\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-2-1.png){width=672}\n:::\n:::\n\n\nIn this code, we're using the `pch` argument to specify different point shapes based on the \"species\" variable and the `col` argument to set different colors. This creates a scatterplot with points that represent two species, A and B.\n\n## Step 3: Draw the Legend Outside\n\nNow comes the fun part—drawing the legend outside of the plot. We'll use the `legend()` function for this. Here's how you can do it:\n\n```R\n# Draw the legend outside the plot with inset\nlegend(\n \"topright\", # Position of the legend\n legend = c(\"Species A\", \"Species B\"), # Legend labels\n pch = c(16, 17), # Point shapes\n col = c(\"red\", \"blue\"), # Colors\n bty = \"n\", # No box around the legend\n inset = c(-0.16, 0) # Adjust the inset (move it to the left)\n)\n```\n\nIn this code, we specify the position of the legend using the `\"topright\"` argument. We also provide labels, point shapes, and colors for our legend. The `bty = \"n\"` argument removes the box around the legend for a cleaner look.\n\n# Example 2\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# create sample data frame\nsample_data <- data.frame(\n x = c(rnorm(50), rnorm(50, 5)), \n y = c(rnorm(50), rnorm(50, 5)), \n group = c(rep(1, 50), rep(2, 50))\n)\n\n# create margin around plot\npar(mar = c(3, 3, 3, 8), xpd = TRUE)\n\n# draw scatter plot\nplot(sample_data$x, sample_data$y, pch = 1, col = sample_data$group)\n\n# add legend outside of plot\nlegend(\n \"topright\", \n inset = c(-0.3, 0), \n legend = c(\"Group 1\", \"Group 2\"), \n pch = 1, \n col = c(\"black\", \"red\"), \n title = \"Groups\"\n)\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-3-1.png){width=672}\n:::\n:::\n\n\n## Step 4: Enjoy Your Plot\n\nThat's it! You've successfully drawn a legend outside of your plot. Your scatterplot now looks clean, and the legend is clearly separated.\n\n# Conclusion\n\nDrawing legends outside of a plot in base R is a valuable skill for data visualization. It allows you to present your data in a more organized and visually appealing way. I encourage you to try this technique with your own data. Experiment with different positions and styling options to make your plots shine.\n\nHappy plotting, and may your data visualization skills continue to grow! If you have any questions or want to learn more R tips and tricks, feel free to reach out.",
"markdown": "---\ntitle: \"Mastering Legends in R: Drawing Them Outside the Plot\"\nauthor: \"Steven P. Sanderson II, MPH\"\ndate: \"2023-10-06\"\ncategories: [rtip, viz]\n---\n\n\n# Introduction\n\nLegends are an essential part of data visualization. They help us understand the meaning behind the colors and shapes in our plots. But what if your legend is too big or clutters your plot? Fear not, fellow R enthusiast! In this blog post, we'll explore how to draw a legend outside of a plot using base R, with a step-by-step example that's easy to follow.\n\n# Why Draw Legends Outside the Plot?\n\nDrawing legends outside the plot is particularly useful when you have a lot of categories or when your legend is taking up too much space inside your plot. By moving it outside, you can make your plot cleaner and more aesthetically pleasing.\n\n# Examples\n\n## Step 1: Create Your Data\n\nLet's start by creating some sample data. We'll use a simple scatterplot to demonstrate how to draw a legend outside of the plot. Imagine we have data on two different species of flowers, and we want to distinguish them with different colors.\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Sample data\nset.seed(123)\ndata <- data.frame(\n x = rnorm(20),\n y = rnorm(20),\n species = rep(c(\"A\", \"B\"), each = 10)\n)\n```\n:::\n\n\n## Step 2: Create the Plot\n\nNext, let's create a scatterplot of our data using the `plot()` function.\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Create the scatterplot\nplot(\n data$x, data$y,\n pch = ifelse(data$species == \"A\", 16, 17),\n col = ifelse(data$species == \"A\", \"red\", \"blue\"),\n main = \"Scatterplot with Legend Outside\"\n)\n\n# create margin around plot\npar(mar = c(3, 3, 3, 8), xpd = TRUE)\n\n# Draw the legend outside the plot with inset\nlegend(\n \"topright\", # Position of the legend\n legend = c(\"Species A\", \"Species B\"), # Legend labels\n pch = c(16, 17), # Point shapes\n col = c(\"red\", \"blue\"), # Colors\n bty = \"n\", # No box around the legend\n inset = c(-0.1, 0) # Adjust the inset (move it to the left\n)\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-2-1.png){width=672}\n:::\n:::\n\n\nIn this code, we're using the `pch` argument to specify different point shapes based on the \"species\" variable and the `col` argument to set different colors. This creates a scatterplot with points that represent two species, A and B.\n\n## Step 3: Draw the Legend Outside\n\nWe already drew the legend but let's now understand what we did. We'll use the `legend()` function for this. Here's how you can do it:\n\n``` r\n# Draw the legend outside the plot with inset\nlegend(\n \"topright\", # Position of the legend\n legend = c(\"Species A\", \"Species B\"), # Legend labels\n pch = c(16, 17), # Point shapes\n col = c(\"red\", \"blue\"), # Colors\n bty = \"n\", # No box around the legend\n inset = c(-0.16, 0) # Adjust the inset (move it to the left)\n)\n```\n\nIn this code, we specify the position of the legend using the `\"topright\"` argument. We also provide labels, point shapes, and colors for our legend. The `bty = \"n\"` argument removes the box around the legend for a cleaner look.\n\n# Example 2\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# create sample data frame\nsample_data <- data.frame(\n x = c(rnorm(50), rnorm(50, 5)), \n y = c(rnorm(50), rnorm(50, 5)), \n group = c(rep(1, 50), rep(2, 50))\n)\n\n# create margin around plot\npar(mar = c(3, 3, 3, 8), xpd = TRUE)\n\n# draw scatter plot\nplot(sample_data$x, sample_data$y, pch = 1, col = sample_data$group)\n\n# add legend outside of plot\nlegend(\n \"topright\", \n inset = c(-0.3, 0), \n legend = c(\"Group 1\", \"Group 2\"), \n pch = 1, \n col = c(\"black\", \"red\"), \n title = \"Groups\"\n)\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-3-1.png){width=672}\n:::\n:::\n\n\n## Step 4: Enjoy Your Plot\n\nThat's it! You've successfully drawn a legend outside of your plot. Your scatterplot now looks clean, and the legend is clearly separated.\n\n# Conclusion\n\nDrawing legends outside of a plot in base R is a valuable skill for data visualization. It allows you to present your data in a more organized and visually appealing way. I encourage you to try this technique with your own data. Experiment with different positions and styling options to make your plots shine.\n\nHappy plotting, and may your data visualization skills continue to grow! If you have any questions or want to learn more R tips and tricks, feel free to reach out.\n",
"supporting": [
"index_files"
],
Expand Down
Loading

0 comments on commit f1a60d2

Please sign in to comment.