Get the colours from the levels of the factor, and use scale_fill_manual
:
ggplot(datm,aes(x = codigo_inver,y = value,fill = factor(variable))) +
geom_bar(stat='identity', position = "fill" ) +
coord_flip()+
scale_fill_manual(values = levels(factor(datm$variable)))
however the legend now looks a bit obvious...