0
votes

I am trying to use ggplot to plot 2 data sets of mine. This is my code below.

data <- read.table("test.txt",sep="\t",header = F)
y <- data.frame(v1=data$V7,v2=data$V8)
head(y)

         v1         v2
1 -0.305037 0.00223546
2 -0.207126 0.26586100
3  0.290341 0.49381600
4 -0.345713 0.21023300
5 -1.249040 0.54090400
6 -0.340126 0.60984800

y2<-head(y,100)
d2<-melt(y2)
ggplot(d2,aes(y2=value, fill=variable)) + geom_density(alpha=0.25)

I was taking help from this post for my data set Overlay density plots I am getting this warning and the graph is not being generated:

Warning message: Computation failed in stat_density(): attempt to apply non-function

How can I make this work?

Thanks

2

2 Answers

1
votes

This seems to generate what I assume you're looking for. The biggest thing is to change y2= to x= in the ggplot call.

(I just used y as the values you provided. I assume this should work for whatever y you have)

The y2 <- head(y,100) is only necessary if you're trying to create the density plots for only the first 100 values.

y <- data.frame(v1 = c(-.305, -.207, .290, -.346, -1.25, -.340),
                v2 = c(.002, .266, .464, .210, .541, .610))

library('reshape2')
d2 <- melt(y)

library('ggplot2')
ggplot(d2, aes(x = value, fill = variable)) + geom_density(alpha = .25)
0
votes

In case anyone else sees this question and the accepted answer which relate to a particular case, the underlying problem that gives rise to the rather puzzling and misleading error message is something wrong with the aes(). I just got the same message when I just forgot to include aes().