<geom_area() - stacked area graph>
<The first example>
> data_frame_1<-data.frame(Year=c(2008,2008,2010,2010,2012,2012,2014,2014,2016,2016,2018,2018,2020,2020),Sex=c("F","M","F","M","F","M","F","M","F","M","F","M","F","M"),Freshmen=c(73,62,79,64,82,70,90,83,103,94,140,125,165,143))
> View(data_frame_1)
> ggplot(data=data_frame_1,aes(x=Year,y=Freshmen,fill=Sex))+geom_area() |
> ggplot(data=data_frame_1,aes(x=factor(Year),y=Freshmen,fill=Sex,group=Sex)) +geom_area() |
elements of Year in the graph is 2007.5, 2010.0 ... difficult to decode the graph. |
elements of factor(Year) in the graph is 2008,2010,2012,2014,2016,2018,2020 |
<The second example>
> data_frame_2<-data.frame(Year=c(2008,2008,2008,2008,2010,2010,2010,2010,2012,2012,2012,2012,2014,2014,2014,2014,2016,2016,2016,2016,2018,2018,2018,2018,2020,2020,2020,2020),Age=c(18,19,20,21,18,19,20,21,18,19,20,21,18,19,20,21,18,19,20,21,18,19,20,21,18,19,20,21),freshmen=c(13,37,46,15,18,39,40,12,25,47,56,17,23,59,73,24,28,78,92,30,33,92,100,32,42,130,140,42))
>View(data_frame_2)
> ggplot(data=data_frame_2,aes(x=factor(Year),y=freshmen,fill=Age,group=Age))+geom_area()
> ggplot(data=data_frame_2,aes(x=factor(Year),y=freshmen,fill=Age,group=Age))+geom_area(colour="black")
> ggplot(data=data_frame_2,aes(x=factor(Year),y=freshmen,fill=Age,group=Age))+geom_area(colour="black",alpha=0.6)
<A way to set stacked order & legend order by yourself>
> data_frame_2$Age<- factor(data_frame_2$Age,level=c(18,19,20,21)) > ggplot(data=data_frame_2,aes(x=factor(Year),y=freshmen,fill=Age,group=Age)) +geom_area(colour="black",alpha=0.6) |
> data_frame_2$Age<-factor(data_frame_2$Age,level=c(21,20,19,18)) > ggplot(data=data_frame_2,aes(x=factor(Year),y=freshmen,fill=Age,group=Age)) +geom_area(colour="black",alpha=0.6) |
|
|
<Swith the order of legend>
>data_frame_2$Age<-factor(data_frame_2$Age)
> ggplot(data=data_frame_2,aes(x=factor(Year),y=freshmen,fill=Age,group=Age)) +geom_area(colour="black",alpha=0.4)+scale_fill_brewer(palette=1) |
> ggplot(data=data_frame_2,aes(x=factor(Year),y=freshmen,fill=Age,group=Age)) +geom_area(colour="black",alpha=0.4) +scale_fill_brewer(palette=1,breaks=rev(data_frame_2$Age)) |
|
|
<Swith the stacked order>
> ggplot(data=data_frame_2,aes(x=factor(Year),y=freshmen,fill=Age,group=Age)) +geom_area(colour="black",alpha=0.4)+scale_fill_brewer(palette=1) |
> ggplot(data=data_frame_2,aes(x=factor(Year),y=freshmen,fill=Age,group=Age)) +geom_area(position=position_stack(reverse=TRUE),colour="black",alpha=0.4) +scale_fill_brewer(palette=1) |
|
|
<Swith both the legend order and stacked order>
> ggplot(data=data_frame_2,aes(x=factor(Year),y=freshmen,fill=Age,group=Age)) +geom_area(colour="black",alpha=0.4) +scale_fill_brewer(palette=1) |
> ggplot(data=data_frame_2,aes(x=factor(Year),y=freshmen,fill=Age,group=Age)) +geom_area(position=position_stack(reverse=TRUE),colour="black",alpha=0.4) +scale_fill_brewer(palette=1,breaks=rev(levels(data_frame_2$Age))) |
|
|
> data_frame_3<-data_frame_2%>%group_by(Year)%>%mutate(sum_freshmen=sum(freshmen),pro_freshmen=freshmen/sum(freshmen)*100)
>View(data_frame_3)
>ggplot(data=data_frame_3,aes(x=factor(Year),y=pro_freshmen,fill=Age,group=Age))+geom_area(colour="black",alpha=0.6)+scale_fill_brewer(palette=1)
Way1 |
Way2 |
> data_frame_3<-ddply(data_frame_2,"Year", transform,freshper=freshmen/sum(freshmen)*100) > ggplot(data_frame_3,aes(x=factor(Year),y=freshper,fill=Age,group=Age)) +geom_area(colour="black",alpha=0.6)+scale_fill_brewer(palette=1) |
> data_frame_3<-data_frame_2%>%group_by(Year) %>%mutate(pro_freshmen=freshmen/sum(freshmen)*100) >ggplot(data=data_frame_3,aes(x=factor(Year),y=pro_freshmen,fill=Age,group=Age)) +geom_area(colour="black",alpha=0.6)+scale_fill_brewer(palette=1) |
|
|