Reputation: 113
I would like to add a scatter plot to overlay onto my ggplot histogram in R.
On the geom_point() below it gives me an error : Error: mapping
must be created by aes()
Could you provide guidance ?
rand_nr <- 10000
rand_max <- 1
rand_min <- 0
lambda <- 1
x_min <- 0
x_max <- 10
bin_nr <- 50
diff_x <- (x_max - x_min) / bin_nr
edge <- seq(x_min, x_max, diff_x)
x_var <- seq(x_min, x_max, 0.01) # actual x variable
rand_var <- runif(rand_nr, rand_min, rand_max)
X <- tan(rand_var * pi / 2) # F(X)=(2/pi)*atan(x)
p <- 1 / (1 + (x_var^2))
X_df <- data.frame(X, group = "x")
P_df <- data.frame(p, group = "p")
plot(rand_var) # plot the random variables
ggplot(X_df, aes(X, probability (x),fill=group, colour=group)) +
geom_histogram(aes(y=..density..), breaks=edge, alpha=0.6,
position="identity", lwd=0.2) +
ggtitle("Normalized")
The working code is above. The non-working code is below
ggplot(X_df, aes(X, probability (x),fill=group, colour=group)) +
geom_histogram(aes(y=..density..), breaks=edge, alpha=0.6,
position="identity", lwd=0.2) +
geom_point(x_var,p,aes(x1,px1)) +
ggtitle("Normalized")
Upvotes: 0
Views: 859
Reputation: 4284
Is it what you are looking for:
ggplot(X_df, aes(x=X,fill=group, colour=group)) +
geom_histogram(aes(y=..density..), breaks=edge, alpha=0.6,
position="identity", lwd=0.2) +
geom_point(data=P_df,aes(x = x_var,y =p)) +
ggtitle("Normalized")
What has changed:
data=
argument in geom_point
x
and y
inside of aes
Upvotes: 2