0

I want to create in R 3.2.2 a barplot with stacked bars but with each part of each bar splitted into an individual series.

Example data frame:

num_var_x = 14
num_var_y = 17

x = runif(num_var_x, 0.0, 1.0)
norm = x/sum(x)
data = data.frame(replicate(num_var_y,sample(norm)))

EDIT:

Thanks to Floo0 I have come up with this continuation of the code:

## preparing dataset for ggplot

require(ggplot2)
require(reshape2)
data$no <- seq_len(nrow(data))
data_molten <- melt(data, id.vars = "no")

data_molten_sort = data_molten[with(data_molten,order(no)),]

## removing elements from variable 'no' whose max. value is e.g. < 0.025

sequence = seq(from=1, to=(num_var_y*num_var_x-num_var_x)+1, by=num_var_x)
for(i in 1:length(sequence))
{
    if(isTRUE((max(data_molten_sort$value[(sequence[i]):((num_var_x+sequence[i])-(1))])) < 0.025))
    {
            data_molten_sort$value[(sequence[i]):((num_var_x+sequence[i])-(1))] = NA
    }
}

View(data_molten)


## preparing posterior exporting

#install.packages("Cairo"); "cairo" type in png() has a better quality
library("Cairo")
#preparing exporting
png(file="ggplot.png",type="cairo", width = 4, height = 5, units = 'in',pointsize=8,res=600)

## plotting

ggplot(data_molten[!is.na(data_molten$value),], aes(x = variable, y = value, fill = factor(no))) + 
    geom_bar(stat = "identity") + 
    scale_fill_hue(l=40) + facet_grid(no~., as.table=FALSE, scale="free_y", space = "free_y") + theme_minimal() +
    geom_vline(xintercept=max(as.numeric(data_molten$variable)) + 0.586, size=0.3) +
    theme(legend.position="none", 
          axis.text.x = element_text(angle = 90, colour="black", vjust = 0.4, hjust=1, size=8), 
          axis.title.x = element_blank(), axis.title.y = element_blank(), 
          axis.line.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank(), 
          strip.text.y=element_text(size = 8, colour="black", family="", angle=00,hjust = 0.1),
          panel.grid=element_blank(),
          axis.line=element_line(size = 0.3, colour = "black", linetype = "solid"),
          axis.ticks.x=element_line(size = 0.3, colour = "black", linetype = "solid"),
          panel.background=element_blank(), panel.margin = unit(0, "lines"))

## exporting barplot "ggplot.png" to directory
dev.off()

which produces the desired barplot:

https://i.stack.imgur.com/eDpI0.png

Miguel
  • 356
  • 1
  • 15
  • Sorry, I deleted accidentaly the comment somebody had done suggesting to use: beside = TRUE, but that produces http://i.imgur.com/U3EWLpy.png?1, which is not what I mean in http://i.imgur.com/kHoHpTQ.png?1 – Miguel Nov 22 '15 at 22:27

1 Answers1

3

You can use ggplot2 to do that as follows:

require(ggplot2)
require(reshape2)
data$no <- seq_len(nrow(data))
data_molten <- melt(data, id.vars = "no")

If you want the rows to have different hights, have a look at: Different y-Axis Labels facet_grid and sizes

I am not 100% sure in which direction you want the plot to be turned:

Version 1

ggplot(data_molten, aes(x = no, y = value, fill = variable)) + geom_bar(stat = "identity") +
  facet_grid(variable~.) + theme(legend.position="none")

enter image description here

Version 2
Thx bergant fot the comment

ggplot(data_molten, aes(x = variable, y = value, fill = factor(no))) + geom_bar(stat = "identity") +
  facet_grid(no~.) + theme(legend.position="none")

enter image description here

Original

ggplot(data_molten, aes(x = no, y = value, fill = variable)) + geom_bar(stat = "identity")

enter image description here

Community
  • 1
  • 1
Rentrop
  • 20,979
  • 10
  • 72
  • 100