Skip to content

Commit

Permalink
new post
Browse files Browse the repository at this point in the history
  • Loading branch information
spsanderson committed Oct 23, 2023
1 parent ee4a118 commit 85ed156
Show file tree
Hide file tree
Showing 12 changed files with 1,341 additions and 606 deletions.
16 changes: 16 additions & 0 deletions _freeze/posts/2023-10-23/index/execute-results/html.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"hash": "1fefeebe24a4f1e13667c9a0fdbd334c",
"result": {
"markdown": "---\ntitle: \"How to Create a Bubble Chart in R using ggplot2\"\nauthor: \"Steven P. Sanderson II, MPH\"\ndate: \"2023-10-23\"\ncategories: [rtip, viz, ggplot2]\n---\n\n\n# Introduction\n\nBubble charts are a great way to visualize data with three dimensions. The size of the bubbles represents a third variable, which can be used to show the importance of that variable or to identify relationships between the three variables.\n\nTo create a bubble chart in R using `ggplot2`, you will need to use the `geom_point()` function. This function will plot points on your chart, and you can use the size aesthetic to control the size of the points.\n\n# Getting Started\n\nBefore we begin, ensure you have R and `ggplot2` installed. If you don't have `ggplot2`, you can install it with the command:\n\n```R\ninstall.packages(\"ggplot2\")\n```\n# Examples \n\n## Example 1: Basic Bubble Chart\n\nLet's start with a simple example using randomly generated data. We'll create a bubble chart that shows the relationship between two variables and represents a third variable using bubble sizes.\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Load ggplot2 library\nlibrary(ggplot2)\n\n# Generate random data\nset.seed(123)\ndata <- data.frame(\n x = rnorm(10),\n y = rnorm(10),\n size = runif(10, min = 5, max = 20)\n)\n\n# Create a bubble chart\nggplot(data, aes(x, y, size = size)) +\n geom_point() +\n scale_size_continuous(range = c(3, 10)) +\n labs(\n title = \"Basic Bubble Chart\", \n x = \"X-Axis\", \n y = \"Y-Axis\",\n size = \"Y\") +\n theme_minimal()\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/ggplot2_bubble_chart-1.png){width=672}\n:::\n:::\n\n\nIn this example, we create a bubble chart with random data points, where `x` and `y` are the coordinates, and `size` represents the bubble size. The `geom_point()` function is used to add the points, and we adjust the size range using `scale_size_continuous()`.\n\n## Example 2: Customizing Bubble Chart\n\nNow, let's customize our bubble chart further. We'll use a sample dataset to visualize car data, with car names on the bubbles.\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Sample data\ncars <- mtcars\ncars$name <- rownames(cars)\n\n# Create a bubble chart\nggplot(cars, aes(x = mpg, y = disp, size = hp, label = name)) +\n geom_point() +\n geom_text(vjust = 1, hjust = 1, size = 3) +\n scale_size_continuous(range = c(3, 20)) +\n labs(\n title = \"Customized Bubble Chart\", \n x = \"Miles per Gallon\", \n y = \"Displacement\",\n size = \"HP\") +\n theme_minimal()\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/ggplot2_bubble_chart2-1.png){width=672}\n:::\n:::\n\n\nIn this example, we're using the mtcars dataset to create a bubble chart that displays car names using `geom_text()`. The `vjust` and `hjust` parameters control the text placement.\n\n# Other Ways to Use Bubble Charts\n\nHere are a few examples of bubble charts that you can create using ggplot2:\n\n* A bubble chart showing the relationship between the population, GDP, and land area of different countries.\n* A bubble chart showing the relationship between the sales, marketing budget, and customer satisfaction of different companies.\n* A bubble chart showing the relationship between the temperature, humidity, and wind speed at different locations on a map.\n\n\n# You Try!\n\nCreating bubble charts in R is not only informative but also fun! Encourage your readers to experiment with their own datasets and customize these examples. The ggplot2 library offers a wealth of possibilities for creating beautiful and insightful visualizations. So, don't hesitate to dive into R and start charting your data with bubbles!\n\nI hope this guide helps you and your readers in creating engaging bubble charts in R using ggplot2. If you have any questions or need further clarification, feel free to ask. Happy coding, Steve!",
"supporting": [
"index_files"
],
"filters": [
"rmarkdown/pagebreak.lua"
],
"includes": {},
"engineDependencies": {},
"preserve": {},
"postProcess": true
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
483 changes: 262 additions & 221 deletions docs/index.html

Large diffs are not rendered by default.

658 changes: 275 additions & 383 deletions docs/index.xml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/listings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
"listing": "/index.html",
"items": [
"/posts/2023-10-23/index.html",
"/posts/2023-10-20/index.html",
"/posts/2023-10-19/index.html",
"/posts/2023-10-18/index.html",
Expand Down
670 changes: 670 additions & 0 deletions docs/posts/2023-10-23/index.html

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 22 additions & 1 deletion docs/search.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion 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-20T12:18:44.460Z</lastmod>
<lastmod>2023-10-23T12:35:35.806Z</lastmod>
</url>
<url>
<loc>https://www.spsanderson.com/steveondata/posts/rtip-2023-04-06/index.html</loc>
Expand Down Expand Up @@ -508,4 +508,8 @@
<loc>https://www.spsanderson.com/steveondata/posts/2023-10-20/index.html</loc>
<lastmod>2023-10-20T12:19:01.002Z</lastmod>
</url>
<url>
<loc>https://www.spsanderson.com/steveondata/posts/2023-10-23/index.html</loc>
<lastmod>2023-10-23T12:35:43.626Z</lastmod>
</url>
</urlset>
90 changes: 90 additions & 0 deletions posts/2023-10-23/index.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
---
title: "How to Create a Bubble Chart in R using ggplot2"
author: "Steven P. Sanderson II, MPH"
date: "2023-10-23"
categories: [rtip, viz, ggplot2]
---

# Introduction

Bubble charts are a great way to visualize data with three dimensions. The size of the bubbles represents a third variable, which can be used to show the importance of that variable or to identify relationships between the three variables.

To create a bubble chart in R using `ggplot2`, you will need to use the `geom_point()` function. This function will plot points on your chart, and you can use the size aesthetic to control the size of the points.

# Getting Started

Before we begin, ensure you have R and `ggplot2` installed. If you don't have `ggplot2`, you can install it with the command:

```R
install.packages("ggplot2")
```
# Examples

## Example 1: Basic Bubble Chart

Let's start with a simple example using randomly generated data. We'll create a bubble chart that shows the relationship between two variables and represents a third variable using bubble sizes.

```{r ggplot2_bubble_chart, message=FALSE}
# Load ggplot2 library
library(ggplot2)
# Generate random data
set.seed(123)
data <- data.frame(
x = rnorm(10),
y = rnorm(10),
size = runif(10, min = 5, max = 20)
)
# Create a bubble chart
ggplot(data, aes(x, y, size = size)) +
geom_point() +
scale_size_continuous(range = c(3, 10)) +
labs(
title = "Basic Bubble Chart",
x = "X-Axis",
y = "Y-Axis",
size = "Y") +
theme_minimal()
```

In this example, we create a bubble chart with random data points, where `x` and `y` are the coordinates, and `size` represents the bubble size. The `geom_point()` function is used to add the points, and we adjust the size range using `scale_size_continuous()`.

## Example 2: Customizing Bubble Chart

Now, let's customize our bubble chart further. We'll use a sample dataset to visualize car data, with car names on the bubbles.

```{r ggplot2_bubble_chart2}
# Sample data
cars <- mtcars
cars$name <- rownames(cars)
# Create a bubble chart
ggplot(cars, aes(x = mpg, y = disp, size = hp, label = name)) +
geom_point() +
geom_text(vjust = 1, hjust = 1, size = 3) +
scale_size_continuous(range = c(3, 20)) +
labs(
title = "Customized Bubble Chart",
x = "Miles per Gallon",
y = "Displacement",
size = "HP") +
theme_minimal()
```

In this example, we're using the mtcars dataset to create a bubble chart that displays car names using `geom_text()`. The `vjust` and `hjust` parameters control the text placement.

# Other Ways to Use Bubble Charts

Here are a few examples of bubble charts that you can create using ggplot2:

* A bubble chart showing the relationship between the population, GDP, and land area of different countries.
* A bubble chart showing the relationship between the sales, marketing budget, and customer satisfaction of different companies.
* A bubble chart showing the relationship between the temperature, humidity, and wind speed at different locations on a map.


# You Try!

Creating bubble charts in R is not only informative but also fun! Encourage your readers to experiment with their own datasets and customize these examples. The ggplot2 library offers a wealth of possibilities for creating beautiful and insightful visualizations. So, don't hesitate to dive into R and start charting your data with bubbles!

I hope this guide helps you and your readers in creating engaging bubble charts in R using ggplot2. If you have any questions or need further clarification, feel free to ask. Happy coding, Steve!

0 comments on commit 85ed156

Please sign in to comment.