Finding the last entry

 Let's say we have a table like below. The same product run on the same machine more than one time, re-work or re-processed.




What we are interested in the last run. 


Using base command aggregate 

laste_entry_max <- aggregate(RUN_Number ~ ID, last_entry, max)




By doing a merge you can do the subset the table.

laste_entry_max_all_c <- merge(laste_entry_max, last_entry, by = c('ID', 'RUN_Number'), all.x = T) 



If the run number information is not there we can use Time_Stamp


You can also use dplyr package

library(dplyr) 

laste_entry_max <- last_entry %>%  

                      group_by(ID) %>% 

                          slice(which.max(RUN_Number)) 



Creating empty data frame

The easiest way to create a null object.

We can use {} to create a NULL object

Simple example, use in for loop.


df <- {}

for (i in 1:10){

  tbl <- c(paste('loop', i, sep='-'))

  df <- rbind(df,tbl)

}


End result is like this,



But if you want to define the columns,


df <- data.frame(matrix(ncol = 3, nrow = 0)) 

colnames(df) <- c("name", "school", "grade")







Handling big numbers in R

One of the issue with R big (long numbers). You can find very detail explanation on what is the issue. But in this post we discuss in a very simple way. Mainly how to work around when faced with this issue.

A simple example

Let's say need to read text file with varying length numbers.


number
1234567891234567
123456789123456789
9988776655443322111111
54767686587697697696679565664656
2423546567899009052342313244568798978
1000000000000000006666666666660000000000000003333333333


Using red csv

aa <- read.csv("C:\\data\\rblog\\number.txt")


What we get is this.



Checking the length....

nchar(aa$number)
[1] 16 18 20 20 20  5


The numbers are automatically changed to scientific formatting.

To avoid scientific formatting you can use,

options(scipen=999)






First looks it seems to be OK, but if you look at the last digits you can see they are changed, except for the first row.


To avoid this we need to force the data as character. 

aa <- read.csv("C:\\data\\rblog\\number.txt",  colClasses= 'character')

Then we get.




If we force the character to convert to numeric,

aa$number <- as.numeric(aa$number)

The issue of changing last digits will reappear. 





Working with Json file

R may be not the best software to handle JSON files. But for those want to insist on using R instead of Python or some other programs, let see how to work with JSON files.


Read JSON file

The example used here is from https://support.oneskyapp.com/hc/en-us/articles/208047697-JSON-sample-files

lets use a simple JSON file

{
    "fruit": "Apple",
    "size": "Large",
    "color": "Red"
}

In R data is normally handled using data frames. 
So lets convert the JSON to data frame.
The package used to do that is ‘jsonlite’. 

The code
library(jsonlite)
# point the location of the file
json_file1 <-"C:\\data\\rblog\\example_1.json"
# read the file using fronJSON function
json_data1 <- fromJSON(json_file,  flatten = FALSE  )





# convert to data frame
json_data_frame1 <- data.frame(json_data)




More complicated JSON files cannot be handled in the same method.
Lets look at the second example.
json_file2 <-"C:\\data\\rblog\\example_2.json"
json_data2 <- fromJSON(json_file2, flatten = FALSE)




Up to this point no issue, but when try to convert to dataframe, the following error occurs.
Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,  : 
  arguments imply differing number of rows: 1, 0, 801, 684, 60

The reason is very clear, the data contains the lists of different length and dataframe.
But it is possible to create separate  dataframes.



For example,
json_data_frame3 <- data.frame(json_data2$afmlist)





Plotting in R Part V - Date Axis

In this tutorial we will look at the some tips to work with date (date and time).
Working with time stamp scale is different from let say a continuous or discrete scale.

The data source link.



Lets look at the basic plot


# Load libraries
library(ggplot2)

# The set your working directory
setwd("C:/R_Train") # please replace with your own directory


# read the data into a dataframe
tbl <- read.csv("Sample Data/temp.csv")

# convert time stamp
tbl$Time_Stamp <- as.POSIXct(tbl$Time_Stamp, format="%m/%d/%Y %H:%M")

