- Web Application Development with R Using Shiny(Third Edition)
- Chris Beeley Shitalkumar R. Sukhdeve
- 543字
- 2021-07-16 17:52:07
Bar chart
Useful when comparing quantities across categories, bar charts are very simple in base graphics, particularly when combined with the table() command. We will use the mpg dataset, which comes with the ggplot2 package; it summarizes the different characteristics of a range of cars. First, let's install the ggplot2 package. You can do this straight from the console using the following code:
> install.packages("ggplot2")
Alternatively, you can use the built-in package functions in IDEs such as RStudio or RKWard. We'll need to load the package at the beginning of each session in which we want to use this dataset, or in the ggplot2 package itself. From the console, type the following command:
> library(ggplot2)
We will use the table() command to count the number of each type of car featured in the dataset, as shown in the following code:
> table(mpg$class)
This returns a table object (another special object type within R) that contains the columns shown in the following screenshot:
Producing a bar chart of this object is achieved simply using the following code:
> barplot(table(mpg$class), main = "Base graphics")
The barplot function takes a vector of frequencies. Where they are named, as is the case in our example (the table() command returning named frequencies in table form), names are automatically included on the x axis. The defaults for this graph are rather plain. Explore ?barplot and ?par to learn more about fine-tuning your graphics.
We've already loaded the ggplot2 package in order to use the mpg dataset, but if you have shut down R in between these two examples, you will need to reload it by using the following command:
> library(ggplot2)
The same graph is produced in ggplot2 as follows:
> ggplot(data = mpg, aes(x = class)) + geom_bar() +
ggtitle("ggplot2")
This ggplot call shows the three fundamental elements of ggplot calls: the use of a dataframe (data = mpg); the setup of aesthetics (aes(x = class)), which determines how variables are mapped onto axes, colors, and other visual features; and the use of + geom_xxx(). A ggplot call sets up the data and aesthetics, but does not plot anything. Functions such as geom_bar() (there are many others; see ??geom) tell ggplot what type of graph to plot, as well as instructing it to take optional arguments—for example, geom_bar() optionally takes a position argument, which defines whether the bars should be stacked, offset, or stretched to a common height to show proportions instead of frequencies.
These elements are the key to the power and flexibility that ggplot2 offers. Once the data structure is defined, ways of visualizing that data structure can be added and taken away easily, not only in terms of the type of graphic (bar, line, or scatter graph) but also the scales and coordinates system (log10, polar coordinates, and so on) and statistical transformations (smoothing data, summarizing over spatial coordinates, and so on). The appearance of plots can be easily changed with preset and user-defined themes, and multiple plots can be added in layers (that is, adding them to one plot) or facets (that is, drawing multiple plots with one function call).
The base graphics and ggplot versions of the bar chart are shown in the following screenshot for the purposes of comparison:
- CorelDRAW X6 中文版圖形設計實戰從入門到精通
- C++黑客編程揭秘與防范
- Building Django 2.0 Web Applications
- INSTANT PhpStorm Starter
- 計算機網絡與數據通信
- 新一代物聯網架構技術:分層算力網絡
- 互聯網安全的40個智慧洞見:2015年中國互聯網安全大會文集
- 正在爆發的互聯網革命
- Learning Swift(Second Edition)
- Building Web Applications with ArcGIS
- 6G:面向2030年的移動通信
- 大型企業微服務架構實踐與運營
- 無線傳感器網絡定位技術
- 5G時代的大數據技術架構和關鍵技術詳解
- 5G非正交多址接入技術:理論、算法與實現