Reputation: 865
I am using ggplot2 and the geom_smooth functions to make predicted lines from a lm model. I am using continuous y and x functions in my ggplot code.
I am trying to create a different line type for each line in the plot (which is the variable "location" ranging from -1,0, or 1), and I would like each location listed in the guide (where -1 = Location A, 0 = Location B, and 1 = Location C). A replicable excerpt of my code is below:
#test scores that range from 1 to 7
score <- c(6, 3, 5, 6, 7, 2, 4, 6, 3, 5, 4, 3, 3, 1, 3, 3, 3, 5, 2, 3, 2, 2, 7, 3, 7, 5, 4, 1, 3, 2, 7, 6, 6, 3, 6)
#location of the school
location <- c(1, 0, 0, 0, 1, 1, 1, -1, 1, -1, 0, -1, 0, 1, 0, 0, 0, -1, 0, -1, 0, -1, 0, 0, 0, 0, 0, -1, 0, -1, 0, 0, 0, 1, 0)
IQ <- c(0.7171425604, 0.7056850461, 1.3929736220, 0.0633936624, -0.6872828336, -1.3665840767, 1.4368569944, 0.7297599487, -0.5735047485, -0.6752912747, -0.6213572428, -0.6110533924, -0.7090921238, 0.0501744806, 1.3916802944, -0.0055243194, 1.3619753292, 1.4406369365, 0.6529601586, 0.0538097896, 1.3821853866, 1.3870600993, 0.0040551996, 0.6600558495, 1.3550162100, 1.3081187951, -1.7541949601, 1.3768167017, -0.6232446826, -1.2793074919, 0.0560708725, -0.5993356051, -0.5857733192, -0.6005459705, -0.6659873442)
df <- data.frame(score, location, IQ)
p1 <- ggplot(data=df,aes(y=score,x=IQ,shape=factor(location))) +
geom_smooth(method = "lm", se=F) + scale_y_continuous("Test Score",limits=c(1,7)) +
scale_x_continuous("IQ level", limits=c(-2.45, 1.45)) +
theme_bw() +
theme(axis.text.x=element_text(size=rel(1.2), color = 'black'),
axis.title.x=element_text(size=rel(1.3)),
axis.title.y=element_text(size=rel(1.3)),
axis.text.y=element_text(size=rel(1.2), color = 'black'),
panel.grid.minor=element_blank(),
panel.grid.major.x=element_blank(),
panel.grid.major.y=element_blank(),
axis.ticks.y = element_blank(),
panel.border = element_blank(),
plot.title = element_text(size = rel(1.4)))
print(p1)
Whenever I try to add linetype or the manual linetype, I keep getting errors about how linetype cannot be mapped with continuous variable (only discrete). Is there any way around this or should I be using a different function? Maybe create labels for the values in location (School 1, School 2, and School 3?) beforehand? Or should I be using some alternative to linetype? I cannot find a solution online or in Wickham's book. Thank you!
Upvotes: 2
Views: 8201
Reputation: 98459
You are getting this error message because location
is continuous variable (numeric variables are treated as continuous in ggplot) and for linetype=
only discrete variables can be used. To use it for linetype=
convert location
to factor. To change labels in legend use scale_linetype()
and provide argument labels=
.
ggplot(data=df,aes(y=score,x=IQ,linetype=factor(location))) +
geom_smooth(method = "lm", se=F) + scale_y_continuous("Test Score",limits=c(1,7)) +
scale_x_continuous("IQ level", limits=c(-2.45, 1.45)) +
scale_linetype(labels=c("Location A","Location B","Location C"))
Upvotes: 2