# basic plot
ggplot(tbl, aes(x=Time_Stamp, y=Area)) + 
  geom_line(size = .5, color = "blue") + geom_point(size = 1, color = "darkgreen") +
  theme(panel.grid.major.x = element_line(color="chocolate4"), 
        panel.grid.minor.x = element_line(color = "cyan4")) +
  theme(panel.background = element_rect(fill = 'bisque2', colour = 'black')) +
  theme(plot.background = element_rect(fill = 'aquamarine3', colour = 'coral4')) +
  theme(axis.title.x = element_text(size = 25, color = "navajowhite4", face = "bold"))+
  theme(axis.title.y = element_text(size = 25, color = "navajowhite4", face = "bold")) +
  theme(axis.text.x = element_text(size = 15))+
  theme(axis.text.y = element_text(size = 15)) +
  theme(plot.margin = unit(c(0, 0, 20, 20), "pt"))




Now let see how to set the x scale. Let say major axis every hour.

Code

ggplot(tbl, aes(x=Time_Stamp, y=Area)) + 
  geom_line(size = .5, color = "blue") + geom_point(size = 1, color = "darkgreen") +
  theme(panel.grid.major.x = element_line(color="chocolate4"), 
        panel.grid.minor.x = element_line(color = "cyan4")) +
  theme(panel.background = element_rect(fill = 'bisque2', colour = 'black')) +
  theme(plot.background = element_rect(fill = 'aquamarine3', colour = 'coral4')) +
  theme(axis.title.x = element_text(size = 25, color = "navajowhite4", face = "bold"))+
  theme(axis.title.y = element_text(size = 25, color = "navajowhite4", face = "bold")) +
  theme(axis.text.x = element_text(size = 15))+
  theme(axis.text.y = element_text(size = 15)) +
  theme(plot.margin = unit(c(0, 0, 20, 20), "pt")) +
  scale_x_datetime(breaks = "hour")

Output


As you can see the overlapping of x axis text (label). We can fix it by changing angle.

Code

ggplot(tbl, aes(x=Time_Stamp, y=Area)) + 
  geom_line(size = .5, color = "blue") + geom_point(size = 1, color = "darkgreen") +
  theme(panel.grid.major.x = element_line(color="chocolate4"), 
        panel.grid.minor.x = element_line(color = "cyan4")) +
  theme(panel.background = element_rect(fill = 'bisque2', colour = 'black')) +
  theme(plot.background = element_rect(fill = 'aquamarine3', colour = 'coral4')) +
  theme(axis.title.x = element_text(size = 25, color = "navajowhite4", face = "bold"))+
  theme(axis.title.y = element_text(size = 25, color = "navajowhite4", face = "bold")) +
  theme(axis.text.x = element_text(size = 15))+
  theme(axis.text.y = element_text(size = 15)) +
  theme(plot.margin = unit(c(0, 0, 20, 20), "pt")) +
  scale_x_datetime(breaks = "hour") + 
  theme(axis.text.x = element_text(size = 6,angle = 90))

Output



It is possible to change the scale in different scales, for example every 4 hours.

Code

ggplot(tbl, aes(x=Time_Stamp, y=Area)) + 
  geom_line(size = .5, color = "blue") + geom_point(size = 1, color = "darkgreen") +
  theme(panel.grid.major.x = element_line(color="chocolate4"), 
        panel.grid.minor.x = element_line(color = "cyan4")) +
  theme(panel.background = element_rect(fill = 'bisque2', colour = 'black')) +
  theme(plot.background = element_rect(fill = 'aquamarine3', colour = 'coral4')) +
  theme(axis.title.x = element_text(size = 25, color = "navajowhite4", face = "bold"))+
  theme(axis.title.y = element_text(size = 25, color = "navajowhite4", face = "bold")) +
  theme(axis.text.x = element_text(size = 15))+
  theme(axis.text.y = element_text(size = 15)) +
  theme(plot.margin = unit(c(0, 0, 20, 20), "pt")) +
  scale_x_datetime(breaks = "4 hour") + 
  theme(axis.text.x = element_text(size = 10,angle = 90))

Output



But if you still need the hourly x scale. You can do with minor breaks.

Code

