[英]Modify Legend of plot with several variables (colour, shape, fill)
我正在嘗試使用 ggplot2 構建散點圖。
但是,我對重命名圖例有一些問題。 我想要“拼寫條件”而不是“拼寫”和“小寫”,“大寫”而不是“低”,“大”。
經過一些閱讀,我同時嘗試了 scale_fill_discrete()+ scale_ colour_discrete() 和 scale_shape_discrete() 以獲得只有 1 個圖例框,但是使用這種方法,我的圖例完全消失了!
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))
你可以試試這個
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))
通過稍微修改 Mamoun Benghezal 的答案,我可以有 1 個圖例,包括顏色和形狀,與重命名的類別一起填充:
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))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.