Converting JSON Format to CSV to Upload Data Table in R to Produce D3 Bubble Chart

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: circles

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



Leave a reply



Submit