Detailed usage of the Qplot () function

Source: Internet
Author: User

Detailed usage of the Qplot () function:

Library (GGPLOT2)

# test Data set, Ggplot2 built-in diamond data
Qplot (carat, price, data = diamonds)
Dsmall <-diamonds[sample (Nrow (diamonds), [+]),] #对diamonds数据集进行抽样

#1. Visualize by basic classification of Color,size,shape

#1.1 Simple scatter plot (using color classification, diamonds of different colors are represented by dots of different colors)
Qplot (carat, price, data = dsmall, colour = color)



#1.2. Simple scatter plots (using shape classification, different cutting methods are represented by different shapes of points)
Qplot (carat, price, data = dsmall, shape = cut)



#2. Drawing different types of charts: Geom parameters

Geom= "" In Qplot (x,y,data=data,geom= "") the type of graphic used to control the output
I. Two variable diagram
(1) geom= "points", default parameters, Plot scatter plot (x, y)
(2) geom= "Smooth" to draw smooth curves (based on loess, GAM, LM, RLM,GLM)
(3) geom= "BoxPlot" plot the box line diagram, when x is a property variable (factor), Y is a numeric variable

Ii. Single-Variable graphs
(4) geom= "Histogram", histogram
(5) geom= "density", nuclear density estimation map
(6) geom= "Bar", bar chart Barchart

Iii. Time Series
(7) geom= "line", linear chart, available for time series (when x=date)
(8) geom= "path", road map (see later)


# 2.1 Plot scatter plot + smooth line at the same time
Qplot (carat, price, data = Dsmall, Geom=c ("point", "smooth"))



#参数调整: method= "" and so on
# (a). method = "Loess", default smoothing algorithm, adjusts window width via span=, span=0 (fluctuation) to span=1 (smooth)
Qplot (carat, price, data = Dsmall, Geom = C ("point", "smooth"),
method = "Loess", span=0.2)

&NBSP;



# (b). Method = "gam": GAM is more efficient than loess in big data and needs to be loaded into the MGCV Package
Library (MGCV)
Qplot (carat, price, data = Dsmall, Geo m = C ("point", "smooth"),
      method= "gam", formula = Y ~ s (x))

# (c). method= "LM", linear smoothing
Qplot (carat, price, data = Dsmall, Geom = C ("point", "smooth"),
     method = " LM ")

# method=" LM ", formula = y ~ NS (x, 3), three times natural spline, need to load splines Package
Library (splines)
Qplot (carat, price, data = Dsmall, Geom = C ("point", "smooth"),
      method = "LM", formula = y ~ NS (x, 3))

# method = "RLM", robust linear model, is less affected by outliers, need to load MASS Package
Library (MASS)
Qplot (carat, price, data = Dsmall, Geom = C (" Point "," smooth "),
      method =" RLM ")


# 2.2:x is an attribute variable, y is a continuous variable, drawing BoxPlot
Qplot (color, Price/carat, data=diamonds,geom= "BoxPlot")



# 2.3: Single variable, histogram
Qplot (carat, data = diamonds, Geom = "histogram")



#2.4: Single variable, kernel density estimation diagram
Qplot (carat, data = diamonds, Geom = "Density")



# density drawing in different colors
Qplot (carat, data = diamonds, Geom = "Density", colour=color)



# 2.5 bar chart (histogram)
#计数, COUNT (color)
Qplot (color, data = diamonds, Geom = "Bar")

#加权, for each sum (carat), similar to a PivotChart in Excel, calculates the sum of carat by different color
Qplot (color, data = diamonds, Geom = "Bar", weight = carat)




#2.6. Time-series
Qplot (date, unemploy/pop, data = economics, Geom = "line")




#2.7. Path plot
The relationship between the #如果要查看失业率 (Unemploy/pop) and the mean time to unemployment (uempmed), one method is to use a scatter plot, but doing so will result in the inability to observe a trend over time, and path plot uses shades of color to represent the year. As the color changes from light blue to dark blue, it is possible to observe the changing trends in the relationship between unemployment and unemployment time.

#具体实现: First Custom Function year (), converts the time in string format to years
Year <-function (x) as. Posixlt (x) $year + 1900

#画出path plot, color by year from shallow to deep
Qplot (Unemploy/pop, uempmed, data = economics,
Geom = "path", colour = year (date))

Detailed usage of the Qplot () function

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.