Create bubble chart similar to d3.js force layout using ggplot2
Is it possible to make a bubble chart similar to this one using R, preferably ggplot2?

Given that there are three categories in this example, the properties are
- all circles attract one another (to clump circles together)
- collision detection (to stop circles overlapping)
- circles are attracted to one of three centers, depending on their category
Source: d3indepth.com/force-layout
data (though I am really sure what the data should look like for a plot of this kind)
set.seed(1)
dat <- data.frame(category = rep(c("A", "B", "C"), each = 10),
bubble = rep(1:10, 3),
radius = round(runif(30, min = 0.5, max = 3), 2),
stringsAsFactors = FALSE)
dat
I'm tagging this with d3.js - which I am not familiar with - although the question is about But feel free to edit the tags and/or post.R. I hope to attract community members that are familiar with either.
Thanks.
r ggplot2 data-visualization circle-pack
add a comment |
Is it possible to make a bubble chart similar to this one using R, preferably ggplot2?

Given that there are three categories in this example, the properties are
- all circles attract one another (to clump circles together)
- collision detection (to stop circles overlapping)
- circles are attracted to one of three centers, depending on their category
Source: d3indepth.com/force-layout
data (though I am really sure what the data should look like for a plot of this kind)
set.seed(1)
dat <- data.frame(category = rep(c("A", "B", "C"), each = 10),
bubble = rep(1:10, 3),
radius = round(runif(30, min = 0.5, max = 3), 2),
stringsAsFactors = FALSE)
dat
I'm tagging this with d3.js - which I am not familiar with - although the question is about But feel free to edit the tags and/or post.R. I hope to attract community members that are familiar with either.
Thanks.
r ggplot2 data-visualization circle-pack
1
Maybe a start: Circle packing in R Graph gallery. Several packages available.
– Henrik
Nov 20 at 23:01
@Henrik Looks very promising! Will give this a try. Thanks for the pointer.
– markus
Nov 20 at 23:03
add a comment |
Is it possible to make a bubble chart similar to this one using R, preferably ggplot2?

Given that there are three categories in this example, the properties are
- all circles attract one another (to clump circles together)
- collision detection (to stop circles overlapping)
- circles are attracted to one of three centers, depending on their category
Source: d3indepth.com/force-layout
data (though I am really sure what the data should look like for a plot of this kind)
set.seed(1)
dat <- data.frame(category = rep(c("A", "B", "C"), each = 10),
bubble = rep(1:10, 3),
radius = round(runif(30, min = 0.5, max = 3), 2),
stringsAsFactors = FALSE)
dat
I'm tagging this with d3.js - which I am not familiar with - although the question is about But feel free to edit the tags and/or post.R. I hope to attract community members that are familiar with either.
Thanks.
r ggplot2 data-visualization circle-pack
Is it possible to make a bubble chart similar to this one using R, preferably ggplot2?

Given that there are three categories in this example, the properties are
- all circles attract one another (to clump circles together)
- collision detection (to stop circles overlapping)
- circles are attracted to one of three centers, depending on their category
Source: d3indepth.com/force-layout
data (though I am really sure what the data should look like for a plot of this kind)
set.seed(1)
dat <- data.frame(category = rep(c("A", "B", "C"), each = 10),
bubble = rep(1:10, 3),
radius = round(runif(30, min = 0.5, max = 3), 2),
stringsAsFactors = FALSE)
dat
I'm tagging this with d3.js - which I am not familiar with - although the question is about But feel free to edit the tags and/or post.R. I hope to attract community members that are familiar with either.
Thanks.
r ggplot2 data-visualization circle-pack
r ggplot2 data-visualization circle-pack
edited Nov 21 at 12:43
asked Nov 20 at 22:56
markus
10.6k1029
10.6k1029
1
Maybe a start: Circle packing in R Graph gallery. Several packages available.
– Henrik
Nov 20 at 23:01
@Henrik Looks very promising! Will give this a try. Thanks for the pointer.
– markus
Nov 20 at 23:03
add a comment |
1
Maybe a start: Circle packing in R Graph gallery. Several packages available.
– Henrik
Nov 20 at 23:01
@Henrik Looks very promising! Will give this a try. Thanks for the pointer.
– markus
Nov 20 at 23:03
1
1
Maybe a start: Circle packing in R Graph gallery. Several packages available.
– Henrik
Nov 20 at 23:01
Maybe a start: Circle packing in R Graph gallery. Several packages available.
– Henrik
Nov 20 at 23:01
@Henrik Looks very promising! Will give this a try. Thanks for the pointer.
– markus
Nov 20 at 23:03
@Henrik Looks very promising! Will give this a try. Thanks for the pointer.
– markus
Nov 20 at 23:03
add a comment |
1 Answer
1
active
oldest
votes
Needs some further work/investigation in the layout but here's an approach.
library(packcircles)
library(tidyverse)
set.seed(1)
dat <- data.frame(category = rep(c("A", "B", "C"), each = 10),
id = 1:30,
radius = round(runif(30, min = 0.5, max = 3), 2),
stringsAsFactors = FALSE)
#Create layouts for each group by splitting, mapping and recombining
dat.gg <- dat %>%
split(.$category) %>%
map(~circleProgressiveLayout(.x$radius, sizetype='radius')) %>%
imap_dfr(~circleLayoutVertices(.x, npoints=50) %>% mutate(category = .y))
#Do the thing
ggplot() +
geom_polygon(data = dat.gg, aes(x, y, group = id, fill = category), colour = "black", alpha = 0.6) +
facet_wrap(~category) +
scale_fill_viridis_d() +
theme_void() +
theme(legend.position="none", plot.margin=unit(c(0,0,0,0),"cm") ) +
coord_equal()

