0
df <- data.frame(sd = c('05AP.U1 1 ','05AP.U2 1','07RT.U1 1'),percentpl = c(50,40,80),percentan = c(50,60,20),count = c(100,100,100))

sd        percentpl  percentan  count
1  05AP.U1       50        50   100
9  03KM.U2       40        60   100 
12 07RT.U1       80        20   100

Is it possiple to draw a stacked,percent barplot onlyon the base of df. I tried it with ggplot but failed on the fill variable.

ggplot(df, aes(fill=`percentpl`+`percentan`, y=count, x=`sd`)) + 
    geom_bar(position="fill", stat="identity")+ theme(axis.text = element_text(angle = 90))

I have seen couple of other similar questions R ggplot2 stacked barplot, percent on y axis, counts in bars Add percentage labels to a stacked barplot

takeITeasy
  • 350
  • 3
  • 19

1 Answers1

1

Maybe you are looking for this. As your count variable adds to 100 because of values in percentpl and percentan you can reshape based on those variables and obtain the filled plot:

library(ggplot2)
library(tidyverse)
#Data
df <- data.frame(sd = c('05AP.U1 1 ','05AP.U2 1','07RT.U1 1'),
                 percentpl = c(50,40,80),
                 percentan = c(50,60,20),
                 count = c(100,100,100))
#Melt and plot
df %>% select(-count) %>%
  pivot_longer(cols = -sd) %>%
  ggplot(aes(x=sd,y=value,fill=name)) + 
  geom_bar(position="fill", stat="identity")+
  theme(axis.text = element_text(angle = 90))

Output:

enter image description here

Duck
  • 39,058
  • 13
  • 42
  • 84