More allsey in R

I use ggplot2 to create chart maps in R. They look amazing and everyone is very happy - except that they would like to have the values ​​of the bullseye layers plotted on the chart. I would be happy to just put them in the lower right corner of the plot, or even in the plot fields, but it's hard for me to do this.

Here again is the example data:

critters <- structure(list(Zoo = "Omaha", Animals = 50, Bears = 10, PolarBears = 3), .Names = c("Zoo", "Animals", "Bears", "PolarBears"), row.names = c(NA, -1L), class = "data.frame") 

And how to do it:

 d <- data.frame(animal=factor(c(rep("Animals", critters$Animals), rep("Bears", critters$Bears), rep("PolarBears", critters$PolarBears)), levels = c("PolarBears", "Bears", "Animals"), ordered= TRUE)) grr <- ggplot(d, aes(x = factor(1), fill = factor(animal))) + geom_bar() + coord_polar() + labs(x = NULL, fill = NULL) + scale_fill_manual(values = c("firebrick2", "yellow2", "green3")) + opts(title = paste("Animals, Bears and Polar Bears:\nOmaha Zoo", sep="")) 

I would like to add a list, say, to the lower right corner of this plot, saying

 Animals: 50 Bears: 10 PolarBears: 3 

But I can’t figure out how to do this. My efforts so far with annotate() have been frustrated, in part by polar coordinates. If I need to add numbers to the title, so be it, but I always hope for a more elegant solution. Thanks in advance.

EDIT: An important note for those who come after: the “bullseye” is a line chart mapped to polar coordinates. The default ggplot2 value for bar charts is reasonable to stack them. However, this means that your bullseye rings will also be stacked (for example, the radius in my example is the sum of all three groups, 63, and not the size of the largest group, 50). I do not think that most people expect from the title page, especially when groups are nested. Using geom_bar(position = position_identity()) will turn stacked rings into layered circles.

EDIT 2: Example from ggplot2 docs:
enter image description here

+6
r plot ggplot2
source share
2 answers

You can add numbers to the legend.

 library(ggplot2) critters <- structure(list(Zoo = "Omaha", Animals = 50, Bears = 10, PolarBears = 3), .Names = c("Zoo", "Animals", "Bears", "PolarBears"), row.names = c(NA, -1L), class = "data.frame") d <- data.frame(animal=factor(c(rep("Animals", critters$Animals), rep("Bears", critters$Bears), rep("PolarBears", critters$PolarBears)), levels = c("PolarBears", "Bears", "Animals"), ordered= TRUE)) levels(d$animal) <- apply(data.frame(table(d$animal)), 1, paste, collapse = ": ") ggplot(d, aes(x = factor(1), fill = factor(animal))) + geom_bar() + coord_polar() + labs(x = NULL, fill = NULL) + scale_fill_manual(values = c("firebrick2", "yellow2", "green3")) + opts(title = paste("Animals, Bears and Polar Bears:\nOmaha Zoo", sep="")) 
+4
source share

You can also add it directly to the plot:

 grr <- ggplot(d, aes(x = factor(1), fill = factor(animal))) + geom_bar() + coord_polar() + labs(x = NULL, fill = NULL) + scale_fill_manual(values = c("firebrick2", "yellow2", "green3")) + opts(title = paste("Animals, Bears and Polar Bears:\nOmaha Zoo", sep=""))+ geom_text(y=c(3,10,50)-3,label=c("3","10","50"),size=4) grr 
+5
source share

All Articles