Use of Qplot () in R language _r language

Source: Internet
Author: User
Tags ord random seed
Ggplot2 () function

Ggplot2 is a powerful mapping tool that allows you to create any graphics that will help you solve your problems without being limited by existing graphics types. Qplot ()

Qplot () belongs to Ggplot2 () and can be understood as a simplified version of it.
Qplot is "rapid mapping" (quick plot), as the name suggests, can quickly data visualization analysis. Its usage is similar to the plot function of the R base package. Qplot ()

Parameters

Qplot (x, y = null, ..., data, facets = null,
      margins = FALSE, Geom = "Auto", stat = list (null),
      position = list (NU LL), Xlim = C (Na, NA),
      ylim = C (Na, na), log = "", main = NULL,
      xlab = Deparse (Substitute (x)),
      Ylab = Deparse ( Substitute (y)), ASP = NA)
Detailed details of the parameters

1.x, y: variable name
2.data: Is the Data box (data.frame) type; If you have this argument, then the name of the x,y must correspond to the name of a column variable in the data box
3.facets: Graphic/Data faceted. This is a special concept of ggplot2 mapping, it classifies data according to some rules, each type of data to do a graphic, so the final effect is a page more than a picture
4.margins: Show Boundaries
5.geom: The geometric type of the graph (geometry), which is also the ggplot2 of the drawing concept. Ggplot2 uses geometric types to represent graphic categories, such as point representation scatter graphs, line representation graphs, bar representation column graphs, and so on.
6.stat: Statistic type (statistics), this is more special. The direct combination of data statistics and graphics is one of the reasons why Ggplot2 is powerful and popular.
7.position: Position adjustment of graphics or data, this is not too special, but for graphics but the appearance is very important
8.xlim, Ylim, set upper and lower limit of axis
9.xlab, Ylab, add label on x,y axis
10.asp: Graph aspect ratio qplot do scatter plot using vector data

Plot function, if you do not specify the type of the graphic, qplot default to make a scatter chart. For a given x and y vectors do scatter graphs, qplot usage is similar to the plot function

> Library (GGPLOT2)
> x <-1:1000
> Y <-rnorm (1000)
> Plot (x, y, main= "scatter plot by Plot" ( )
> Qplot (x,y, main= "Scatter plot by Qplot ()")


Working with Data box data

Although vector data can be used directly, Ggplot2 prefers to use data frame type data for drawing. There are several benefits to using a data box: The Data box can be used to store data such as numeric values, strings, factors, etc., and putting data in the same R-box object avoids the confusion of data relationships during use, and facilitates the collation and conversion of data appearance. One of the most direct effects of using a data frame in a ggplot2 is that you can determine the appearance of the graphic element directly by using the data's classification characteristics (column variables in the Data box), which is called mapping (mapping) in Ggplot2, and is automatic.
before demonstrating the benefits of using data box mapping, let's take a look at a set of model data on diamonds provided by the following Ggplot2 diamonds:

 > str (diamonds) Classes ' tbl_df ', ' tbl ' and ' data.frame ': 53940 obs. Variables: $ carat:num 0.23 0.2  1 0.23 0.29 0.31 0.24 0.24 0.26 0.22 0.23 ... $ cut:Ord.factor w/5 levels "Fair" < "good".: 5 4 2 4 2 3 3 3 1 3. $ color:Ord.factor W/7 Levels "D" < "E" < "F" < "G".: 2 2 2 6 7 7 6 5 2 5 ... $ clarity:Ord.factor w /8 Levels "I1" < "SI2" < "SI1".: 2 3 5 4 2 6 7 3 4 5 ... $ depth:num 61.5 59.8 56.9 62.4 63.3 62.8 62.3 61.9 65.1 59.4 ... $ table:num $ price:int 326 326 327 334 335 336 336 337 337 338 (+).  ... $ x:num 3.95 3.89 4.05 4.2 4.34 3.94 3.95 4.07 3.87 4 ... $ y:num 3.98 3.84 4.07 4.23 4.35 3.96 3.98 4.11 3.78 4.05 ... $ z:num 2.43 2.31 2.31 2.63 2.75 2.48 2.47 2.53 2.49 2.39 ... 

You can see that this is a data box (Data.frame) type with 10 variables (columns) and 53,940 measured values (rows) for each variable. The first list is the carat number (carat) of the diamond, which is the numeric data, and the second list is the cut of the diamond (cut), which is the factor type data, there are 5 levels; The third is the diamond color (color), the 7 level factor; With too much data, we take only 100 random observations from the first 7 columns. The data is basically our usual record of the original data style:

> set.seed (1000) # set random seed to make random sampling repeatable
> datax<-diamonds[seq (1,7)]
> Head (datax, 4)
# #       Carat cut   color clarity Depth Table Price
# # 17686  1.23 Ideal     H     VS2  62.2  7130
# # 40932  0.30 Ideal     E     SI1 61.7   499
# 6146   0.90  good     H     VS2  61.9  3989
# 37258  0.31 Ideal     G    VVS1  62.8   977

If you want to make a graph of diamond carat and price relationship, use plot and Qplot functions alike:

Plot (X=datax$carat, Y=datax$price, xlab= "carat", ylab= "price", main= "Plot function")
Qplot (X=carat, Y=price, Data=datax, xlab= "Carat", ylab= "price", main= "Qplot function")



But if you want to cut for the classification of the drawing, the plot function processing is complex, you first have to classify the data extraction, and then a mapping. Although you can use the loop to complete, but the map after the addition of the icon to be very careful, you have to ensure that the data and graphic appearance of the relationship between:

Plot (X=datax$carat, Y=datax$price, xlab= "carat", ylab= "price", main= "Plot function", type= ' n ')
cut.levels <- Levels (datax$cut)
cut.n <-Length (cut.levels) for
(i-in seq (1,CUT.N)) {
  Subdatax <- =cut.levels[i],]
  points (X=subdatax$carat, Y=subdatax$price, Col=i, pch=i)
}
Legend ("TopLeft", legend= Cut.levels, Col=seq (1,CUT.N), Pch=seq (1,CUT.N), box.col= "Transparent", cex=0.8)


But with Ggplot2 you need to think about the data classification and graphic elements of the problem is very little, you just tell it to use for the classification of the data can be:

Qplot (X=carat, Y=price, Data=datax, Color=cut, Shape=cut, main= "Qplot function")

Qplot do graphs

As with the plot function, Qplot can also generate graphs by setting appropriate parameters, which are geom (geometric types). The combination of graphs is very straightforward, combining the vectors that represent the geometric types:

Qplot (X=carat, Y=price, Data=datax, Color=cut, geom= "line", main= "geom=\" line\ "")
Qplot (X=carat, Y=price, data= Datax, Color=cut, Geom=c ("line", "point"), main= "Geom=c" (\ "line\", \ "point\") ")


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.