Determine tag size by category in geom_bar

I am trying to build a complex barcher using geom_bar and mark each category with its own value within barchart. Since some categories have small values, the height of the corresponding segment of velvet is sometimes small. So, I'm trying to adjust the size of labels using geom_text .
I tried to define a vector called size , which changes depending on the value of the variable that I am trying to build, but although the size of the labels varies between categories, it does not seem to be related to the values.In addition, I'm not sure why I get the legend for my label size on the right side of the graph.
Here is a split version of the code I'm using:

 library(ggplot2) library(plyr) library(scales) Var1 = as.factor(rep(c("A", "B", "C", "D", "E", "F"),2)) Var2 = as.factor(rep(c("Y1","Y2"),each=6)) Freq = c(0.4, 0.1, 0.3, 0.1, 0.05, 0.05,0.2,0.2,0.3,0.2,0.05,0.05) Data = data.frame(Var1, Var2, Freq) Data <- ddply(Data, .(Var2), mutate, y = cumsum(Freq)-Freq/2) size = ifelse(Data$Freq > 0.05, 10, 3) label = paste(round(Data$Freq*100,0),"%", sep = "") p = ggplot(data = Data, aes(x = factor(''), y = Freq, fill = Var1)) + geom_bar(stat = "identity",position = "fill", width = 1) + scale_fill_brewer(palette = 3) + facet_grid(facets = . ~ Var2) + geom_text(aes(y = y, label = label, position ="identity", face = "bold"), size = size, hjust=0.5, vjust=0.5) + xlab('') + ylab('') + labs(fill = '') + ggtitle('Example') + theme(axis.text.y = element_text(size=14,face="bold"), panel.background = element_blank(), plot.title = element_text(size = 20, colour = "black", face = "bold")) p 

As far as I can see, the problem is caused by faces, as this slightly simplified version (i.e. without faces) works fine:

 library(ggplot2) library(plyr) library(scales) Var1 = as.factor(c("A", "B", "C", "D", "E", "F")) Freq = c(0.4, 0.1, 0.3, 0.1, 0.05, 0.05) y = cumsum(Freq)-Freq/2 Data = data.frame(Var1, Freq, y) size = ifelse(Data$Freq > 0.05, 10, 3) label = paste(round(Data$Freq*100,0),"%", sep = "") p = ggplot(data = Data, aes(x = factor(''), y = Freq, fill = Var1)) + geom_bar(stat = "identity",position = "fill", width = 1) + scale_fill_brewer(palette = 3) + geom_text(aes(y = y, label = label, position ="identity", face = "bold"), size = size, hjust=0.5, vjust=0.5) + xlab('') + ylab('') + labs(fill = '') + ggtitle('Example') + theme(axis.text.y = element_text(size=14,face="bold"), panel.background = element_blank(), plot.title = element_text(size = 20, colour = "black", face = "bold")) p 
+6
source share
1 answer

By adding size to my data frame and moving these parameters within geom_text aes () `, it seems to be normal for me. I think..

 Data$size <- size p <- ggplot(data = Data, aes(x = factor(''), y = Freq, fill = Var1)) + geom_bar(stat = "identity",position = "fill", width = 1) + scale_fill_brewer(palette = 3) + geom_text(aes(y = y, label = label, position ="identity", face = "bold", size = size), hjust=0.5, vjust=0.5) + xlab('') + ylab('') + labs(fill = '') + ggtitle('Example') + theme(axis.text.y = element_text(size=14,face="bold"), panel.background = element_blank(), plot.title = element_text(size = 20, colour = "black", face = "bold")) + facet_grid(facets = . ~ Var2) + guides(size=FALSE) 

Also, if you add + guides(size=FALSE) to the end, since I did this, you will remove the size legend.

My explanation for this, perhaps incorrectly, is that after you have a facet, you still provide the full length of the size and do not allow the facet to slice the size data according to Var2 .

I think your calibration problem is that size you have only two sizes, you get big differences (maybe there should be some kind of relative scaling), maybe add + scale_size(range=c(6,10)) and play with it to get something more suitable? A size range of 6,10 looks a lot better for me.

enter image description here

+3
source

All Articles