ggplot(tbl, aes(x=Time_Stamp, y=Area)) + 
  geom_line(size = .5, color = "blue") + geom_point(size = 1, color = "darkgreen") +
  theme(panel.grid.major.x = element_line(color="chocolate4"), 
        panel.grid.minor.x = element_line(color = "cyan4")) +
  theme(panel.background = element_rect(fill = 'bisque2', colour = 'black')) +
  theme(plot.background = element_rect(fill = 'aquamarine3', colour = 'coral4')) +
  theme(axis.title.x = element_text(size = 25, color = "navajowhite4", face = "bold"))+
  theme(axis.title.y = element_text(size = 25, color = "navajowhite4", face = "bold")) +
  theme(axis.text.x = element_text(size = 15))+
  theme(axis.text.y = element_text(size = 15)) +
  theme(plot.margin = unit(c(0, 0, 20, 20), "pt")) +
  scale_x_datetime(breaks = "4 hour", minor_breaks = "1 hours") + 
  theme(axis.text.x = element_text(size = 10,angle = 90))

Output



That is the end of Part V of Plotting In R. 

If you have any specific request configuring ggplot2, please leave a comment. 
I will try to add to in the current posts or cover that in future posts.





Plotting in R Part IV - Plotting Margins

In Part I, I have discussed about the basic plot and axis modification/enhancement
In Part II, I have discussed about grid line modification/enhancement
In Part III, I have discussed about panel/plot background modification/enhancement

In this section we will discus how to change the panel and plot area margins.

Recap:

The libraries and data set needed. 
Library: ggplot2, stringr
Dataset: iris (to be loaded from local drive)
IDE: RStudio

# First load the library
library(ggplot2)

library(stringr)
library(ggpubr)
library(jpeg)

# The set your working directory
setwd("C:/R_Train") # please replace with your own directory

# Read the file and create a data frame
tbl <- read.csv("Sample Data/iris.csv")


Get the csv file  from here.
data source 

  • Modify the plot margins
    • Change the top margins
The original image 



    • Change the right margins
Code
ggplot(tbl, aes(x=Name, y=SepalLength, fill = Name)) + 
  geom_bar(stat="identity", width = 0.3) +
  theme(axis.title.x = element_text(size = 20, color = "blue", face = "italic")) +
  theme(axis.text.x = element_text(size = 10, color = "firebrick3", face = "bold")) +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 10)) + 
  theme(axis.title.y = element_text(size = 20, color = "blue", face = "italic")) +
  scale_y_continuous(limits = c(0,350), breaks = seq(0,350,50)) +
  scale_fill_manual("legend", values = c("Iris-setosa"  = "indianred3", "Iris-versicolor" = "lightcyan4", "Iris-virginica" = "darkolivegreen4")) +
  theme(panel.grid = element_blank()) +
  theme(plot.background = element_rect(fill = 'aquamarine3', colour = 'coral4')) + 
  theme(plot.margin = unit(c(0, 50, 0, 0), "pt"))

Output



    • Change the bottom margins
Code
ggplot(tbl, aes(x=Name, y=SepalLength, fill = Name)) + 
  geom_bar(stat="identity", width = 0.3) +
  theme(axis.title.x = element_text(size = 20, color = "blue", face = "italic")) +
  theme(axis.text.x = element_text(size = 10, color = "firebrick3", face = "bold")) +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 10)) + 
  theme(axis.title.y = element_text(size = 20, color = "blue", face = "italic")) +
  scale_y_continuous(limits = c(0,350), breaks = seq(0,350,50)) +
  scale_fill_manual("legend", values = c("Iris-setosa"  = "indianred3", "Iris-versicolor" = "lightcyan4", "Iris-virginica" = "darkolivegreen4")) +
  theme(panel.grid = element_blank()) +
  theme(plot.background = element_rect(fill = 'aquamarine3', colour = 'coral4')) + 
  theme(plot.margin = unit(c(0, 0, 50, 0), "pt"))

Output



    • Change the bottom margins
Code

ggplot(tbl, aes(x=Name, y=SepalLength, fill = Name)) + 
  geom_bar(stat="identity", width = 0.3) +
  theme(axis.title.x = element_text(size = 20, color = "blue", face = "italic")) +
  theme(axis.text.x = element_text(size = 10, color = "firebrick3", face = "bold")) +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 10)) + 
  theme(axis.title.y = element_text(size = 20, color = "blue", face = "italic")) +
  scale_y_continuous(limits = c(0,350), breaks = seq(0,350,50)) +
  scale_fill_manual("legend", values = c("Iris-setosa"  = "indianred3", "Iris-versicolor" = "lightcyan4", "Iris-virginica" = "darkolivegreen4")) +
  theme(panel.grid = element_blank()) +
  theme(plot.background = element_rect(fill = 'aquamarine3', colour = 'coral4')) + 
  theme(plot.margin = unit(c(0, 0, 0, 50), "pt"))

