R Syntax Basics for Beginners-3

 

Multiple bar plots with different variables in R

library(ggplot2)

library(reshape)


WW <- BA[,c(1,6:9)]


PP <- melt(WW, id.vars = "Stations")


melt  ফাংশন ব্যবহার করা হয় একটা ভ্যারিয়েবলের ভেলুর সাপেক্ষে একটা নতুন কলাম তৈরি হবে যেখানে ঐ ভ্যারিয়েবলের নাম প্রতি কলামে থাকবে। 


ggplot(PP, aes(Stations, value, fill = variable)) +

 

 

  geom_bar(stat="identity", position = "dodge") +         

 

 

  labs(title="Multiple Bar plots")


এখানে position=”dodge” ব্যবহার হয়েছে যেন ভ্যারিয়েবল গুলো ওভারল্যাপ না হয়ে বা ভার্টিক্যালি না বসে হরিজন্টালি বসবে। 

Dodging preserves the vertical position of a geom while adjusting the horizontal position.

labs ফাংশন ফিগার নেইম লেখার জন্য ব্যবহার হয়েছে। 






Line plot with points in R

library(dplyr)


BU <- filter(BA,Depth==3)


library(ggplot2)


ggplot(BU,

       aes(x=Stations,

           y=Phytoplankton,

           group=1)) +

  geom_line(color="red",size=1)+

  geom_point(color="black",size=3)


group=1 দেয়ার অর্থ হলো আমরা বলে জানিয়ে দিচ্চি যে যে কোন একটা ভ্যারিয়েবল এখানে ক্যাটাগরিক্যাল এবং ক্যাটাগরিক্যাল ভ্যারিয়েবল গুলো অর্ডার ফলো করছে। 

নতুন করে BU ডেটা সেট করে নেবার কারন হলো আমাদের আগের ডেটা সেটে x and y axix এ প্রতিটা ভ্যারিয়েবলে চারটা বার করে আছে। এবং সেটা দিয়ে গ্রাফ বানালে ভুল গ্রাফ হবে। লাইন গ্রাফে একটা পয়েন্টে একটা মাত্র ভেলু থাকবে। 

মাল্টিপল ভ্যারিয়েবল দিয়ে গ্রাফ বানাতে হলে আমাদের নিচের ফাংশন ব্যবহার করতে হবে। 




Line graph for multiple variables in R


মাল্টিপল ভ্যারিয়েবলের লাইন গ্রাফ ড্র করতে গেলে প্রথমেই আমাদের x axis ডিফাইন করে দিতে হবে। এবং পরবর্তিতে প্রতিটা ভ্যারিয়েবলের জন্য আলাদা আলাদা করে geom_line and geom_point ডিফাইন করে দিতে হবে নিচের মত করে। 


ggplot(BU,

       aes(x=Stations,

           group=1)) +

 

  geom_line(aes(y=Phytoplankton,color="Phytoplankton"))+

  geom_point(aes(y=Phytoplankton,color="Phytoplankton"))+

 

  geom_line(aes(y=Diatoms,color="Diatoms"))+

  geom_point(aes(y=Diatoms,color="Diatoms"))+

 

  geom_line(aes(y=Dinoflagilates,color="Dinoflagilates"))+

  geom_point(aes(y=Dinoflagilates,color="Dinoflagilates"))+

 

  labs(y="Density (Cells/L)")


Multiple line graphs in R


library(dplyr)

#Data filtering for single station names

BU <- filter(BA,Depth==3)


#selecting data for multiple lines

GF <- BU[,c(1,6:9)]


install.packages("reshape")

library(reshape)


Reshape function in R transforms the data from wide to long and also transforms back the data from long to wide.


#arranging data in one table


BF <- melt(GF, id.vars = "Stations")


library(ggplot2)


#Viewing line graphs

ggplot(BF,

       aes(x=Stations,

           y=value,

           color=variable,

           group=variable)) +

  geom_line()+

  geom_point()+

 

  facet_wrap(~variable)



Bar plot + Line Graph in R

library(ggplot2)


ggplot(BU)  +

  geom_bar(aes(x=Stations,

               y=Phytoplankton),

           stat="identity",

           fill="gray",

           colour="black")+

 

  geom_line(aes(x=Stations,

                y=Phytoplankton,

                group=1),

            stat="identity",

            color="red")+

 

  labs(title= "Combined Graph",

       x="Stations",

       y="Phytoplankton")


এখানে যেহেতু আমরা দুইটা আলাদা গ্রাফ করছি সেহেতু aesthetic আলাদা আলাদা ভাবে দুইটা গ্রাফে উল্লেখ ইন্ডিকেট করে দেবার প্রয়োজন হয়েছে। geom_line এ stat=”identity” না দিলেও অসুবিধা নেই। 



Dot plot as Bar style in R


