R Syntax Basics for Beginners-4

 




Slinear Regression in R

LR <- ggplot(BA,aes(x = NH4,

              y = Diatoms)) +

  geom_point(aes(color = Zones),

             size = 5,

             alpha = 0.7) +

  geom_smooth(aes(color = NULL),

              method = "loess",

              formula = "y~x",

              se=T) +

  stat_regline_equation(label.x = 2,

                        label.y = 400,

                        aes(label = ..eq.label..)) +

  stat_regline_equation(label.x = 2,

                        label.y = 350,

                        aes(label = ..rr.label..))

LR

লিনিয়ার রিগ্রেশন থেকে প্রাপ্ত ইকুয়েশন ডায়াগ্রামের কোথায় বসবে এবং কি ধরনের পরিসিংখ্যান আমরা ডায়াগ্রামে রাখবো সেটা উল্লেখ করতে “stat_regline_equation” ফাংশন ব্যবহার হয়। label.x এবং label.y দ্বারা আমরা বোঝাচ্ছি যে x and y এক্সিক্স এর ঠিক কোথায় ইকুয়েশনটা বসবে। আস্থেটিক লেবেল হচ্ছে আমরা কি ধরনের পরিসংখ্যান বসাতে চাচ্ছি। যেমন label = ..rr.label.. দ্বারা বুঝাচ্ছে R square এর মান।

 

#Zone-wise segmentation

LR+facet_wrap(~Zones)

একইভাবে পূর্বের মত আলাদা গ্রুপে দেখতে চাইলে facet_wrap ফাংশন ব্যবহার করতে হবে।

Depth-wise segmentation

LR+facet_wrap(~Depth)+

  labs(x="NH4 (ug/L)",

       y="Diatoms (cells/L)",

       title="Scatterplot",

       subtitle="Phytoplankton Vs Diatoms",

       caption = "Source: Biol_data")

 

 

Segmented Scatter plot in R

install.packages("ggplot2")

library(ggplot2)

install.packages("ggExtra")

library(ggExtra)

theme_set(theme_bw()) 

theme_set হলো থিমের একটা টাইপ মাত্র। যেটা দেখতে কিছুটা সুন্দর।

 

g <- ggplot(BA,aes(Phytoplankton,

                   Diatoms,

                   color=Zones,

                   size=Depth))+

  geom_point(alpha=0.5)

 

Viewing Graph

plot(g)

এই ডায়াগ্রামের সাথে আমরা আলাদাভাবে যদি কোন গ্রাফ মার্জিনে দেখাতে চাই তাহলে আমাদের ggMarginal ফাংশন ব্যবহার করতে হবে নিচের মত করে। এবং type এর ভেতরে আমরা যে ডায়াগ্রাম দেখতে চাই সেটা লিখে দিতে হবে।

 

ggMarginal(g, type = "histogram", fill="Pink")

 

ggMarginal(g, type = "boxplot", fill="Pink")

 

#type = “density”, “histogram”,

#type = “boxplot”, “violin”, “densigram”

 

Multiple scatter plots in R

library(ggplot2)

library(tidyverse)

library(modelr)

library(ggpubr)

 

Segmenting scatter plot with 2 groups

 

ggplot(BA, aes(Phytoplankton,

               Temperature,

               colour = Depth,

               size=Phytoplankton)) +

  geom_point() +

  facet_grid(BA$Zones~BA$Depth)+

  theme()+

  stat_regline_equation(label.x = 0.3,

                        label.y = 400,

                        aes(label = ..eq.label..)) +

  stat_regline_equation(label.x = 0.3,

                        label.y = 350,

                        aes(label = ..rr.label..))

 

এর পুর্বে মাল্টিপল গ্রাফের ক্ষেত্রে facet_warp ব্যবহার করেছিলাম, এই ক্ষেত্রে দুইটা আলাদা কন্টেক্সে Zones ও Depth এর আলাদা আলাদা গ্রাফ পাওয়া যাবে।

 

Sized Scatter-Plot in R

#non-scientific

GF <- ggplot(BA,

             aes(Zones,

              Stations,

              color=Depth,

              size=Phytoplankton))+

  geom_point(alpha=0.5)+

  theme_classic()

GF

GF+

  facet_wrap(~Depth)

 

#inverse Scatter Plot####

ggplot(BA,

       aes(Depth,

              Stations,

              color=Zones,

              size=Phytoplankton))+

  geom_point(alpha=0.5)+

  theme_classic()

#NB: These are non-scientific Plot

 

3d Scatter Plot in R

install.packages("tidyverse")

library(tidyverse)

 

থ্রিডি প্লটের ক্ষেত্রে আমাদের tidyverse ও  rgl প্যাকেজ ইন্সটল করে নিতে হবে এবং লাইব্রেরি কল করতে হবে।

 

install.packages("rgl")

library(rgl)

 

plot3d(x=BA$Temperature,

       z=BA$Phytoplankton,

       y=BA$Salinity,

       xlab="Temperature",

       ylab = "Salinity",

       zlab = "Phytoplankton",

       col=1:5,type="s",

       size =2)

 

এখানে থ্রিডি প্লটের জন্য আমরা তিনটা আলাদা এক্সিস নিয়েছি এবং আলাদা আলাদা ভাবে ডিফাইন করে দিয়েছি। col=1:5 দিয়ে ৫ টা কালারের কথা বলে দিলাম। type দিয়ে গ্রাফের ভেতরে ভ্যারিয়েবলের ধরন বলে দেই যে ন্যাচার কেমন হবে। এক্ষেত্রে  'p' for points, 's' for spheres, 'l' for lines, 'h' for line segments from z = 0 , and 'n' for nothing.

 

rgl.snapshot("3dTS.png")

উপরের কমান্ড ব্যবহার হয় গ্রাফ সেইভ করে রাখার জন্য। ব্রাকেটের ভেতরে ফাইল নেইম রাখা হয়েছে।

 

Colored scatter Plot in R

smoothScatter(y=BA$Temperature,

              x=BA$Salinity,

            

              xlab = "Salinity",

              ylab = "Temperature",

              main="T-S Diagram")    #এখানে মেইন বলতে মেইন টাইটেল বোঝাচ্ছে।

 

library(RColorBrewer)

library(ggplot2)

 

ggplot(data = BA,

       aes(y=Temperature,

           x=Salinity)) + 

  stat_density2d(aes(fill = ..density..^0.15),

                 geom = "tile",

                 contour = F,

                 n = 120) + 

  scale_fill_continuous(low = "yellow",

                        high = "red")+

  geom_point(size=3,alpha=0.7)

 

T-S diagram with zones in R

library(ggplot2)

ggplot(BA,aes(x=Salinity,y=Temperature))+

  stat_density2d(geom="polygon",

                 aes(fill=Zones,

                     alpha = ..level..))+

  geom_point(aes(shape=Zones),

             color="black",

             size=2)+

  theme_bw()+

  scale_fill_manual(

    values=c("#ff0061","#10E6F0","#ffae00","#ff0100"))

Comments