Output





    • All together

Code

ggplot(tbl, aes(x=Name, y=SepalLength, fill = Name)) + 
  geom_bar(stat="identity", width = 0.3) +
  theme(axis.title.x = element_text(size = 20, color = "blue", face = "italic")) +
  theme(axis.text.x = element_text(size = 10, color = "firebrick3", face = "bold")) +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 10)) + 
  theme(axis.title.y = element_text(size = 20, color = "blue", face = "italic")) +
  scale_y_continuous(limits = c(0,350), breaks = seq(0,350,50)) +
  scale_fill_manual("legend", values = c("Iris-setosa"  = "indianred3", "Iris-versicolor" = "lightcyan4", "Iris-virginica" = "darkolivegreen4")) +
  theme(panel.grid = element_blank()) +
  theme(plot.background = element_rect(fill = 'aquamarine3', colour = 'coral4')) + 
  theme(plot.margin = unit(c(50, 50, 50, 50), "pt"))

Output




 theme(plot.margin = unit(c(top, right, bottom, left), "pt"))

That is the end of Part IV of Plotting In R. 

If you have any specific request configuring ggplot2, please leave a comment. 
I will try to add to in the current posts or cover that in future posts.


Plotting in R Part III - Background

In Part I, I have discussed about the basic plot and axis modification/enhancement
In Part II, I have discussed about grid line modification/enhancement

Recap:

The libraries and data set needed. 
Library: ggplot2, stringr
Dataset: iris (to be loaded from local drive)
IDE: RStudio

# First load the library
library(ggplot2)

library(stringr)
library(ggpubr)
library(jpeg)

# The set your working directory
setwd("C:/R_Train") # please replace with your own directory

# Read the file and create a data frame
tbl <- read.csv("Sample Data/iris.csv")


Get the csv file  from here.
data source 


Modify the Backgrounds
  • Modify the panel background

Code
ggplot(tbl, aes(x=Name, y=SepalLength, fill = Name)) + 
  geom_bar(stat="identity", width = 0.3) +
  theme(axis.title.x = element_text(size = 20, color = "blue", face = "italic")) +
  theme(axis.text.x = element_text(size = 10, color = "firebrick3", face = "bold")) +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 10)) + 
  theme(axis.title.y = element_text(size = 20, color = "blue", face = "italic")) +
  scale_y_continuous(limits = c(0,350), breaks = seq(0,350,50)) +
  scale_fill_manual("legend", values = c("Iris-setosa"  = "indianred3", "Iris-versicolor" = "lightcyan4", "Iris-virginica" = "darkolivegreen4")) +
  theme(panel.grid = element_blank()) +
  theme(panel.background = element_rect(fill = 'bisque2', colour = 'black'))


fill = 'bisque2' - as it says its the fill color
colour = 'black' - this is the border color


Output



  • Modify the plot background
Code
ggplot(tbl, aes(x=Name, y=SepalLength, fill = Name)) + 
  geom_bar(stat="identity", width = 0.3) +
  theme(axis.title.x = element_text(size = 20, color = "blue", face = "italic")) +
  theme(axis.text.x = element_text(size = 10, color = "firebrick3", face = "bold")) +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 10)) + 
  theme(axis.title.y = element_text(size = 20, color = "blue", face = "italic")) +
  scale_y_continuous(limits = c(0,350), breaks = seq(0,350,50)) +
  scale_fill_manual("legend", values = c("Iris-setosa"  = "indianred3", "Iris-versicolor" = "lightcyan4", "Iris-virginica" = "darkolivegreen4")) +
  theme(panel.grid = element_blank()) +
  theme(panel.background = element_rect(fill = 'bisque2', colour = 'black')) +
  theme(plot.background = element_rect(fill = 'aquamarine3', colour = 'coral4'))

fill = 'aquamarine3' - as it says its the fill color
colour = 'coral4' - this is the border color

Output





  • Add image as background
To do this you need to install packages ggpubr, jpeg

For this tutorial purposes I have created a image.




Code
# load image
img <- readJPEG("Posts/ggplot3/backgg.jpg")

