Pregunta

My dataset is:

green   orange  red codinver
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

, well I want to do something like:

enter image description here

where X axis is percent, and Y axis is codinver

I use this code:

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'))
print(datm)
ggplot(datm,aes(x = variable,y = value,fill = ind)) + 
  geom_bar(position = "fill") 
dev.off()

But it dont show nothing and give me: Mapping a variable to y and also using stat="bin".

Thanks in advance.

¿Fue útil?

Solución

You need to tell geom_bar() where to map. By default it tries to do stat='bin' (like a histogram). This will give you what you want:

geom_bar(stat='identity', position = "fill")

Otros consejos

To get the result with percentages on the x-axis and codinver on the y-axis, use this:

ggplot(datm, aes(x = variable, y = value, fill = ind)) + 
  geom_bar(stat='identity', position = "fill") +
  coord_flip()
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top