Reputation: 23
I am trying to build a scatterplot using ggplot2.
However, I have some issue with renaming the legend. I would like to have "Spelling conditions" instead of "spelling" and "Lowercase", "Uppercase" instead of "lower","upper".
After some readings, I tried simultaneously the scale_fill_discrete()+ scale_ colour_discrete() and scale_shape_discrete() in order to get only 1 legend box, however using this method, my legend disappeared completely !
RT.data <- read.csv("http://www.psy.gla.ac.uk/~christop/MScStats/2015/Regress/RTs.csv")
head(RT.data)
x <- RT.data$logfreq
y <- RT.data$RT
ggplot(RT.data, aes(x, y, colour=spelling, shape=spelling)) +
geom_point() +
geom_smooth(method=lm, se=TRUE,level=0.95, aes(fill=spelling) ,alpha=0.09)+
ggtitle("Scatterplot of RT as a function \n of log lexical frequency and spelling of words :")+
scale_colour_discrete(name="Spelling conditions",
breaks=c("Lowercase", "Uppercase"),
labels=c("Lowercase", "Uppercase")) +
scale_fill_discrete(name="Spelling conditions",
breaks=c("Lowercase", "Uppercase"),
labels=c("Lowercase", "Uppercase")) +
scale_shape_discrete(name="Spelling conditions",
breaks=c("Lowercase", "Uppercase"),
labels=c("Lowercase", "Uppercase")) +
ylab("RT(ms)") +
xlab("Log lexical frequency") +
theme(panel.background = element_rect(colour = "gray", size= 1, fill = "gray99"),
legend.background = element_rect(fill="gray90", size=.5, linetype="dotted"),
legend.position="top",
axis.title.y=element_text(face="bold",colour="sienna",size=16),
axis.title.x =element_text(size=16,face="bold", colour= "sienna"),
plot.title = element_text(lineheight=.9, face="bold",size=16))
Upvotes: 2
Views: 451
Reputation: 23
By slightly modifying Mamoun Benghezal answer, I can have 1 legend including colour and shape, fill together with the categories renamed :
ggplot(RT.data, aes(logfreq, RT,colour=spelling,shape=spelling,fill=spelling)) +
geom_point() +
geom_smooth(method=lm, se=TRUE,level=0.99,alpha=0.09)+
ggtitle("Linear Regression (99% CI) of RT as a function of log Lexical \n Frequency and spelling conditions of words :") +
ylab("RT (ms)\n") +
xlab("\nLog lexical frequency") +
scale_colour_discrete(name="Spelling conditions",
breaks=c("lower", "upper"),
labels=c("Lowercase", "Uppercase")) +
scale_fill_discrete(name="Spelling conditions",
breaks=c("lower", "upper"),
labels=c("Lowercase", "Uppercase")) +
scale_shape_discrete(name="Spelling conditions",
breaks=c("lower", "upper"),
labels=c("Lowercase", "Uppercase")) +
theme(panel.background = element_rect(colour = "gray", size= 1, fill= "gray99"),
legend.background = element_rect(size=0.05),
legend.position=c(1,1),legend.justification=c(1,1),
axis.title.y=element_text(face="bold",colour="pink4",size=16),
axis.title.x =element_text(face="bold", colour= "palevioletred3",size=16),
plot.title = element_text(face="bold",size=18))
Upvotes: 0
Reputation: 5314
you can try this
library(ggplot2)
RT.data <- read.csv("http://www.psy.gla.ac.uk/~christop/MScStats/2015/Regress/RTs.csv")
x <- RT.data$logfreq
y <- RT.data$RT
ggplot(RT.data, aes(x, y, colour=spelling, shape=spelling)) +
geom_point() +
geom_smooth(method=lm, se=TRUE,level=0.95,alpha=0.09, aes(fill=spelling) )+
ggtitle("Scatterplot of RT as a function \n of log lexical frequency and spelling of words :")+
scale_colour_discrete(name="Spelling conditions",
breaks=c("lower", "upper"),
labels=c("Lowercase", "Uppercase")) +
ylab("RT(ms)") +
xlab("Log lexical frequency") +
guides(shape=FALSE, fill =F)+
theme(panel.background = element_rect(colour = "gray", size= 1, fill = "gray99"),
legend.background = element_rect(fill="gray90", size=.5, linetype="dotted"),
legend.position="top",
axis.title.y=element_text(face="bold",colour="sienna",size=16),
axis.title.x =element_text(size=16,face="bold", colour= "sienna"),
plot.title = element_text(lineheight=.9, face="bold",size=16))
Upvotes: 1