Reputation: 1531
this is my code in R:
prod <- read.csv("/tmp/pepper.csv",header=T,sep="\t")
png("/tmp/image.png", width=1000, height=1000)
datm <- melt(cbind(prod,ind = rownames(prod)),is.vars = c('ind'))
ggplot(datm,aes(x = codigo_inver,y = value,fill = factor(variable))) +
geom_bar(stat='identity', position = "fill" ) +
coord_flip()+
scale_colour_manual(values = c("red","orange", "green"))
dev.off()
Data set is:
green orange red codigo_inver
48.40 30.22 21.38 7_7726-14
32.31 28.18 39.51 8_7726-14
46.74 30.13 23.13 9_7577-4
55.13 32.80 12.06 21_7562-4
51.30 30.76 17.94 28_7614-1
40.65 37.75 21.60 30_7094-2
and it produce:
But the colors mismatch with
values = c("red","orange", "green")
In the picture, red should be green , green should be orange and blue should be red.
Colours must match with the column name data, red is red, orange is orange and green is green.
How can I match this colours with original column name color?
thanks in advance!
Upvotes: 0
Views: 528
Reputation: 94172
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...
Upvotes: 1
Reputation: 4187
As you use fill
to assign the colors, you should also use the corresponding scale:
scale_fill_manual(values = c("red","orange", "green"))
now it should work
Upvotes: 1