-1

I have a dataset of three X variables (X1,X2,X3) and one Y variable (Y). The range of the X variables is (-4 - 4) and Y (400 - 540).

Sample of dataset can be generated below

df1 <- data.frame( Y = rnorm(1000,400:450),
           X1 = rnorm(1000,-4:5),
           X2 = rnorm(1000,-4:5),
           X3 = rnorm(1000,-4:5)
           )

I wish to produce a plot where the three X variables appear like curves and one line showing the average of Y. Exactly like this figure

I have tried ggplot geom_line but to no vain,

ggplot(read, aes(x=X1, y=Y)) + geom_line(linetype = 2)

I only was able to obtain a dense line plot like this

user438383
  • 5,716
  • 8
  • 28
  • 43
  • Did you just delete your question where you had many comments and repost it? Actually, you did, when you should have updated the question with the relevant additional information. Deleting a question where others tried to help you/give you hints on how to improve the question is quite rude and disrespectful... – dario Oct 03 '21 at 13:05
  • All of your variables are totally random, thus there is no correlation between them and the plot must look like an earthquake. – danlooo Oct 03 '21 at 13:16
  • Can you please add suitable example data so there is at least going to be some resemblance to your desired output plot? See also here for information on how to create a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) and [a good overview on how to ask a good question](https://stackoverflow.com/help/how-to-ask) – dario Oct 03 '21 at 13:22
  • @dario I deleted it because the question was closed and had no choice except for posting a new one and deleting the old one. I am not sure what more data I should provide, the code I posted generate the exact data I have except for the number of obs – Mudar Saied Oct 03 '21 at 13:42

1 Answers1

0
library(tidyverse)

df1 = tibble(Y = rnorm(1000),
             x = seq(-4, 5, length.out =  1000),
             X1 = -2*x^2+7*x+1,
             X2 = -4*x^2+7.5*x+3,
             X3 = -2*x+10
)

df = df1 %>% pivot_longer(X1:X3) %>% 
  mutate(name = name %>% factor(labels = c("At school in general", 
                                           "Outside school for Schoolwork",
                                           "Outside school for leisure"))) 

df %>%  
  ggplot(aes(x, value, color=name))+
  geom_line(aes(linetype=name))+
  geom_hline(yintercept =mean(df$Y))+
  annotate("text", label="Mean reading perofmence", x=3, y=2)+
  labs(x ="ITC use", y = "Reading performance")

enter image description here

Marek Fiołka
  • 4,825
  • 1
  • 5
  • 20