Graphing mutliple lines (999) on the same xy-plane in r
Graphing mutliple lines (999) on the same xy-plane in r
I have two variables that i want to bootstrap and graph the resulting linear regression line on the same xy plane from each new data set.
I was thinking that i can hold each resulting intercept and slope from the lm() but i dont know how i could graph that information for each resulting pair of information in the same graph. I know that abline() can do one pair but not all of them. Feel free to throw anything at me.
intercept_stuff<-rep(NA,T)
opp_stuff<-rep(NA,T)
N<-1000
for(t in 1:T)
idx <- sample(1:N, size =N, replace=TRUE)
intercept_stuff[t]<- lm(oppose_any~local_topic ,data = facebook[idx,
])$coefficient[1]
opp_stuff[t]<- lm(oppose_any~local_topic ,data = facebook[idx,
])$coefficient[2]
lines()
Check out here for examples of bootstrapping plots. cran.r-project.org/web/packages/broom/vignettes/…
– Jon Spring
Sep 17 '18 at 3:51
1 Answer
1
Here is an example of how to do multiple pairs of lines on ggplot
with some simulated data. Hopefully this will give you some useful clues:
ggplot
library(reshape2)
library(tibble)
# simulate some data
obs <- c(1:90)
values1 <- rnorm(90,mean=0,sd=1)
values2 <- rnorm(90,mean=5,sd=2)
values3 <- rnorm(90,mean=10,sd=3)
df <- as.tibble(cbind(obs,values1,values2,values3))
It looks like this:
> df
# A tibble: 90 x 4
obs values1 values2 values3
<dbl> <dbl> <dbl> <dbl>
1 1. -0.162 7.47 10.7
2 2. 0.518 5.17 7.61
3 3. 1.52 7.66 4.42
# ... with 80 more rows
Then melt
it into long form:
melt
m.df <- melt(df,id="obs",measures=c("values1","values2","values3"))
to look like this:
> m.df
obs variable value
1 1 values1 -0.16228714
2 2 values1 0.51755370
3 3 values1 1.52433685
4 4 values1 -1.82067006
5 5 values1 -1.42180601
...
Then plot many lines (as long as there are groups like the color
group here, they will be unique lines):
color
ggplot(m.df,aes(x=obs,y=value,color=variable)) + geom_line()
And here is the plot:
Good luck!
Thanks a bunch this really helps
– Jerry Tucay
Oct 3 '18 at 17:51
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy
Check out the
lines()
command. Many other ideas here.– Dan Y
Sep 16 '18 at 23:35