Create bubble chart similar to d3.js force layout using ggplot2












1














Is it possible to make a bubble chart similar to this one using R, preferably ggplot2?



enter image description here



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 R. I hope to attract community members that are familiar with either. But feel free to edit the tags and/or post.



Thanks.










share|improve this question




















  • 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














Is it possible to make a bubble chart similar to this one using R, preferably ggplot2?



enter image description here



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 R. I hope to attract community members that are familiar with either. But feel free to edit the tags and/or post.



Thanks.










share|improve this question




















  • 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








1







Is it possible to make a bubble chart similar to this one using R, preferably ggplot2?



enter image description here



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 R. I hope to attract community members that are familiar with either. But feel free to edit the tags and/or post.



Thanks.










share|improve this question















Is it possible to make a bubble chart similar to this one using R, preferably ggplot2?



enter image description here



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 R. I hope to attract community members that are familiar with either. But feel free to edit the tags and/or post.



Thanks.







r ggplot2 data-visualization circle-pack






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












1 Answer
1






active

oldest

votes


















4














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)






share|improve this answer





















  • Thanks Jake. Really appreciate your help.
    – markus
    Nov 21 at 9:41











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
});


}
});














draft saved

draft discarded


















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









4














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)






share|improve this answer





















  • Thanks Jake. Really appreciate your help.
    – markus
    Nov 21 at 9:41
















4














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)






share|improve this answer





















  • Thanks Jake. Really appreciate your help.
    – markus
    Nov 21 at 9:41














4












4








4






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)






share|improve this answer












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)







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 at 1:04









Jake Kaupp

5,22221428




5,22221428












  • 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




Thanks Jake. Really appreciate your help.
– markus
Nov 21 at 9:41


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Tonle Sap (See)

I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

Guatemaltekische Davis-Cup-Mannschaft