D3 Bubble Chart
When using the Bubble Chart, the data must be flattened. In the example you linked, there's a call to a function called "classes" which for every node, returns the class as a new object with the value
property containing the size. (Your code above suggests you omitted this call).
Later, the call to the function bubble.nodes
uses the value
property of each object created by the classes function (which were pushed into a single array) to compute the x and y (which in turn is used within the transform function). See the d3 docs for more info about the pack function. This same nodes
function also computes the radius (as r
) which is used later.
It's that call to nodes
that determines the complete layout of the Bubble chart. The remainder of the code in the sample is all about constructing the necessary SVG elements such as a circle and text in the positions specified by the results computed by the nodes
function.
So you can't directly pass the json data to the bubble.nodes function unless it meets specific requirements (such as having a value
property).
Here is a working example on jsfiddle.net.
Create bubble chart with biggest bubble at the center
This should do, the main idea being that you sort by the value of the radius, so the first is the biggest, then shift the values around it (odd on one side, even on the other) so that the values are decreasing both ways.
Further explanations in the code.
library(plotrix)
library(RColorBrewer)
# Set the random seed, to get reproducible results
set.seed(54321)
# Generate some random values for the radius
num.circles <- 11
rd <- runif(num.circles, 1, 20)
df <- data.frame(labels=paste("Lbl", 1:num.circles), radius=rd)
# Sort by descending radius. The biggest circle is always row 1
df <- df[rev(order(df$radius)),]
# Now we want to put the biggest circle in the middle and the others on either side
# To do so we reorder the data frame taking the even values first reversed, then the odd values.
# This ensure the biggest circle is in the middle
df <- df[c(rev(seq(2, num.circles, 2)), seq(1, num.circles, 2)),]
# Space between the circles. 0.2 * average radius seems OK
space.between <- 0.2 * mean(df$radius)
# Creat an empty plot
plot(0, 0, "n", axes=FALSE, bty="n", xlab="", ylab="",
xlim=c(0, sum(df$radius)*2+space.between*num.circles),
ylim=c(0, 2.5 * max(df$radius)))
# Draw the circle at half the height of the biggest circle (plus some padding)
xx <- 0
mid.y <- max(df$radius) * 1.25
# Some nice degrading tones of blue
colors <- colorRampPalette(brewer.pal(8,"Blues"))(num.circles/2)
for (i in 1:nrow(df))
{
row <- df[i,]
x <- xx + row$radius + i*space.between
y <- mid.y
# Draw the circle
draw.circle(x, y, row$radius,
col=colors[abs(num.circles/2-i)])
# Add the label
text(x, y, row$labels, cex=0.6)
# Update current x position
xx <- xx + row$radius * 2
}
The result:
Live version on RFiddle.
How do I generate multilevel JSON with R?
Maybe something like this would do the job? Split
the dataframe on the column cat
, then drop the first column from each resulting dataframe and convert to JSON using toJSON
from the jsonlite
package.
JSONoutput <- jsonlite::toJSON(lapply(split(data, data$cat), function(x) {x[,-1]}))
Related Topics
How to Get All Arguments of a Function as Single Object Inside That Function
Mixing Jsf El in a JavaScript File
Can Nokogiri Interpret JavaScript? - Web Scraping
How to Use Ranges in a Switch Case Statement Using JavaScript
Jquery: Load Txt File and Insert into Div
How Does a Function in a Loop (Which Returns Another Function) Work
Youtube Iframe Player API - Onstatechange Not Firing
Can a Site Invoke a Browser Extension
Convert Camelcasetext to Title Case Text
Check If One Date Is Between Two Dates
How to Disable Tree Shaking in Rollupjs
Angular 4.3.3 Httpclient:How Get Value from the Header of a Response
Wicketpdf Rendering Table Not Aligned Properly and Footer Place at Last Page
How to Return a Variable from Google Maps JavaScript Geocoder Callback
How to Run Node Js Code from Npm Inside of Swift