ggplot2: qplot()
Dữ liệu
library ( ggplot2 ) head ( diamonds )
## carat cut color clarity depth table price x y z ## 1 0.23 Ideal E SI2 62 55 326 4.0 4.0 2.4 ## 2 0.21 Premium E SI1 60 61 326 3.9 3.8 2.3 ## 3 0.23 Good E VS1 57 65 327 4.0 4.1 2.3 ## 4 0.29 Premium I VS2 62 58 334 4.2 4.2 2.6 ## 5 0.31 Good J SI2 63 58 335 4.3 4.3 2.8 ## 6 0.24 Very Good J VVS2 63 57 336 3.9 4.0 2.5
set.seed ( 1410 ) dsmall <- diamonds [ sample ( nrow ( diamonds ), 100 ), ]
Lệnh cơ bản
# Basic use qplot ( carat , price , data = diamonds )
qplot ( log ( carat ), log ( price ), data = diamonds )
qplot ( carat , x * y * z , data = diamonds )
Các thuộc tính thẩm mỹ
# Color and shape qplot ( carat , price , data = dsmall , colour = color )
qplot ( carat , price , data = dsmall , shape = cut )
# Alpha aesthetic qplot ( carat , price , data = diamonds , alpha = I ( 1 / 10 ))
qplot ( carat , price , data = diamonds , alpha = I ( 1 / 100 ))
qplot ( carat , price , data = diamonds , alpha = I ( 1 / 200 ))
# Add smoother qplot ( carat , price , data = dsmall , geom = c ( "point" , "smooth" ))
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.
qplot ( carat , price , data = diamonds , geom = c ( "point" , "smooth" ))
## geom_smooth: method="auto" and size of largest group is >=1000, so using gam with formula: y ~ s(x, bs = "cs"). Use 'method = x' to change the smoothing method.
Các đối tượng hình học
Thêm smoother
# method = "loess", default for n < 1000 qplot ( carat , price , data = dsmall , geom = c ( "point" , "smooth" ), span = 0.2 )
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.
qplot ( carat , price , data = dsmall , geom = c ( "point" , "smooth" ), span = 1 )
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.
# method = "gam", default for n > 1000 qplot ( carat , price , data = dsmall , geom = c ( "point" , "smooth" ), method = "gam" , formula = y ~ s ( x ))
qplot ( carat , price , data = dsmall , geom = c ( "point" , "smooth" ), method = "gam" , formula = y ~ s ( x , bs = "cs" ))
# method = "lm" qplot ( carat , price , data = dsmall , geom = c ( "point" , "smooth" ), method = "lm" )
library ( splines ) qplot ( carat , price , data = dsmall , geom = c ( "point" , "smooth" ), method = "lm" , formula = y ~ ns ( x , 5 ))
# method = "rlm" library ( MASS ) qplot ( carat , price , data = dsmall , geom = c ( "point" , "smooth" ), method = "rlm" )
Boxplots và jittered points
# Basic command qplot ( color , price / carat , data = diamonds , geom = "jitter" )
qplot ( color , price / carat , data = diamonds , geom = "boxplot" )
# More colorful? qplot ( color , price / carat , data = diamonds , geom = "jitter" , color = color )
qplot ( color , price / carat , data = diamonds , geom = "boxplot" , fill = color )
# Add semi-transparent qplot ( color , price / carat , data = diamonds , geom = "jitter" , alpha = I ( 1 / 5 ))
qplot ( color , price / carat , data = diamonds , geom = "jitter" , alpha = I ( 1 / 50 ))
qplot ( color , price / carat , data = diamonds , geom = "jitter" , alpha = I ( 1 / 200 ))
Hoành đồ và mật độ
# basic qplot ( carat , data = diamonds , geom = "histogram" )
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
qplot ( carat , data = diamonds , geom = "density" )
# add binwidth & xlim qplot ( carat , data = diamonds , geom = "histogram" , binwidth = 1 , xlim = c ( 0 , 3 ))
qplot ( carat , data = diamonds , geom = "histogram" , binwidth = 0.1 , xlim = c ( 0 , 3 ))
qplot ( carat , data = diamonds , geom = "histogram" , binwidth = 0.01 , xlim = c ( 0 , 3 ))
## Warning: position_stack requires constant width: output may be incorrect
# more colorful? qplot ( carat , data = diamonds , geom = "density" , colour = color )
qplot ( carat , data = diamonds , geom = "histogram" , fill = color )
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
Biểu đồ cột
# simple version qplot ( color , data = diamonds , geom = "bar" )
# weighted version (by carat) qplot ( color , data = diamonds , geom = "bar" , weight = carat ) + scale_y_continuous ( "carat" )
# more colorful? qplot ( color , data = diamonds , geom = "bar" , fill = color )
Chuỗi thời gian
# with line qplot ( date , unemploy / pop , data = economics , geom = "line" )
qplot ( date , uempmed , data = economics , geom = "line" )
# with path year <- function ( x ) as.POSIXlt ( x ) $ year + 1900 qplot ( unemploy / pop , uempmed , data = economics , geom = c ( "point" , "path" ))
qplot ( unemploy / pop , uempmed , data = economics , geom = "path" , color = year ( date )) + scale_size_area ()
Faceting
# facets qplot ( carat , data = diamonds , facets = color ~ . , geom = "histogram" , binwidth = 0.1 , xlim = c ( 0 , 3 ))
# with ..density.. qplot ( carat , ..density.. , data = diamonds , facets = color ~ . , geom = "histogram" , binwidth = 0.1 , xlim = c ( 0 , 3 ))
Các tùy chọn khác
qplot ( carat , price , data = dsmall , xlab = "Price ($)" , ylab = "Weight (carats)" , main = "Price-weight relationship" )
qplot ( carat , price / carat , data = dsmall , ylab = expression ( frac ( price , carat )), xlab = "Weight (carats)" , main = "Small diamonds" , xlim = c ( .2 , 1 ) )
## Warning: Removed 35 rows containing missing values (geom_point).
qplot ( carat , price , data = dsmall , log = "xy" )