Reputation: 6080
Imagine I have a pie bar in ggplot2,
data <- data.frame(cluster = paste("Cluster", 1:3), size = c(0.33, 0.33, 0.33))
data = rbind(data, data)
ggplot(data, aes(x = factor(1), fill = cluster, weight=size)) +
geom_bar(width = 1) + coord_polar(theta="y")+ theme_bw() +
scale_x_discrete("",breaks=NULL) + scale_y_continuous("",breaks=NULL) +
theme(panel.border=element_blank(),
strip.text=element_blank(),
strip.background=element_blank(),
legend.position="none",
panel.grid=element_blank())
There is another vector of strength that I is represented by a vector (0.2, -1, 1). I like to color each slices as a gradient from blue to red. say, blue, for -1, and and red for 1.
Upvotes: 0
Views: 776
Reputation: 701
First of all, I would suggest a clearer data frame which contains all the data at once, created as follows:
> data <- data.frame(cluster = paste("Cluster", 1:3), size = c(0.2, 0.3, 0.5), strength = c(0.2, -1, 1))
> data
cluster size strength
1 Cluster 1 0.2 0.2
2 Cluster 2 0.3 -1.0
3 Cluster 3 0.5 1.0
And then the following minimal code produces a pie chart with the fill colour diverging on a scale around midpoint zero depending on the cluster's strength value:
> ggplot(data, aes( x = factor(1), group = cluster, fill = strength, weight = size)) + geom_bar(width = 1) + coord_polar( theta = "y") + scale_fill_gradient2()
For reference, have a look at the scale_fill_gradient2 doc page examples in the geom_bar doc page: http://docs.ggplot2.org/current/scale_gradient2.html http://docs.ggplot2.org/current/geom_bar.html
Upvotes: 1