使用多个变量(颜色,形状,填充)修改绘图图例

时间:2015-11-03 00:26:49

标签: r ggplot2 legend

我正在尝试使用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)) 

2 个答案:

答案 0 :(得分:1)

你可以试试这个

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))

答案 1 :(得分:0)

通过略微修改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))