r - Issue with geom_text when using position_dodge -


i saw this answer couldn't replicate it.

i data this:

df = data.frame(x = rep(sample(letters, 4), 2),                  y = round(runif(8,1,100),0),                  z = c(rep("group1",4), rep("group2",4)))  # add 'percent' column so:  df$perc[1:4] = df$y[1:4] / sum(df$y[1:4]) df$perc[5:8] = df$y[5:8] / sum(df$y[5:8])  # convert so: df$perc = paste(round(df$perc * 100, 1), "%", sep="")  # ggplot: library(ggplot2)  ggplot(df) +  geom_bar(aes(z, y, fill=x), position="dodge", stat="identity") +  geom_text(aes(z,y,label=perc), position=position_dodge(width=1), size=4) 

result:

enter image description here

i can't figure out did wrong.

just 1 minor change solves issue. need specify group=x inside geom_text(aes(...)) call.

ggplot(df) +  geom_bar(aes(z, y, fill=x), position=position_dodge(width=1), stat="identity") +  geom_text(aes(z,y,label=perc, group=x), position=position_dodge(width=1), size=4) 

enter image description here


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -