#http://www.cookbook-r.com/Graphs/Bar_and_line_graphs_(ggplot2)/
#总结
#第一篇
ggplot(data = dat,aes(x=time,y=total_bill,fill=time))+geom_bar(fill="#DD8888",color='black',stat = 'identity',width = 0.5)+guides(fill=F)+
ggtitle('barplot')+ylab('Total_bill')+xlab('Time of day')
#stat='identity'根据datafram中的数值画图,guides(fill=FALSE)不画图例
#在geom_bar()里面添加color=''直接设定柱子的边框颜色
ggplot(data = dat,aes(x=time,y=total_bill,group=1))+geom_line()+geom_point()+
ggtitle('barplot')+ylab('Total_bill')+xlab('Time of day')+
expand_limits(y=0)
#expand_limits()设定y轴范围
ggplot(data = dat1,aes(x=time,y=total_bill,fill=sex))+geom_bar(stat = 'identity',position = 'dodge')+
scale_fill_manual(values=c("#999999", "#E69F00"))
ggplot(data = dat1,aes(x=time,y=total_bill,fill=sex))+geom_bar(stat = 'identity',position = 'dodge',color='black')+
scale_fill_manual(values=c("#999999", "#E69F00"))
#如果改变柱子的填充颜色,只有一个的话,geom_bar(fill=''),大于一个:scale_fill_mannel(values=('','')
ggplot(data = dat1,aes(x=time,y=total_bill,group=sex,shape=sex,color=sex))+geom_line(size=1.5)+
geom_point(size=3,fill='white')+scale_shape_manual(values=c(22,21))
#shape= geom_line(size=) scale_shape_manual(values = ) geom_point(fill='white')
ggplot(data = dat1,aes(x=time,y=total_bill,group=sex,shape=sex,color=sex))+geom_line(aes(linetype=sex),size=1)+
geom_point(size=3,fill='white')+expand_limits(y=0)+scale_color_hue(name='sex',l=30)+
scale_shape_manual(values = c(22,21))+
scale_linetype_discrete(name='sex')+
xlab('x')+ylab('y')+
ggtitle('average')+
theme_bw()+theme(legend.position = c(.7,.4))
#geom_line(linetype=) scale_color_hue(name=,l=)设置透明度,其中name=是legend的name,一起设置
#scale_shape_manual(name='',values=c()) scale_linetype_discrete(name='')设置线型或是点的形状的legend
#theme_bw() theme(legend.position = c())可以设置legend的位置
ggplot(data = datn,aes(x=dose,y=length,group=supp,color=supp,shape=supp))+geom_line(aes(linetype=supp))+
geom_point(size=3,fill='white')+scale_shape_manual(values=c(22,21))
#第二篇
#如果有重叠的话:pd <- position_dodge(0.1) # move them .05 to the left and right
pd<-position_dodge(0.1)
ggplot(tgc,aes(x=dose,y=len,color=supp,group=supp))+
geom_errorbar(aes(ymin=len-se,ymax=len+se),color='black',width=.1,position = pd)+
geom_point(position = pd,size=.3,shape=21,fill='white')+
geom_line(position = pd)+
xlab('Dose')+
ylab('Tooth')+
scale_color_hue(name='Supplement type',
breaks=c('OJ','VC'),
labels=c("Orange juice", "Ascorbic acid"),
l=40)+
ggtitle('The Effect of Vitamin C on\nTooth Growth in Guinea Pigs')+
expand_limits(y=0)+
scale_y_continuous(breaks = 0:20*4)+
theme_bw()+
theme(legend.justification = c(1,0),
legend.position = c(1,0))
#geom_errorbar() scale_color_hue(breaks=c(),labels=c())将legend的注释从breaks修改为labels
#scale_y_continuous(breaks = a:b*c)从a到b每份c个
#theme(legend.justification = c(),legend.position = c())
dfw_long<-melt(dfw,
id.vars = 'subject',
measure.vars = c("pretest","posttest"),
variable.name = 'condition')
#按照subject将"pretest","posttest"两项数据排列,变量名为condition
ggplot(dfw_long,aes(x=condition,y=value,color=subject,group=subject))+
geom_line()+geom_point(shape=21,fill='white')+
ylim(ymin,ymax)
#ylim
#names(data_long)[names(data_long)=='value']<-'Time'
data_long$ColorScheme<-NA
data_long$ColorScheme[grep('Mono$',data_long$Condition)]<-"Monochromatic"
#另外新建了一列colorscheme,如果data_long的condition列是以Mono结尾,就将scheme填上Monochromatic
data_long$Shape[grepl("^Square", data_long$Condition)] <- "Square"
#如果是以Square开头
ggplot(datac,aes(x=Shape,y=Time,fill=ColorScheme))+
geom_bar(position = position_dodge(.9),color='black',stat='identity')+
geom_errorbar(position = position_dodge(.9),width=.25,aes(ymin=Time-ci,ymax=Time+ci))+
coord_cartesian(ylim = c(40,46))+
scale_fill_manual(values = c("#CCCCCC","#FFFFFF"))+
scale_y_continuous(breaks = seq(1:100))+
theme_bw()+geom_hline(yintercept = 38)
#geom_hline(yintercept = num)在y=num位置添加一条线