3.baskı, Seçkin Yayınevi, Ankara.
Veri görselleştirme, verilerin resim veya grafiklerle sunulmasıdır. Bu sayede araştırmacılar veya karar vericiler veri gruplarındaki kalıpları, eğilimleri ve aykırı değerleri tespit edebilirler.
Gerekli R paketleri
install.packages("graphics") install.packages("ggplot2") install.packages("GGally") install.packages("ggalt") install.packages("lattice") install.packages("partykit") install.packages("mlbench") install.packages("waffle")
plot(pressure)
qplot(wt, mpg, data=mtcars, geom=c("point", "smooth"),color=cyl)
ggpairs(data=WeatherPlay, columns=1:3, ggplot2::aes(colour=play))
ggplot(iris, aes(Sepal.Length, Sepal.Width, group=Species, color=factor(Species))) + geom_point() + geom_smooth(se=FALSE, linetype="solid", size=1.5)
dotplot(iris$Sepal.Length~iris$Petal.Length|iris$Species)
WeatherPlay
## outlook temperature humidity windy play ## 1 sunny 85 85 false no ## 2 sunny 80 90 true no ## 3 overcast 83 86 false yes ## 4 rainy 70 96 false yes ## 5 rainy 68 80 false yes ## 6 rainy 65 70 true no ## 7 overcast 64 65 true yes ## 8 sunny 72 95 false no ## 9 sunny 69 70 false yes ## 10 rainy 75 80 false yes ## 11 sunny 75 70 true yes ## 12 overcast 72 90 true yes ## 13 overcast 81 75 false yes ## 14 rainy 71 91 true no
mpg
## # A tibble: 234 x 11 ## manufacturer model displ year cyl trans drv cty hwy fl class ## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr> ## 1 audi a4 1.8 1999 4 auto(l~ f 18 29 p comp~ ## 2 audi a4 1.8 1999 4 manual~ f 21 29 p comp~ ## 3 audi a4 2 2008 4 manual~ f 20 31 p comp~ ## 4 audi a4 2 2008 4 auto(a~ f 21 30 p comp~ ## 5 audi a4 2.8 1999 6 auto(l~ f 16 26 p comp~ ## 6 audi a4 2.8 1999 6 manual~ f 18 26 p comp~ ## 7 audi a4 3.1 2008 6 auto(a~ f 18 27 p comp~ ## 8 audi a4 quat~ 1.8 1999 4 manual~ 4 18 26 p comp~ ## 9 audi a4 quat~ 1.8 1999 4 auto(l~ 4 16 25 p comp~ ## 10 audi a4 quat~ 2 2008 4 manual~ 4 20 28 p comp~ ## # ... with 224 more rows
head(EuStockMarkets)
## DAX SMI CAC FTSE ## [1,] 1628.75 1678.1 1772.8 2443.6 ## [2,] 1613.63 1688.5 1750.5 2460.2 ## [3,] 1606.51 1678.6 1718.0 2448.2 ## [4,] 1621.04 1684.1 1708.1 2470.4 ## [5,] 1618.16 1686.6 1723.1 2484.7 ## [6,] 1610.61 1671.6 1714.3 2466.8
head(Titanic)
## , , Age = Child, Survived = No ## ## Sex ## Class Male Female ## 1st 0 0 ## 2nd 0 0 ## 3rd 35 17 ## Crew 0 0 ## ## , , Age = Adult, Survived = No ## ## Sex ## Class Male Female ## 1st 118 4 ## 2nd 154 13 ## 3rd 387 89 ## Crew 670 3 ## ## , , Age = Child, Survived = Yes ## ## Sex ## Class Male Female ## 1st 5 1 ## 2nd 11 13 ## 3rd 13 14 ## Crew 0 0 ## ## , , Age = Adult, Survived = Yes ## ## Sex ## Class Male Female ## 1st 57 140 ## 2nd 14 80 ## 3rd 75 76 ## Crew 192 20
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
head(pressure)
## temperature pressure ## 1 0 0.0002 ## 2 20 0.0012 ## 3 40 0.0060 ## 4 60 0.0300 ## 5 80 0.0900 ## 6 100 0.2700
head(economics)
## # A tibble: 6 x 6 ## date pce pop psavert uempmed unemploy ## <date> <dbl> <dbl> <dbl> <dbl> <dbl> ## 1 1967-07-01 507. 198712 12.6 4.5 2944 ## 2 1967-08-01 510. 198911 12.6 4.7 2945 ## 3 1967-09-01 516. 199113 11.9 4.6 2958 ## 4 1967-10-01 512. 199311 12.9 4.9 3143 ## 5 1967-11-01 517. 199498 12.8 4.7 3066 ## 6 1967-12-01 525. 199657 11.8 4.8 3018
data(Sonar) head(Sonar)
## V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 ## 1 0.0200 0.0371 0.0428 0.0207 0.0954 0.0986 0.1539 0.1601 0.3109 0.2111 0.1609 ## 2 0.0453 0.0523 0.0843 0.0689 0.1183 0.2583 0.2156 0.3481 0.3337 0.2872 0.4918 ## 3 0.0262 0.0582 0.1099 0.1083 0.0974 0.2280 0.2431 0.3771 0.5598 0.6194 0.6333 ## 4 0.0100 0.0171 0.0623 0.0205 0.0205 0.0368 0.1098 0.1276 0.0598 0.1264 0.0881 ## 5 0.0762 0.0666 0.0481 0.0394 0.0590 0.0649 0.1209 0.2467 0.3564 0.4459 0.4152 ## 6 0.0286 0.0453 0.0277 0.0174 0.0384 0.0990 0.1201 0.1833 0.2105 0.3039 0.2988 ## V12 V13 V14 V15 V16 V17 V18 V19 V20 V21 V22 ## 1 0.1582 0.2238 0.0645 0.0660 0.2273 0.3100 0.2999 0.5078 0.4797 0.5783 0.5071 ## 2 0.6552 0.6919 0.7797 0.7464 0.9444 1.0000 0.8874 0.8024 0.7818 0.5212 0.4052 ## 3 0.7060 0.5544 0.5320 0.6479 0.6931 0.6759 0.7551 0.8929 0.8619 0.7974 0.6737 ## 4 0.1992 0.0184 0.2261 0.1729 0.2131 0.0693 0.2281 0.4060 0.3973 0.2741 0.3690 ## 5 0.3952 0.4256 0.4135 0.4528 0.5326 0.7306 0.6193 0.2032 0.4636 0.4148 0.4292 ## 6 0.4250 0.6343 0.8198 1.0000 0.9988 0.9508 0.9025 0.7234 0.5122 0.2074 0.3985 ## V23 V24 V25 V26 V27 V28 V29 V30 V31 V32 V33 ## 1 0.4328 0.5550 0.6711 0.6415 0.7104 0.8080 0.6791 0.3857 0.1307 0.2604 0.5121 ## 2 0.3957 0.3914 0.3250 0.3200 0.3271 0.2767 0.4423 0.2028 0.3788 0.2947 0.1984 ## 3 0.4293 0.3648 0.5331 0.2413 0.5070 0.8533 0.6036 0.8514 0.8512 0.5045 0.1862 ## 4 0.5556 0.4846 0.3140 0.5334 0.5256 0.2520 0.2090 0.3559 0.6260 0.7340 0.6120 ## 5 0.5730 0.5399 0.3161 0.2285 0.6995 1.0000 0.7262 0.4724 0.5103 0.5459 0.2881 ## 6 0.5890 0.2872 0.2043 0.5782 0.5389 0.3750 0.3411 0.5067 0.5580 0.4778 0.3299 ## V34 V35 V36 V37 V38 V39 V40 V41 V42 V43 V44 ## 1 0.7547 0.8537 0.8507 0.6692 0.6097 0.4943 0.2744 0.0510 0.2834 0.2825 0.4256 ## 2 0.2341 0.1306 0.4182 0.3835 0.1057 0.1840 0.1970 0.1674 0.0583 0.1401 0.1628 ## 3 0.2709 0.4232 0.3043 0.6116 0.6756 0.5375 0.4719 0.4647 0.2587 0.2129 0.2222 ## 4 0.3497 0.3953 0.3012 0.5408 0.8814 0.9857 0.9167 0.6121 0.5006 0.3210 0.3202 ## 5 0.0981 0.1951 0.4181 0.4604 0.3217 0.2828 0.2430 0.1979 0.2444 0.1847 0.0841 ## 6 0.2198 0.1407 0.2856 0.3807 0.4158 0.4054 0.3296 0.2707 0.2650 0.0723 0.1238 ## V45 V46 V47 V48 V49 V50 V51 V52 V53 V54 V55 ## 1 0.2641 0.1386 0.1051 0.1343 0.0383 0.0324 0.0232 0.0027 0.0065 0.0159 0.0072 ## 2 0.0621 0.0203 0.0530 0.0742 0.0409 0.0061 0.0125 0.0084 0.0089 0.0048 0.0094 ## 3 0.2111 0.0176 0.1348 0.0744 0.0130 0.0106 0.0033 0.0232 0.0166 0.0095 0.0180 ## 4 0.4295 0.3654 0.2655 0.1576 0.0681 0.0294 0.0241 0.0121 0.0036 0.0150 0.0085 ## 5 0.0692 0.0528 0.0357 0.0085 0.0230 0.0046 0.0156 0.0031 0.0054 0.0105 0.0110 ## 6 0.1192 0.1089 0.0623 0.0494 0.0264 0.0081 0.0104 0.0045 0.0014 0.0038 0.0013 ## V56 V57 V58 V59 V60 Class ## 1 0.0167 0.0180 0.0084 0.0090 0.0032 R ## 2 0.0191 0.0140 0.0049 0.0052 0.0044 R ## 3 0.0244 0.0316 0.0164 0.0095 0.0078 R ## 4 0.0073 0.0050 0.0044 0.0040 0.0117 R ## 5 0.0015 0.0072 0.0048 0.0107 0.0094 R ## 6 0.0089 0.0057 0.0027 0.0051 0.0062 R
not<-data.frame(cinsiyet = c("Kadın", "Erkek"),say = c(60, 40)) pie<-ggplot(not, aes(x="", y=say, fill=cinsiyet))+geom_bar(width = 1, stat = "identity") pie<-pie + coord_polar("y", start=0) pie + geom_text(aes(label = paste0(say, "%")),position = position_stack(vjust = 0.5))
pie <- ggplot(mpg, aes(x = "", fill = factor(cyl))) + geom_bar(width = 1) + labs(fill="Silindir", x=NULL, y=NULL,title="Silindir sayısı için daire grafiği") pie <- pie + coord_polar(theta = "y", start=0) pie + theme_classic()+ theme(axis.line = element_blank(),axis.text = element_blank(), axis.ticks = element_blank(),plot.title = element_text(hjust = 0.5))
notlar <- c(10,15,18,24,36,32,30,14,12) names(notlar) <- c("AA","BA","BB","CB","CC","DC","DD","FD","FF") barplot(notlar, col = "tan1",main="Harf notlarının dağılımı")
nM<-as.vector(table(mpg$cyl)) names(nM)<-c("4","5","6","8") barplot(nM, col = "tan1",main="Silindir sayılarının dağılımı", xlab = "Silindir sayısı")
graf1 <- ggplot(mpg, aes(manufacturer))+ geom_bar(aes(fill=class), width = 0.5) graf1 + theme(axis.text.x = element_text(angle=65, vjust=0.6)) + labs(title="Araçların markalara göre dağılımı", subtitle = "(sınıflara göre birikimli)",x="Marka",y="Frekans")
veri1<-expand.grid(y = 1:13, x = 1:18) tablo<-sort(table(mpg$class)) veri1$category <- factor(rep(names(tablo),tablo )) head(veri1)
## y x category ## 1 1 1 2seater ## 2 2 1 2seater ## 3 3 1 2seater ## 4 4 1 2seater ## 5 5 1 2seater ## 6 6 1 minivan
ggplot(veri1, aes(x=x, y=y, fill=category)) + geom_tile(colour="black", size=0.5) + scale_x_continuous(expand=c(0,0))+scale_y_continuous(expand=c(0,0),trans='reverse')+ scale_fill_brewer(palette="Set3")+labs(title="Waffle Grafiği", subtitle="'Araç sınıfları için", caption="Veri: mpg") + theme(plot.title = element_text(size = rel(1.2)),legend.position = "right")
waffle(c(KADIN=20,ERKEK=30),rows=5,title = "Cinsiyete göre dağılım")
g <- ggplot(mpg, aes(cty)) g + geom_histogram(bins=10, fill="tan1", col="black", size=0.5) + labs(title="Histogram (sınıf sayısı verilmişken)", subtitle="Şehir içi mesafelerin dağılımı")
g + geom_histogram(binwidth = 2,fill="tan1",col="black",size=0.5)+ labs(title="Histogram (sınıf aralığı verilmişken)", subtitle="Şehir içi mesafelerin dağılımı")
g + geom_histogram(aes(fill=class),binwidth = 2, col="black", size=0.5) + labs(title="Histogram (sınıf aralığı verilmişken)", subtitle="Şehir içi mesafelerin araç sınıfına göre dağılımı")
newMpg<-mpg[mpg$class!="suv",] g <- ggplot(newMpg, aes(cty)) g + geom_histogram(binwidth = 2,fill="tan1",col="black",size=0.5)+ labs(title="Histogram (suv tipi araçlar hariç)", subtitle="Şehir içi mesafelerin dağılımı")
g <- ggplot(mpg, aes(cty)) g + geom_density(fill="tan1", alpha=0.8) + labs(title="Yoğunluk grafiği", subtitle="Şehir içi mesafe", caption="Veri seti: mpg", x="Şehir içi mesafe (mil)")
g + geom_density(aes(fill=factor(cyl)), alpha=0.8) + labs(title="Yoğunluk grafiği",subtitle="Silindir sayısına göre şehir içi mesafe", caption="Veri seti: mpg", x="Şehir içi mesafe (mil)",fill="Silindir sayısı")
g <- ggplot(mpg, aes(factor(cyl), cty)) g + geom_boxplot(varwidth=T, fill="tan1") + labs(title="Kutu grafiği", subtitle="Silindir sayısına göre şehir içi mesafe", caption="Veri: mpg", x="Silindir sayısı", y="Şehir içi mesafe (mil)")
g + geom_boxplot(aes(fill=drv)) + labs(title="Kutu grafiği", subtitle="Silindir sayısına göre şehir içi mesafe (çekiş türüne göre gruplanmış)", caption="Veri: mpg", x="Silindir sayısı", y="Şehir içi mesafe (mil)")
g <- ggplot(mpg, aes(factor(cyl), cty)) g + geom_boxplot() +geom_dotplot(binaxis='y',stackdir='center',dotsize=.5,fill="tan1")+ labs(title="Nokta-Kutu Grafiği", subtitle="Silindir sayısına göre şehir içi mesafe", caption="Veri: mpg", x="Silindir sayısı", y="Şehir içi mesafe (mil)")
g + geom_violin(fill="tan1") + labs(title="Keman grafiği", subtitle="Silindir sayısına göre şehir içi mesafe", caption="Veri: mpg", x="Silindir sayısı", y="Şehir içi mesafe (mil)")
mpg_select <- mpg[mpg$displ>5 & mpg$displ < 7 & mpg$hwy>20 & mpg$hwy<30, ] ggplot(mpg, aes(x=displ, y=hwy)) + geom_point(aes(col=class)) + geom_encircle(aes(x=displ, y=hwy),data=mpg_select,color="red",size=2, expand=0.02) + labs(subtitle="Motor hacmi ile şehirler arası mesafe", y="Şehirler arası mesafe (mil)", x="Motor hacmi(cc)",title="İşaretlenmiş noktalarla serpme diyagramı",caption="Veri:mpg")
ggplot(mpg, aes(x=displ, y=hwy)) + geom_point(aes(col=class)) + geom_smooth(method="loess", se=F) + xlim(c(0,7)) + ylim(c(0, 50)) + labs(subtitle="Motor hacmi ile şehirler arası mesafe", y="Şehirler arası mesafe (mil)", x="Motor hacmi(cc)",title="Düzgünleştirme eğrisi ile serpme diyagramı",caption="Veri:mpg")
plot(EuStockMarkets[,1],EuStockMarkets[,4],xlab="Almanya", ylab="İngiltere", main="Almanya ile İngiltere borsaları arasındaki ilişki")
borsa<-data.frame(EuStockMarkets) pairs(borsa)
ggpairs(borsa)
g <- ggplot(mpg, aes(cty, hwy)) + geom_point() g + labs(subtitle="şehir içi ve şehirler arası mesafe",y="şehirler arası mesafe (mil)", x="şehir içi mesafe (mil)",title="Çakışan noktalarla serpme diyagramı",caption="Veri:mpg")
g <- ggplot(mpg, aes(cty, hwy)) + geom_jitter(width = .5, size=1) g + labs(subtitle="şehir içi ve şehirler arası mesafe", y="şehirler arası mesafe (mil)", x="şehir içi mesafe (mil)", title="Jitter grafiği")
plot(EuStockMarkets[,1],xlab="Zaman",ylab="DAX")
plot(EuStockMarkets,xlab="Zaman")
ecoGraf<-ggplot(data = economics, aes(x = date, y = pop)) ecoGraf + geom_line(color = "tan1")
ecoGraf<-ggplot(data = economics, aes(x = date, y = pop)) ecoGraf + geom_line(aes(size = unemploy/pop), color = "tan1")
n<-dim(economics)[1] pVeri<-data.frame(economics$date,rep("psavert",n),economics$psavert) names(pVeri)<-c("date","variable","value") uVeri<-data.frame(economics$date,rep("uempmed",n),economics$uempmed) names(uVeri)<-c("date","variable","value") veri<-rbind(pVeri,uVeri) ggplot(veri, aes(x = date, y = value)) + geom_line(aes(color = variable), size = 1)
ggplot(data = economics, aes(x = date, y = psavert)) + geom_line(color = "tan1", size = 1.5) + scale_x_date(limits = c(as.Date("2002-1-1"), as.Date("2012-1-1")))
ggplot(data = economics, aes(x = date, y = psavert)) + geom_line(color = "tan1", size = 1) + stat_smooth(color = "blue", fill = "blue", method = "loess")
ggplot(veri, aes(x = date, y = value)) + geom_area(aes(color = variable, fill=variable), alpha=0.5,position=position_dodge(0.8))+scale_color_manual(values=c("green","tan1"))+ scale_fill_manual(values = c("green", "tan1"))