Created on 2018-11-20 by the reprex package (v0.2.1)
Thanks Jake. Really appreciate your help.
– markus
Nov 21 at 9:41
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402836%2fcreate-bubble-chart-similar-to-d3-js-force-layout-using-ggplot2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Needs some further work/investigation in the layout but here's an approach.
library(packcircles)
library(tidyverse)
set.seed(1)
dat <- data.frame(category = rep(c("A", "B", "C"), each = 10),
id = 1:30,
radius = round(runif(30, min = 0.5, max = 3), 2),
stringsAsFactors = FALSE)
#Create layouts for each group by splitting, mapping and recombining
dat.gg <- dat %>%
split(.$category) %>%
map(~circleProgressiveLayout(.x$radius, sizetype='radius')) %>%
imap_dfr(~circleLayoutVertices(.x, npoints=50) %>% mutate(category = .y))
#Do the thing
ggplot() +
geom_polygon(data = dat.gg, aes(x, y, group = id, fill = category), colour = "black", alpha = 0.6) +
facet_wrap(~category) +
scale_fill_viridis_d() +
theme_void() +
theme(legend.position="none", plot.margin=unit(c(0,0,0,0),"cm") ) +
coord_equal()

Created on 2018-11-20 by the reprex package (v0.2.1)
Thanks Jake. Really appreciate your help.
– markus
Nov 21 at 9:41
add a comment |
Needs some further work/investigation in the layout but here's an approach.
library(packcircles)
library(tidyverse)
set.seed(1)
dat <- data.frame(category = rep(c("A", "B", "C"), each = 10),
id = 1:30,
radius = round(runif(30, min = 0.5, max = 3), 2),
stringsAsFactors = FALSE)
#Create layouts for each group by splitting, mapping and recombining
dat.gg <- dat %>%
split(.$category) %>%
map(~circleProgressiveLayout(.x$radius, sizetype='radius')) %>%
imap_dfr(~circleLayoutVertices(.x, npoints=50) %>% mutate(category = .y))
#Do the thing
ggplot() +
geom_polygon(data = dat.gg, aes(x, y, group = id, fill = category), colour = "black", alpha = 0.6) +
facet_wrap(~category) +
scale_fill_viridis_d() +
theme_void() +
theme(legend.position="none", plot.margin=unit(c(0,0,0,0),"cm") ) +
coord_equal()

Created on 2018-11-20 by the reprex package (v0.2.1)
Thanks Jake. Really appreciate your help.
– markus
Nov 21 at 9:41
add a comment |
Needs some further work/investigation in the layout but here's an approach.
library(packcircles)
library(tidyverse)
set.seed(1)
dat <- data.frame(category = rep(c("A", "B", "C"), each = 10),
id = 1:30,
radius = round(runif(30, min = 0.5, max = 3), 2),
stringsAsFactors = FALSE)
#Create layouts for each group by splitting, mapping and recombining
dat.gg <- dat %>%
split(.$category) %>%
map(~circleProgressiveLayout(.x$radius, sizetype='radius')) %>%
imap_dfr(~circleLayoutVertices(.x, npoints=50) %>% mutate(category = .y))
#Do the thing
ggplot() +
geom_polygon(data = dat.gg, aes(x, y, group = id, fill = category), colour = "black", alpha = 0.6) +
facet_wrap(~category) +
scale_fill_viridis_d() +
theme_void() +
theme(legend.position="none", plot.margin=unit(c(0,0,0,0),"cm") ) +
coord_equal()

Created on 2018-11-20 by the reprex package (v0.2.1)
Needs some further work/investigation in the layout but here's an approach.
library(packcircles)
library(tidyverse)
set.seed(1)
dat <- data.frame(category = rep(c("A", "B", "C"), each = 10),
id = 1:30,
radius = round(runif(30, min = 0.5, max = 3), 2),
stringsAsFactors = FALSE)
#Create layouts for each group by splitting, mapping and recombining
dat.gg <- dat %>%
split(.$category) %>%
map(~circleProgressiveLayout(.x$radius, sizetype='radius')) %>%
imap_dfr(~circleLayoutVertices(.x, npoints=50) %>% mutate(category = .y))
#Do the thing
ggplot() +
geom_polygon(data = dat.gg, aes(x, y, group = id, fill = category), colour = "black", alpha = 0.6) +
facet_wrap(~category) +
scale_fill_viridis_d() +
theme_void() +
theme(legend.position="none", plot.margin=unit(c(0,0,0,0),"cm") ) +
coord_equal()

Created on 2018-11-20 by the reprex package (v0.2.1)
answered Nov 21 at 1:04
Jake Kaupp
5,22221428
5,22221428
Thanks Jake. Really appreciate your help.
– markus
Nov 21 at 9:41
add a comment |
Thanks Jake. Really appreciate your help.
– markus
Nov 21 at 9:41
Thanks Jake. Really appreciate your help.
– markus
Nov 21 at 9:41
Thanks Jake. Really appreciate your help.
– markus
Nov 21 at 9:41
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402836%2fcreate-bubble-chart-similar-to-d3-js-force-layout-using-ggplot2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Maybe a start: Circle packing in R Graph gallery. Several packages available.
– Henrik
Nov 20 at 23:01
@Henrik Looks very promising! Will give this a try. Thanks for the pointer.
– markus
Nov 20 at 23:03