Tin Le Mini Lesson 6 Making Art with GGplot
set.seed(345) #set the seed of R's random number generator
library(ggplot2)
library(RColorBrewer)
ngroup=20 # changes how many groups there are in the art graph
names=paste("G_",seq(1,ngroup),sep="")
DAT=data.frame()
#creating dataframe
for(i in seq(1:20)){ #must change if changed the ngroup from previous code block
data=data.frame( matrix(0, ngroup , 3))
data[,1]=i
data[,2]=sample(names, nrow(data))
data[,3]=prop.table(sample( c(rep(0,100),c(1:ngroup)) ,nrow(data)))
DAT=rbind(DAT,data)
}
colnames(DAT)=c("Year","Group","Value")
DAT=DAT[order( DAT$Year, DAT$Group) , ]
coul = brewer.pal(12, "Paired")
coul = colorRampPalette(coul)(ngroup)
coul=coul[sample(c(1:length(coul)) , size=length(coul) ) ] #deciding the color for the pallete
ggplot(DAT, aes(x=Year, y=Value, fill=Group )) +
geom_area(alpha=1 , color=1 )+
theme_bw() +
scale_fill_manual(values = coul)+
theme(line = element_blank(),
text = element_blank(),
title = element_blank(),
legend.position="none",
panel.border = element_blank(),
panel.background = element_blank())
## Changing ngroup
ngroup=50 # changes how many groups there are in the art graph
names=paste("G_",seq(1,ngroup),sep="")
DAT2=data.frame()
#creating dataframe
for(i in seq(1:50)){ #must change if changed the ngroup from previous code block
data=data.frame( matrix(0, ngroup , 3))
data[,1]=i
data[,2]=sample(names, nrow(data))
data[,3]=prop.table(sample( c(rep(0,100),c(1:ngroup)) ,nrow(data)))
DAT2=rbind(DAT2,data)
}
colnames(DAT2)=c("Year","Group","Value")
DAT2=DAT2[order( DAT2$Year, DAT2$Group) , ]
coul = brewer.pal(12, "Paired")
coul = colorRampPalette(coul)(ngroup)
coul=coul[sample(c(1:length(coul)) , size=length(coul) ) ] #deciding the color for the pallete
ggplot(DAT2, aes(x=Year, y=Value, fill=Group )) +
geom_area(alpha=1 , color=1 )+
theme_bw() +
scale_fill_manual(values = coul)+
theme(line = element_blank(),
text = element_blank(),
title = element_blank(),
legend.position="none",
panel.border = element_blank(),
panel.background = element_blank())
Source: http://www.r-graph-gallery.com/137-spring-shapes-data-art/