0

I want to produce a cumulative percent bar chart that sums to 100%. I've figured out how to get the frequencies into percents on a chart, but I need to collapse the chart into one bar. Any assistance you can provide in base R is appreciated.

barplot(prop.table(table(log$log1)), beside=FALSE, horiz = TRUE)

I want the chart to have just one bar adding to 100%. Currently, I have 4 bars. See image below: enter image description here

I want something like the graph below, but with just one horizontal bar. I can figure out the color part myself, but not how to collapse the 4 bars (in the above picture) into one.

enter image description here

freeazabird
  • 347
  • 2
  • 11
  • Please [make this question reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by providing some or all of `log` as plain text, and images of current output and desired output. – neilfws Jun 24 '19 at 23:06
  • 1
    You need [stacked barplot](https://stackoverflow.com/search?q=%5Br%5D+stacked+bar+plot) – mlt Jun 24 '19 at 23:26
  • So when I look at examples of stacked barplot, it looks like people are manually creating matrices. Is there a way for me to produce my desired results without doing that? – freeazabird Jun 25 '19 at 00:35

2 Answers2

0

If you have a dataframe:

df <- data.frame(c(75, 7, 5, 13), c("u", "v", "x", "z"), c("x", "x", "x", "x"))
names(df) <- c("Value", "Name", "xvar")

ggplot(df, aes(x = xvar, y = Value, fill = Name, label = Value)) +
  geom_bar(stat = "identity") +
  geom_text(position = position_stack(vjust = 0.5)) + 
  theme(axis.title.x=element_blank(), axis.text.x=element_blank(),  
        axis.ticks.x=element_blank())

Stacked barplot

tosdanielle
  • 163
  • 8
0

If you could provide some data to reference, it would be helpful.

I think is something like this that you want:

Here the code:

#Create data Randon data
set.seed(1124)
data=matrix(sample(1:30,4) , nrow=4)
colnames(data)=c("Values")
rownames(data)=c("2","3","3.5","4")

#create color palette:
library(RColorBrewer)
coul = brewer.pal(4, "Pastel2") 

#Transform this data in %
data_percentage=apply(data, 2, function(x){x/sum(x,na.rm=T)})

# Make a stacked barplot
barplot(data_percentage, col=coul , 
    horiz = TRUE, border="white", xlab="group",
    legend.text = row.names(data_percentage))    

stackbar

vpz
  • 984
  • 1
  • 15
  • 26