0

How can I use geom_smooth() function to highlight my graph. . But I couldn't get the solution for this.

This is my dataset as:

   A1        A2      A3
21.09542  71.06014   0
21.09564  71.06064   1
21.09619  71.06128   1
21.09636  71.06242   2
21.09667  71.06564   0
21.09483  71.06619   3
.....

I have calculated the variance for the A1 and A2 and plotted via ggplot. A piece of code is given below:

var.A1 = var(A1)
var.A2 = var(A2)
plt <- ggplot(df4, aes(x = A1, y = A2, colour = A3), pch = 17) +geom_point()
plt + geom_errorbar(aes(x=A1,y=A2, ymin=A2-var.A2, ymax=A2+var.A2),width=0.001)+
geom_errorbarh(aes(xmin=A1-var.A1,xmax=A1+var.A1),height=0.001)
ayush
  • 343
  • 3
  • 20

1 Answers1

0

I am not sure what your problem was, but when I tried it with your data there seemed to be not enough points to generate a smoother. So I generated some more points.

# Generate the original data (I think)
raw <- c(
21.09542,  71.06014,   0,
21.09564,  71.06064,   1,
21.09619,  71.06128,   1,
21.09636,  71.06242,   2,
21.09667,  71.06564,   0,
21.09483,  71.06619,   3)
m <- matrix(raw,6,3,byrow=T)
df4 <- data.frame(m)
names(df4) <- c("A1","A2","A3")
df4$A3 <- as.factor(df4$A3)


# Generate a new data set of 100 points with similar statistics
npts <- 100
na1 <- rnorm(npts,mean(df4$A1),sd(df4$A1))
na2 <- rnorm(npts,mean(df4$A2),sd(df4$A2))
na3 <- sample(0:3,npts,replace=T)
df5 <- data.frame(A1=na1,A2=na2,A3=na3)
df5$A3 <- as.factor(df5$A3)

# now plot it
var.A1 = var(df5$A1)
var.A2 = var(df5$A2)

plt <- ggplot(df5, aes(x = A1, y = A2, colour = A3), pch = 17) +geom_point()  + geom_smooth()
plt + geom_errorbar(aes(x=A1,y=A2, ymin=A2-var.A2, ymax=A2+var.A2),width=0.001)+
  geom_errorbarh(aes(xmin=A1-var.A1,xmax=A1+var.A1),height=0.001) + geom_smooth()

print(plt)

This is what the plot looks like. enter image description here

Mike Wise
  • 22,131
  • 8
  • 81
  • 104
  • Mike this is not what I want. I have also produced the same plot but its plotting according to the A3. I want to highlight the standard deviation for the whole path. I am updating the question so that you can look into it. – ayush Jul 02 '15 at 23:13
  • Mike can you view this [link](http://stackoverflow.com/questions/31228362/how-can-i-highlight-variance-over-a-ggplot). There its shown clearly. and that's exactly what I need – ayush Jul 05 '15 at 23:00