简体   繁体   中英

Can colors and shape be changed on a combined line and point plot while maintaining one legend per graph?

Using the following data frame:

day <- gl(8,1,48,labels=c("Mon","Tues","Wed","Thurs","Fri","Sat","Sun","Avg"))
day <- factor(day, level=c("Mon","Tues","Wed","Thurs","Fri","Sat","Sun","Avg"))
month<-gl(3,8,48,labels=c("Jan","Mar","Apr"))
month<-factor(month,level=c("Jan","Mar","Apr"))

snow<-gl(2,24,48,labels=c("Y","N"))
snow<-factor(snow,levels=c("Y","N"))
count <-c(.94,.95,.96,.98,.93,.94,.99,.9557143,.82,.84,.83,.86,.91,.89,.93,.8685714,1.07,.99,.86,1.03,.81,.92,.88,.9371429,.94,.95,.96,.98,.93,.94,.99,.9557143,.82,.84,.83,.86,.91,.89,.93,.8685714,1.07,.99,.86,1.03,.81,.92,.88,.9371429)
d <- data.frame(day=day,count=count,month=month,snow=snow)

I'd like to change the colors and shapes of the lines and points grouped by month on the following graph:

library(ggplot2)
library(scales)

ggplot(data=d[d$day=="Avg",],aes(x=day, y=count, fill=month,group=month,label=month),show_guide=F)+
facet_wrap(~snow,ncol=1,scales="free")+
geom_line(data=d[d$day!="Avg",],aes(x=day, y=count, group=month, colour=month), show_guide=F)+
scale_x_discrete(limits=levels(d$day))+
scale_y_continuous(labels = percent)+
geom_point(aes(colour = month),size = 4,position=position_dodge(width=1.2))+
expand_limits(y=0)

在此处输入图片说明

  1. How can I change the colors and shapes (I realize that many options exist) of both the line graph and the points so that they are still grouped by month?

NOTE: A single legend per graph with the appropriate shape/color is desired (bonus points if you can figure out how to create a legend for each graph without using grid.arrange).

I believe @thunk has it right, but there's some more problems with your code. (1) You specify fill , but then you don't use any geoms that take a fill . (2) You set the default aesthetics in the first ggplot , then you needlessly reset them in your geom s. (3) You want the shapes to change, but you never specify a shape aesthetic.

I think this gives what you want:

ggplot(data=d[d$day=="Avg",],
       aes(x=day, y=count, color=month, group=month,
           label=month, shape = month), show_guide=F)+
    facet_wrap(~snow, ncol=1, scales="free")+
    geom_line(data=d[d$day!="Avg", ])+
    scale_x_discrete(limits=levels(d$day))+
    scale_y_continuous(labels = percent)+
    geom_point(size = 4, position = position_dodge(width=1.2))+
    scale_color_manual(values = c("dodgerblue4", "firebrick4", "forestgreen")) +
    expand_limits(y=0)

固定地块

In the first aes call in the initial ggplot() we specify that both color and shape vary by month. Then, in the geom_line and geom_point calls we don't need to say so again. Adding scale_color_manual() lets us pick whatever colors we want, (and if you want to specify shapes, adding scale_shape_manual() will work for that).

Hope this helps!

I think that what you want to do can be achieved by adding the following line to your graph creation:

+ scale_colour_manual(values = c("yellow","pink", "brown"))

Here you can chuck in whichever colours you want your lines and points to have. The same can be done with scale_shape_manual etc. Have a closer look at the docs:

http://docs.ggplot2.org/current/scale_manual.html

But in general, I think that the graph you are producing does not necessarily portray the data you have in the most useful and accessible way. So maybe rethink what you actually want this graph to tell, and eg reconsider if connecting the days of the week with a line is helpful here, as the values are really averages for the weekdays and thus these averages are not really temporally connected in a linear order. And I also think that putting the average in the same graph (as if it were another day) adds some confusion. But then, I don't know the bigger picture...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM