DEV Community

Dr. Azad Rasul
Dr. Azad Rasul

Posted on

14- Creat basic plots in R Programming

Load trees data from datasets library

library(datasets)
tr<-trees 
head(tr)
Enter fullscreen mode Exit fullscreen mode

Histogram

hist(tr$Volume, col = 'darkred',main="Your title", xlab="Volume")
Enter fullscreen mode Exit fullscreen mode

Image description

Barplot

barplot(tr$Height, names.arg=c(1:31), col='darkgreen')
Enter fullscreen mode Exit fullscreen mode

Image description

Boxplot

boxplot(tr$Height, col = 'darkblue', xlab='Height',ylab='Height (m)')
Enter fullscreen mode Exit fullscreen mode

Image description

boxplot(tr, col='darkorange')
Enter fullscreen mode Exit fullscreen mode

Image description

Scatter plot

plot(tr$Height, tr$Volume, col='#FDAE61', pch=19, xlab='Height', ylab=
      'Volume', main='Your title')
Enter fullscreen mode Exit fullscreen mode

Image description

plot(tr$Volume,tr$Height, col = factor(tr$Girth[1:6]), pch=19,
     xlab='Volume', ylab='Height', main='Your title')

# Legend
legend("topleft",
       legend = levels(factor(tr$Girth[1:6])),
       pch = 19,
       col = factor(levels(factor(tr$Girth[1:6]))))
Enter fullscreen mode Exit fullscreen mode

Image description

Line graph

plot(tr$Height,type = "o",col = "darkgreen", xlab = "Day", ylab = "Value", 
     main = "Your title", ylim = c(0, 85), pch=18, lty=6)
lines(tr$Volume, type = "o", col = "darkblue", pch=19, lty=2)
Enter fullscreen mode Exit fullscreen mode

Image description

Piechart

piedata <-c(79.814, 0.023, 16.21, 1.636, 2.318)
class <- c("Barren Land", "Water", "Built-up Area", "Tree", "Grass")
pct <- round(piedata/sum(piedata)*100, 2)
class <- paste(class, pct)
class <- paste(class,"%",sep="")
pie(piedata,labels = class, col=rainbow(5), main="LULC Types in 2013")
Enter fullscreen mode Exit fullscreen mode

Image description

If you like the content, please SUBSCRIBE to my channel for the future content

To get full video tutorial and certificate, please, enroll in the course through this link:
https://www.udemy.com/course/r-for-research/?referralCode=B6DCFDE343F0592EA61A

Top comments (0)