Do you love to create quick plots in R?. If you say yes, then here is qplot in R. As we know about plotting systems in R, in this article, we will explore the qplot function for faster graphs.
What is qplot in R?
qplot is the short form of quick plot in R. The ggplot2 system offers two functions. One is qplot for the quick or faster plotting and another is ggplot, which is for more customized plots. It is a very easy-to-use plotting function. To use this function, you need to install the ‘ggplot2‘ package.
#Install the ggplot2 package install.packages('ggplot2') #Load the ggplot2 library library(ggplot2)
If you are done with installing and loading the required packages, then let’s move ahead. Now, we are going to load the data “Housing” for this purpose. Let’s explore the data first and then we can move to visualizations.
Load the “Housing” data
We are using the housing data for our quick plots. You can try with other available datasets as well. You can find this dataset on ‘Kaggle’ – https://www.kaggle.com/ashydv/housing-dataset
#Read the data and peek into it df <- read.csv('Housing.csv') head(df)
Explore the dataset to understand the datatypes and the data points as well.
#Display the structure str(df)
'data.frame': 545 obs. of 13 variables: $ price : int 13300000 12250000 12250000 12215000 11410000 10850000 10150000 10150000 9870000 9800000 ... $ area : int 7420 8960 9960 7500 7420 7500 8580 16200 8100 5750 ... $ bedrooms : int 4 4 3 4 4 3 4 5 4 3 ... $ bathrooms : int 2 4 2 2 1 3 3 3 1 2 ... $ stories : int 3 4 2 2 2 1 4 2 2 4 ... $ mainroad : chr "yes" "yes" "yes" "yes" ... $ guestroom : chr "no" "no" "no" "no" ... $ basement : chr "no" "no" "yes" "yes" ... $ hotwaterheating : chr "no" "no" "no" "no" ... $ airconditioning : chr "yes" "yes" "no" "yes" ... $ parking : int 2 3 2 3 2 2 2 0 2 1 ... $ prefarea : chr "yes" "no" "yes" "yes" ... $ furnishingstatus: chr "furnished" "furnished" "semi-furnished" "furnished" ...
Key variables are –
- Bedrooms – Number of bedrooms can be a key aspect of deciding the price of the house.
- Area – Area of the property will also contribute more in pricing.
well, we have the data ready and what you are waiting for? Let’s plot, qplot.
Histogram using qplot in R
Let’s plot a histogram that will give us a sense of the data distribution. You can see a fat histogram with our quick-plot.
#Creates a quick plot of histogram qplot(log(area), data = df, xlab = 'Area of the properties')
Now, we can make use of another variable to see the data distribution using qplot in R.
#Quick plots the histogram with fill parameter qplot(log(area), data = df, xlab = 'Area of the properties', fill = guestroom)
- You can see the distribution of guestroom data points in the context of housing data.
- It is very easy and quick to create a plot using qplot in R.
Scatter plot using qplot
As we know, a scatter plot is commonly used to analyze the relationship between multivariate data. Let’s plot the scatter plot of two variables.
#Creates a quick scatter plot qplot(log(price),log(area),data = df, geom = c('point','smooth'))
The relationship seems to be the best as the distribution is modest.
You can further use other variables in the plotting of the scatter plot. The below graph will show the distribution of price in the context of a Parking facility in an area. Here legend is the parking data and it ranges from 0 to 3.
#Create scatter plot with a legend qplot(log(area),log(price),data = df, color = parking)+geom_smooth(method = 'lm')
Facets using qplot in R
The facet graph is useful to generate small multiple plots which represent the subset of data. These plots are really important in EDA. Using this, you can instantly compare the data distribution against the target variable.
Let’s plot a facet graph for this data.
#Creates facet graphs of subset of data qplot(log(area),log(price), data = df, facets = .~guestroom)+geom_smooth(method = 'lm')
Density plot using qplot
The density plots are commonly used for the plotting of the distribution of a numerical variable. You can plot the density graphs with a filling variable i.e. You can create the plot in the context of other variables. The below density plot will show you how the price varies when the basement is available or not available.
#Creates a density plot qplot(log(price), data = df, geom = 'density', color = basement)
The qplot in R is the more improved version of the plot() function in the ggplot2 system. But qplot offers more features compared to the traditional plot() function. Using this, you can quickly plot graphs and analyze the data and relationships between the attributes. As we know visualization is the heart of EDA, nothing can serve better and faster than qplot in R. That’s all for now. Happy R!!!
More read: qplot documentation