ggplot(BA,

       aes(Stations,

           Phytoplankton))+

  geom_point(size=2,

             color="red")


Stacked Bar Plot in R

ggplot(BA,

       aes(fill=Depth,

           y=Phytoplankton,

           x=Stations)) +

  geom_bar(position="stack",

           stat="identity")


এখানে আমরা Stacked bar plot ড্র করেছি Depth এর বেসিসে তাই fill=Depth বসানো হয়েছে। এবং স্টাক বারের জন্য position="stack" লেখা হয়েছে। 


Grouped Stacked Bar Plot

P <- ggplot(BA,

            aes(fill=Zones,

                y=Phytoplankton,

                x=Stations)) +

  geom_bar(position="stack",

           stat="identity")

P


Q <- ggplot(BA,

            aes(fill=Depth,

                y=Phytoplankton,

                x=Stations)) +

  geom_bar(position="fill",

           stat="identity")

Q



Multiple Grouped Stacked Bar in R 

ggplot(BA, aes(fill=Zones, y=Phytoplankton, x=Stations)) +

  geom_bar(position="stack", stat="identity")+

  facet_wrap(~Depth)


Non-scientific Graph

ggplot(BA, aes(fill=Depth, y=Phytoplankton, x=Stations)) +

  geom_bar(position="stack", stat="identity")+

  facet_wrap(~Zones)



Pie Plot in R

R <- ggplot(BA,

            aes(x = "",

                y = Phytoplankton,

                fill = Zones)) +

  geom_col() +

  coord_polar(theta = "y")+

  theme_void()

R



Circular Barplot in R


ggplot(BA) +

  geom_bar(aes(x=Stations, y=Phytoplankton,fill=Depth),

           stat="identity") +

  ylim(-800,800)+

  coord_polar(start=0)+theme_void()


Circular Grouped Bar in R

ggplot(BA) +

  geom_bar(aes(x=Stations, y=Phytoplankton,fill=Zones),

           stat="identity") +

  ylim(-800,800)+

  coord_polar(start=0)+theme_void()


Multiple Graphs on the same sheet in R


install.packages("ggpubr")

library(ggpubr)


The ggpubr R package facilitates the creation of beautiful ggplot2-based graphs for researchers with non-advanced programming backgrounds. 


ggarrange(P, Q,

          ncol = 1, nrow = 2)


ggarrange ব্যবহার হয় একাধিক গ্রাফকে একই সাথে দেখানোর জন্য। 


ggarrange(P, Q + font("x.text",  size = 10),

          ncol = 2, nrow = 1)


"x.text" for x axis texts (x axis tick labels)



Horizontal Graph Arrangement in R


ggarrange(P,Q,R,

          ncol = 1, nrow = 3,

          labels = c("(A)", "B", "C"))




Vertical Graph Arrangement in R


ggarrange(P, R, Q, Q +

            font("x.text", size = 8),

          ncol = 2,

          nrow = 2,

          labels = c("(A)", "(B)", "C","D"))


এখানে labels দিয়ে ফিগার গুলোর লেভেলিং বুঝানো হচ্ছে। 



Scatter plots in R 


Cutting table in sub-table

BO <- BA[,c(-1:-5)]


এখানে মাইনাস সাইনের অর্থ হলো BA টেবিল থেকে ১ থেকে ৫ নাম্বার কলাম বাদ দিয়ে নতুন টেবিল BO তৈরি করবে। 


BB <- BA[,c(6:17)]

#View(BB)

plot(BB)



Scatter plots with Biological Parameters

BX=BB[,c(1:4)]

BX

plot(BX)



Scatter plot of Environmental Parameters

BY=BB[,c(-1,-2,-3,-4)]


plot(BY)



Simple linear regression in R


GG <- lm(Phytoplankton~Diatoms,BA)


Coefficient Value

GG  


Summary of the regression

summary(GG)


Single-scatterplot with linear regression in R


library(ggpubr)

library(ggplot2)


ggplot(BA,

       aes(x = Phytoplankton,

           y = Diatoms)) +

 

  geom_point(aes(color = Zones),

             size = 5,

             alpha = 0.7) +

 

  geom_smooth(aes(color = NULL),   

              method = "lm",

              formula = "y~x",

              se=T) +

 

 

  stat_cor(aes(label = paste(..rr.label..,

                             ..p.label..,

                             sep = "~`,`~")),

           r.accuracy = 0.01,

           p.accuracy = 0.001,

           label.x = 0,

           label.y = 375,

           size = 3)

“geom_smooth” aids the eye in seeing patterns in the presence of overplotting. 


Smoothing method (function) to use, accepts either NULL or a character vector, e.g. "lm", "glm", "gam", "loess" or a function.


“se” to display confidence interval around smooth. (TRUE by default, see level to control.)


Comments