Question

I am making a stacked bar chart (goodbye pie chart) and I noticed that my numeric values are converted. The x-axis in the data are not followed.

I want my x-axis labels to stay the same and not divided by hundreds as seen in the result below:

enter image description here

require(reshape)
require(ggplot)

tab <- data.frame(
  AAA=c("ANNNK","ASA","ANNNK","ASA"),
  DDD=c("OAUDF","OAUDF","ANAN","ANAN"),
  BBB=as.numeric(c(83.927061,87.336000,16.072939,12.664000))
)
dat <- melt(tab)

plot <- ggplot(dat,aes(AAA,value,fill=as.factor(DDD))) + 
  geom_bar(position="fill", stat="identity") +
  scale_fill_brewer("NBAKDJSD", palette="OrRd") +
  labs(x="AAA", y="DDD (%)") +
  coord_flip() +
  theme_bw() +
  theme(
    axis.title=element_text(size=rel(2), face="bold"),
    axis.text=element_text(size=rel(1.5),color="#AAAAAA"),
    axis.title.x=element_text(vjust=-3),
    axis.title.y=element_text(vjust=-0.25),
    strip.text.x = element_text(size = rel(2)),
    legend.title=element_text(face="bold"),
    legend.position="right",
    plot.margin = unit(c(1,1.25,1.5,1.5), "cm"), # t,r,b,l
    panel.margin = unit(2, "lines")
  ) +
  xlim(rev(levels(dat$AAA)))
Was it helpful?

Solution

You have to remove position="fill" from the geom_bar() If you use position="fill" then bars shows relative proportions and bars are the same height (1). You already have data that sums to 100.

 + geom_bar(stat="identity")

If you want to use position="fill" and get 100% on the axis then you can use label=percent (you will need library scales for that) inside the scale_y_continuous().

library(scales)
ggplot(dat,aes(AAA,value,fill=as.factor(DDD))) + 
  geom_bar(position="fill", stat="identity") +
  scale_y_continuous(label=percent)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top