- Applied Data Visualization with R and ggplot2
- Dr. Tania Moulik
- 251字
- 2021-07-23 16:59:46
Creating a Histogram Using qplot and ggplot
In this section, we want to visualize the humidity distribution for the city of Vancouver. We'll create a histogram for humidity data using qplot and ggplot.
Let's begin by implementing the following steps:
- Create a plot with RStudio by using the following command: qplot(df_hum$Vancouver):
- Use ggplot to create the same plot using the following command:
ggplot(df_hum,aes(x=Vancouver))
This command does not do anything; ggplot2 requires the name of the object that we wish to make. To make a histogram, we have to specify the geom type (in other words, a histogram). aes stands for aesthetics, or the quantities that get plotted on the x- and y- axes, and their qualities. We will work on changing the aesthetics later, in order to visualize the plot more effectively.
Notice that there are some warning messages, as follows:
'stat_bin()' using 'bins = 30'. Pick better value with 'binwidth'.
Warning message:
Removed 1826 rows containing non-finite values (stat_bin).
You can ignore these messages; ggplot automatically detects and removes null or NA values.
- Obtain the histogram with ggplot by using the following command:
ggplot (df_hum, aes(x=Vancouver)) + geom_histogram()
You'll see the following output:
Here's the output code:
require("ggplot2")
require("tibble")
#Load a data file - Read the Humidity Data
df_hum <- read.csv("data/historical-hourly-weather-data/humidity.csv")
#Display the summary
str(df_hum)
qplot(df_hum$Vancouver)
ggplot(df_hum, aes(x=Vancouver)) + geom_histogram()
Refer to the complete code at https://goo.gl/tu7t4y.
In order for ggplot to work, you will need to specify the geometric object. Note that the column name should not be enclosed in strings.
In order for ggplot to work, you will need to specify the geometric object. Note that the column name should not be enclosed in strings.
推薦閱讀
- Oracle SOA Governance 11g Implementation
- Hadoop 2.x Administration Cookbook
- Visual FoxPro 6.0數(shù)據(jù)庫(kù)與程序設(shè)計(jì)
- 數(shù)控銑削(加工中心)編程與加工
- Windows內(nèi)核原理與實(shí)現(xiàn)
- 可編程序控制器應(yīng)用實(shí)訓(xùn)(三菱機(jī)型)
- 大數(shù)據(jù)時(shí)代
- 高維聚類知識(shí)發(fā)現(xiàn)關(guān)鍵技術(shù)研究及應(yīng)用
- AI的25種可能
- The DevOps 2.1 Toolkit:Docker Swarm
- Mastering Ceph
- 簡(jiǎn)明學(xué)中文版Flash動(dòng)畫制作
- 樂高創(chuàng)意機(jī)器人教程(中級(jí) 上冊(cè) 10~16歲) (青少年iCAN+創(chuàng)新創(chuàng)意實(shí)踐指導(dǎo)叢書)
- ARM嵌入式開發(fā)實(shí)例
- iLike就業(yè)SQL多功能教材