I want to make a graph with two lines (one for a level and anorther for other level of the factor), but each level have two categories on ggplot2. So I need make a graph like this (image) but with two symbols for each line. Help, please!!!!
Asked
Active
Viewed 57 times
-2
-
1Please share some data with `dput()` and the code you tried. – markus Nov 22 '18 at 19:28
-
I tried putting the data and ended up going as image. And the graph is an example of the internet. – Bruna Carvalho Nov 22 '18 at 21:08
-
Please consider [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – markus Nov 22 '18 at 21:13
-
I put a online table with the data. – Bruna Carvalho Nov 22 '18 at 21:37
1 Answers
0
A (potential) solution in R base is this:
Define your data in a dataframe:
df <- data.frame(Gender = c(rep("m",10), rep("f", 10)),
Age = sample(10:90, 20, replace = T),
Fitness_level = sample(1:10, 20, replace = T))
Add two distinct point characters depending on age level:
df$pch <- ifelse(df$Age > 40, 8, 12) # 8 and 12 are point characters
Plot fitness level against age for men and, respectively for women, adding for each a regression line:
# scatter plot:
plot(df$Fitness_level[df$Gender=="m"] ~ df$Age[df$Gender=="m"], pch = df$pch, col="red", ylim=c(1,10))
# regression line for men:
abline(lm(df$Fitness_level[df$Gender=="m"] ~ df$Age[df$Gender=="m"]), col="red")
# add points for women:
points(df$Fitness_level[df$Gender=="f"] ~ df$Age[df$Gender=="f"], pch = df$pch, col = "blue")
# add regression line:
abline(lm(df$Fitness_level[df$Gender=="f"] ~ df$Age[df$Gender=="f"]), col="blue")

Chris Ruehlemann
- 20,321
- 4
- 12
- 34