Reputation: 485
I am finding to sets of coordinates (X,Y) and (Xe,Ye). Now I want to tidy the data and pair the values of X and Y's. Hence I want a 3 colums, one with (X,Y) or (Xe,Ye), one with X or Xe values and one with Y or Ye values.
Then I want in ggplot to map x-values to x, y-values to y and the (X,Y) or (Xe,Ye) to the col aesthetics.
I have done below know but there must be another way. I am using gather two times but I really want to learn how to do this nicer and more efficient.
Thanks,
N <- 10^6
X <- rchisq(N, 3)
w <- dexp(X, 1) / dchisq(X, 3)
Y <- log(1+X)*w #h(x)*w(x)
mean(Y)
sd(Y)/sqrt(N)
c(mean(Y)-2*sd(Y)/sqrt(N),
mean(Y)+2*sd(Y)/sqrt(N))
Xe <- rexp(N, 1/2)
we <- dexp(Xe, 1) / dexp(Xe, 1/2)
Ye <- log(1+Xe)*we
mean(Ye)
sd(Ye)/sqrt(N)
c(mean(Ye)-2*sd(Ye)/sqrt(N),
mean(Ye)+2*sd(Ye)/sqrt(N))
library(dplyr)
library(ggplot2)
library(tidyr)
tidy <- as_tibble(cbind(X, Xe, Y, Ye))
head(tidy)
Xtib <- as_tibble(cbind(X, Xe)) %>%
gather(Xs, Xvalue)
Ytib <- as_tibble(cbind(Y, Ye)) %>%
gather(Ys, Yvalue)
Alltib <- cbind(Xtib, Ytib)
ggplot(Alltib, aes(x=Xvalue, y=Yvalue, col = Xs)) +
geom_point()
Upvotes: 0
Views: 46
Reputation: 3647
The way you wrote it seems fine. You can avoid the intermediate data frames if you like:
library(tidyverse)
N <- 10^5
X <- rchisq(N, 3)
w <- dexp(X, 1) / dchisq(X, 3)
Y <- log(1+X)*w #h(x)*w(x)
Xe <- rexp(N, 1/2)
we <- dexp(Xe, 1) / dexp(Xe, 1/2)
Ye <- log(1+Xe)*we
tidy <- tibble(X, Xe) %>% gather(Xpoints, Xvals) %>%
cbind(gather(tibble(Y, Ye), Ypoints, Yvals)) %>%
mutate(coords = ifelse(Xpoints=="X", "p1", "p2") ) %>%
select(Xvals, Yvals, coords)
head(tidy)
#> Xvals Yvals coords
#> 1 5.873079 0.10576790 p1
#> 2 2.397328 0.59713947 p1
#> 3 4.780936 0.18421969 p1
#> 4 2.850478 0.48129485 p1
#> 5 6.024188 0.09792576 p1
#> 6 1.014710 1.04947049 p1
ggplot(tidy, aes(x=Xvals, y=Yvals, color = coords)) +
geom_line() + theme_minimal()
Maybe I'm not understanding precisely what you are looking for?
Created on 2018-05-04 by the reprex package (v0.2.0).
Upvotes: 1