I have prepared example of two charts, Multiseries Line chart and Scatterplot to illustrate how ggplot2 is working. Additionally I have put some formatting elements to show how we can improve looks of our charts.
We need libraries below to create our graphs. Install them if you don't have it yet.
The first six rows of my data.frame looks as following:
country date cumulative_cases new_cases_past_week cumulative_deaths new_
Multiseries line chart
Let me explain now what each of the ggplot2 layers means. We start with defining the data we are going to use which is covid2 data.frame. Then we define aesthetics. On x-axis we take date, on y-axis numeric variable "cumulative_cases" and additionally we will visualize categoric variable "country" by colour differentiation. We need to precise now what type of chart we want. I did it by using geom_line() which returns a line chart. If we want different type of chart we will use different geom function. To have better visibility I added size equal 1.25.
The remain function I used help me with formatting the chart. With ggtitle() I defined the main title and with theme() I bold this title and put it in the middle of chart. I add the name of the x and y axis with xlab() and ylab(). With theme() I changed the colour of the background to " cornsilk". At the end I format also the legend with the function guides().
Scatterplot
aes(x=cumulative_deaths,y=cumulative_cases,color=country))+
geom_point()+
geom_smooth(method="lm")+
ggtitle("Correlation between number of deaths vs. number of cases per country")+
xlab("Number of deaths")+ylab("Number of cases")+ theme_gray(base_size=12)+ theme(plot.title=element_text(hjust=0.5),title=element_text(face = "bold"))+theme(panel.background = element_rect(fill="cornsilk"))+guides(color=guide_legend(title = "Country",label.position = "left", reverse=T))
The results of this code looks like this:
Let me explain now what each of the ggplot2 layers means. We start with defining the data we are going to use which is covid2 data.frame. Then we define aesthetics. On x-axis we take numeric variable "cumulative_deaths", on y-axis numeric variable "cumulative_cases" and additionally we will visualize categoric variable "country" by colour differentiation. We need to precise now what type of chart we want. I did it by using geom_point() which returns a scatterplot. With geom_smooth() I add regression line.
The remain function I used, help me with formatting the chart. With ggtitle() I defined the main title. I add the name of the x and y axis with xlab() and ylab(). With theme() I bold the title and put it in the middle of chart, I changed the color of the background to " cornsilk". At the end I format also the legend with the function guides().


Comments
Post a Comment