Cogu makaledeki figurlere bakinca hangi programla hangi paket kullanilarak cizilmis anlayabiliyorsunuz. Oysa bunun boyle olmasi icin bir gerek yok. Profesyonel gorunumlu, kendi tarzinizi yansitan figurler cizmek istiyorsaniz, ggplot’ta kendi temanizi yaratip makalenizdeki tum figurler icin ayni temayi kullanmak cok kolay! Oncelikle ggplot icindeki temalara goz atalim.
Figurler icin R icinde hazir yuklu olan verilerden birini, iris verisini kullanacagim.
head(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width,color=Species))+
geom_point()
Bu figuru guzellestirmek istiyoruz diyelim. Ilk olarak ggplot icinde yuklu temalar zaten istedigimizi buyuk olcude bize verebilir.
px=ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width,color=Species))+
geom_point()
p1= px + theme_bw()
p2= px + theme_dark()
p3= px + theme_minimal()
p4= px + theme_classic()
grid.arrange(p1,p2,p3,p4,ncol=2)
Ben genelde kendi temami kullanmayacaksam theme_bw() tercih ediyorum, theme_classic() ise ggplot syntax i ile base R gorunumlu grafikler elde etmenizi sagliyor. Makalelerde kullanmak icin genelde bu ikisi de oldukca iyi secenekler. Ama daha fazla ozellestirme yapabiliyorken neden duralim, ozellikle de websitemizi, sunumlarimizi daha guzel kilmak varken? Burada imdadimiza ggthemes paketi yetisiyor.
library(ggthemes)
Tabii ggthemes icindeki tum temalari burada gostermemin bir anlami yok kendiniz acip hepsine bakabilirsiniz, ancak orneklendirmek adina sunlara bakalim:
px=ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width,color=Species))+
geom_point()
p1= px + theme_bw()
p2= px + theme_economist()
p3= px + theme_gdocs()
p4= px + theme_solarized()
p5= px + theme_stata()
p6= px + theme_wsj()
grid.arrange(p1,p2,p3,p4,p5,p6,ncol=2)
Bunlar sadece, neler yapabileceginizi hayal etmenizi kolaylastirmak acisindan birer ornek. Hadi simdi kendi temamizi olusturalim!
Burada her secenegin ne yaptigini orneklemek adina muhtemelen igrenc ve asiri bir tema uretecegim. Duzgun secenekleri kullanarak hem estetik hem de vurgulayici temalar uretmek size kalmis.
theme_Publication <- function (base_size = 20, base_family = "Helvetica"){
half_line <- base_size/2
theme(line = element_line(size = 0.5, linetype = 2, lineend = 'square'),
rect = element_rect(fill = "beige", colour = "blue", size = 1, linetype = 1),
text = element_text(family = base_family, face = "bold", colour = "darkred",
size = base_size, lineheight = 0.9, hjust = 0.5, vjust = 0.5,
angle = 0, margin = margin(), debug = FALSE),
axis.line = element_line(size=1,color='gray25',linetype=1),
axis.text = element_text(size = rel(0.8), colour = "forestgreen"),
axis.ticks = element_line(colour = "blue",size=1),
axis.ticks.length = unit(half_line/2, "pt"),
axis.title.x = element_text(margin = margin(t = half_line), vjust = 1),
axis.title.y = element_text(angle = 85, margin = margin(r = half_line), vjust = 0.5),
legend.background = element_rect(fill='gray85'),
legend.key = element_blank(),
legend.key.size = unit(2, "lines"),
legend.text = element_text(size = rel(0.8)),
legend.title = element_text(hjust = 0, face='bold.italic',size=rel(0.9)),
legend.position = 'top',
legend.justification = "centre",
legend.box.margin = margin(0, 0, 0, 0, "cm"),
legend.box.background = element_blank(),
legend.box.spacing = unit(0.1, "cm"),
panel.background = element_rect(fill = 'lightblue', colour = 'grey25'),
panel.border = element_blank(),
panel.grid.major = element_line(colour = "gray50", size = 0.25),
panel.grid.minor = element_line(colour = "gray70", size = 0.15),
panel.spacing = unit(half_line, "pt"),
strip.background = element_rect(fill = 'green', colour = 'black'),
strip.text = element_text(colour = "midnightblue", size = rel(0.9), hjust=0.5),
strip.text.x = element_text(margin = margin(t = half_line, b = half_line)),
strip.text.y = element_text(angle = -90, margin = margin(l = half_line, r = half_line)),
strip.placement = "inside",
strip.switch.pad.grid = unit(0.1, "cm"),
strip.switch.pad.wrap = unit(0.1, "cm"),
plot.title = element_text(size = rel(1.5), hjust = 0, vjust = 1, margin = margin(b = half_line * 1.2),color='black',face='italic'),
plot.subtitle = element_text(size = rel(1.2), hjust = 0, vjust = 1, margin = margin(b = half_line * 0.9),color='pink'),
plot.caption = element_text(size = rel(0.5), hjust = 1, vjust = 1, margin = margin(t = half_line * 0.9),color='gray45'),
plot.margin = margin(half_line, half_line, half_line, half_line))
}
px+theme_Publication()+facet_wrap(~Species)+labs(title='My Title',subtitle='MySubtitle',caption='MyCaption')
Lutfen ama lutfen boyle bir tema uretmeyin, ancak her kismin ne ise yaradigini gostermek adina guzel bir kombinasyondu diye tahmin ediyorum. Kendi figurlerimde genellikle kullandigim tema ile bir ornekle sonlandirayim:
tema uygulamadan:
px+facet_wrap(~Species)+labs(title='My Title',subtitle='MySubtitle',caption='MyCaption')
tema ile birlikte:
px+theme_melike()+facet_wrap(~Species)+labs(title='My Title',subtitle='MySubtitle',caption='MyCaption')