ggplot(tbl, aes(x=Name, y=SepalLength, fill = Name)) + 
  background_image(img) +
  geom_bar(stat="identity", width = 0.3) +
  theme(axis.title.x = element_text(size = 20, color = "blue", face = "italic")) +
  theme(axis.text.x = element_text(size = 10, color = "firebrick3", face = "bold")) +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 10)) + 
  theme(axis.title.y = element_text(size = 20, color = "blue", face = "italic")) +
  scale_y_continuous(limits = c(0,350), breaks = seq(0,350,50)) +
  scale_fill_manual("legend", values = c("Iris-setosa"  = "indianred3", "Iris-versicolor" = "lightcyan4", "Iris-virginica" = "darkolivegreen4")) +
  theme(panel.grid = element_blank()) +
  theme(plot.background = element_rect(fill = 'aquamarine3', colour = 'coral4'))

Please note the image is added before box plot is plotted in the code.

Output




That is the end of part III of Plotting In R. 

If you have any specific request configuring ggplot2, please leave a comment. I will try to add to in the current posts or cover that in future posts.


Plotting in R Part II - Grid Lines

We will start from end of Part I.

Recap:

The libraries and data set needed. 
Library: ggplot2, stringr
Dataset: iris (to be loaded from local drive)
IDE: RStudio

# First load the library
library(ggplot2)

# The set your working directory
setwd("C:/R_Train") # please replace with your own directory

# Read the file and create a data frame
tbl <- read.csv("Sample Data/iris.csv")


Get the csv file  from here.
data source 


Modify the grid lines

The grid can be modified individually using panel.grid.major.x,   panel.grid.major.y,  panel.grid.minor.x and panel.grid.minor.y.


  • change the color of the major grid of y axis.

Code
ggplot(tbl, aes(x=Name, y=SepalLength, fill = Name)) + 
  geom_bar(stat="identity", width = 0.3) +
  theme(axis.title.x = element_text(size = 20, color = "blue", face = "italic")) +
  theme(axis.text.x = element_text(size = 10, color = "firebrick3", face = "bold")) +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 10)) + 
  theme(axis.title.y = element_text(size = 20, color = "blue", face = "italic")) +
  scale_y_continuous(limits = c(0,350), breaks = seq(0,350,50)) +
  scale_fill_manual("legend", values = c("Iris-setosa"  = "indianred3", "Iris-versicolor" = "lightcyan2", "Iris-virginica" = "darkolivegreen2")) +
  theme(panel.grid.major.y = element_line(colour = "black"))

Output

  • Change the color of the minor grid of y axis.

Code

ggplot(tbl, aes(x=Name, y=SepalLength, fill = Name)) + 
  geom_bar(stat="identity", width = 0.3) +
  theme(axis.title.x = element_text(size = 20, color = "blue", face = "italic")) +
  theme(axis.text.x = element_text(size = 10, color = "firebrick3", face = "bold")) +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 10)) + 
  theme(axis.title.y = element_text(size = 20, color = "blue", face = "italic")) +
  scale_y_continuous(limits = c(0,350), breaks = seq(0,350,50)) +
  scale_fill_manual("legend", values = c("Iris-setosa"  = "indianred3", "Iris-versicolor" = "lightcyan2", "Iris-virginica" = "darkolivegreen2")) +
  theme(panel.grid.major.y = element_line(colour = "black"), panel.grid.minor.y = element_line(colour = "blue"))

Output


  • Change the color of the grid of x axis
Code
ggplot(tbl, aes(x=Name, y=SepalLength, fill = Name)) + 
  geom_bar(stat="identity", width = 0.3) +

  theme(axis.title.x = element_text(size = 20, color = "blue", face = "italic")) +
  theme(axis.text.x = element_text(size = 10, color = "firebrick3", face = "bold")) +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 10)) + 
  theme(axis.title.y = element_text(size = 20, color = "blue", face = "italic")) +
  scale_y_continuous(limits = c(0,350), breaks = seq(0,350,50)) +
  scale_fill_manual("legend", values = c("Iris-setosa"  = "indianred3", "Iris-versicolor" = "lightcyan2", "Iris-virginica" = "darkolivegreen2")) +
  theme(panel.grid.major.y = element_line(colour = "black"), panel.grid.minor.y = element_line(colour = "blue")) +
  theme(panel.grid.major.x = element_line(colour = "black"))


