I am attempting to insert labels in a proportional barchart: one segment per section, as well as the percentage of each segment. With the help of Thoothal I managed to do this:
var1 & lt; - Factors (such as Chachar (c (1,1,2,3,1,4,3,2,3, 2,1,4,2,3,2,1,4,3,1,2)) ) Var2 & lt; - factor (as.character (c (1,4,2,3,4,2,1), 2,3,4,2,1,1,3,2,1,2,4,3,2) )) Data & lt; - data.frame (var1, var2) dat & lt; - ddply (data, (var1), function (.) {Res> <- cumsum (prop.table (table (factor (. $ Var2))) data.frame (lab = names (res), y = Ggplot (data, aes (x = var1)) + geom_bar (aes (fill = var2), position = 'fill') + geom_text (AES (label = lab, x = var1, y = y ), Data = dat) / code> I do not want to label the percent of each level, and for level names.
Any help appreciated !
You y
as variable var2
You are saying geom_text
to use. It is actually translated as as.numeric (data $ var2)
, which is translated for a range of 1-4
However, your barplot uses cumulative percentages.
So you have to calculate these posts first:
Library (GG Plot 2) Library (Piler) # bus facility for var1 & lt; - factor (as.character (c (1,1,2,3,1,4,3,2,3,2,1,4,2,3,2,1,4,3,1,2)) ) Var2 & lt; - Factors (such as Chachar (c (1,4,2,3,4,2,1,2,3,4,2,1,1,3,2,1,2,4,3), 2) )) Data & lt; - data.frame (var1, var2) dat & lt; - ddply (data, (var1), function (.) {Res> <- cumsum (prop.table (table (factor (. $ Var2)))) To use only the used levels, Factor Res2 <- prop.table (table (factor (. $ Var2)) # re-factor only used level data.frame (Lab = to use names) (AES (fill = var2) Position = 'fill') + geom_text (AES), g = (label = round (lab2, 2), x = var1, y = y), data = dat
Keeps labs at the end of each time if you want to offset them a bit, Then you should play in the creation of dat
.
Comments
Post a Comment