For all of the examples and code below, write the code in R studio (It won't compile in Rfiddle). The dataset used is the HAPPINESS2017 dataset on Sakai.
#Install the package first and import it:
install.packages("ggplot2")
#Import the library.
library(ggplot2)
#Read the dataset; Replace the file path with your path.
HAPPINESS2017 <-read.csv("C:/Users/Neelesh/Desktop/Data101/HAPPINESS2017.csv"")
#Basic ggplot command
p1<-ggplot(HAPPINESS2017, aes(x=INCOME, y=HAPPINESS))
Now for the plot to print, we need to specify the next layer, which is how the symbols should look - do we want points or lines? What color? How big? Let's start with points:
# Print plot with default points
p1 + geom_point()
That's the bare bones of it. Now we have fun with adding layers.
Change the colour of points:
library(ggplot2)
p1 <- ggplot(HAPPINESS2017, aes(x = INCOME, y = HAPPINESS)) + geom_point()
p2 <- p1 + geom_point(color="red") #set one color for all points
p3 <- p1 + geom_point(aes(color = INCOME)) #set color scale by a continuous variable
p4 <- p1 + geom_point(aes(color=factor(IMMIGRANT))) #set color scale by a factor variable
Change shape or size of points:
p2 <- p1 + geom_point(size = 3) #increase all points to size 5
p3 <- p1 + geom_point(aes(shape = factor(IMMIGRANT))) #set point shape by factor variable
Add lines to the scatterplot
p2 <- p1 + geom_point(color="blue") + geom_line() #connect points with line
p3 <- p1 + geom_point(color="red") + geom_smooth(method = "lm", se = TRUE) #add regression line
p4 <- p1 + geom_point() + geom_vline(xintercept = 40000, color="red") #add vertical line
Change the axis labels
There are a few ways to do this. If you only want to quickly add labels you can use the labs() layer. If you want to change the font size and style of the label, then you need to use the theme() layer. If you want to change the limits of the axes, and exactly where the breaks are, you use the scale_x_continuous for the X-axis (and scale_y_continuous for the Y-axis).
p2 <- ggplot(HAPPINESS2017, aes(x = INCOME, y = HAPPINESS)) + geom_point()
p3 <- p2 + labs(x="Income",
y = "Happiness") #label all axes at once
p4 <- p2 + theme(axis.title.x = element_text(face="bold", size=20)) +
labs(x="Income") #label and change font size
p5 <- p2 + scale_x_continuous("Income",
limits=c(20000,120000),
breaks=seq(20000, 120000, 10000)) #adjust axis limits and breaks
Another plot library that is widely used is Plotly. In fact, you can even integrate it with ggplot (it's called ggplotly!). You can learn more about it here: https://plot.ly/r/
There are other libraries as well which allow you to create beautiful plots. Check them out!