Output




  • Change major grids color at once.

Code

ggplot(tbl, aes(x=Name, y=SepalLength, fill = Name)) + 
  geom_bar(stat="identity", width = 0.3) +
  theme(axis.title.x = element_text(size = 20, color = "blue", face = "italic")) +
  theme(axis.text.x = element_text(size = 10, color = "firebrick3", face = "bold")) +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 10)) + 
  theme(axis.title.y = element_text(size = 20, color = "blue", face = "italic")) +
  scale_y_continuous(limits = c(0,350), breaks = seq(0,350,50)) +
  scale_fill_manual("legend", values = c("Iris-setosa"  = "indianred3", "Iris-versicolor" = "lightcyan2", "Iris-virginica" = "darkolivegreen2")) +
  theme(panel.grid.major = element_line(colour = "black"))

Output
  • Change all the grids at once

Code
ggplot(tbl, aes(x=Name, y=SepalLength, fill = Name)) + 
  geom_bar(stat="identity", width = 0.3) +
  theme(axis.title.x = element_text(size = 20, color = "blue", face = "italic")) +
  theme(axis.text.x = element_text(size = 10, color = "firebrick3", face = "bold")) +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 10)) + 
  theme(axis.title.y = element_text(size = 20, color = "blue", face = "italic")) +
  scale_y_continuous(limits = c(0,350), breaks = seq(0,350,50)) +
  scale_fill_manual("legend", values = c("Iris-setosa"  = "indianred3", "Iris-versicolor" = "lightcyan2", "Iris-virginica" = "darkolivegreen2")) +
  theme(panel.grid = element_line(colour = "black"))


Output


But in some cases you don't want the grid lines. 

  • remove x grid lines
Code
ggplot(tbl, aes(x=Name, y=SepalLength, fill = Name)) + 
  geom_bar(stat="identity", width = 0.3) +
  theme(axis.title.x = element_text(size = 20, color = "blue", face = "italic")) +
  theme(axis.text.x = element_text(size = 10, color = "firebrick3", face = "bold")) +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 10)) + 
  theme(axis.title.y = element_text(size = 20, color = "blue", face = "italic")) +
  scale_y_continuous(limits = c(0,350), breaks = seq(0,350,50)) +
  scale_fill_manual("legend", values = c("Iris-setosa"  = "indianred3", "Iris-versicolor" = "lightcyan2", "Iris-virginica" = "darkolivegreen2")) +
  theme(panel.grid.major.x = element_blank())


Output



  • remove the y minor grid lines
Code

ggplot(tbl, aes(x=Name, y=SepalLength, fill = Name)) + 
  geom_bar(stat="identity", width = 0.3) +
  theme(axis.title.x = element_text(size = 20, color = "blue", face = "italic")) +
  theme(axis.text.x = element_text(size = 10, color = "firebrick3", face = "bold")) +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 10)) + 
  theme(axis.title.y = element_text(size = 20, color = "blue", face = "italic")) +
  scale_y_continuous(limits = c(0,350), breaks = seq(0,350,50)) +
  scale_fill_manual("legend", values = c("Iris-setosa"  = "indianred3", "Iris-versicolor" = "lightcyan2", "Iris-virginica" = "darkolivegreen2")) +
  theme(panel.grid.major.x = element_blank(), panel.grid.minor.y = element_blank())

Output


  • remove all the grid lines
Code
ggplot(tbl, aes(x=Name, y=SepalLength, fill = Name)) + 
  geom_bar(stat="identity", width = 0.3) +
  theme(axis.title.x = element_text(size = 20, color = "blue", face = "italic")) +
  theme(axis.text.x = element_text(size = 10, color = "firebrick3", face = "bold")) +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 10)) + 
  theme(axis.title.y = element_text(size = 20, color = "blue", face = "italic")) +
  scale_y_continuous(limits = c(0,350), breaks = seq(0,350,50)) +
  scale_fill_manual("legend", values = c("Iris-setosa"  = "indianred3", "Iris-versicolor" = "lightcyan2", "Iris-virginica" = "darkolivegreen2")) +
  theme(panel.grid = element_blank())

Output



That is the end of part 2 of Plotting In R. 

If you have any specific request configuring ggplot2, please leave a comment. I will try to add to in the current posts or cover that in future posts.