-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee4a118
commit 85ed156
Showing
12 changed files
with
1,341 additions
and
606 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Binary file added
BIN
+17.9 KB
_freeze/posts/2023-10-23/index/figure-html/ggplot2_bubble_chart-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+48.2 KB
_freeze/posts/2023-10-23/index/figure-html/ggplot2_bubble_chart2-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file added
BIN
+17.9 KB
docs/posts/2023-10-23/index_files/figure-html/ggplot2_bubble_chart-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+48.2 KB
docs/posts/2023-10-23/index_files/figure-html/ggplot2_bubble_chart2-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |