ggplot2: Scales
Sử dụng scales
library ( ggplot2 ) plot <- qplot ( cty , hwy , data = mpg )
## Error: ggplot2 doesn't know how to deal with data of class numeric
plot
## standardGeneric for "plot" defined from package "graphics" ## ## function (x, y, ...) ## standardGeneric("plot") ## <environment: 0x46d7a88> ## Methods may be defined for arguments: x, y ## Use showMethods("plot") for currently available ones.
plot + aes ( x = drv )
## Error in plot + aes(x = drv): non-numeric argument to binary operator
p <- qplot ( sleep_total , sleep_cycle , data = msleep , colour = vore ) p
## Warning: Removed 51 rows containing missing values (geom_point).
# Explicitly add the default scale p + scale_colour_hue ()
## Warning: Removed 51 rows containing missing values (geom_point).
# Adjust parameters of the default, here changing the appearance # of the legend p + scale_colour_hue ( "What does\nit eat?" , breaks = c ( "herbi" , "carni" , "omni" , NA ), labels = c ( "plants" , "meat" , "both" , "don't know" ))
## Warning: Removed 51 rows containing missing values (geom_point).
# Use a different scale p + scale_colour_brewer ( palette = "Set1" )
## Warning: Removed 51 rows containing missing values (geom_point).
Các chi tiết
Tên
p <- qplot ( cty , hwy , data = mpg , colour = displ )
## Error: ggplot2 doesn't know how to deal with data of class numeric
p
## Warning: Removed 51 rows containing missing values (geom_point).
p + scale_x_continuous ( "City mpg" )
## Warning: Removed 51 rows containing missing values (geom_point).
p + xlab ( "City mpg" )
## Warning: Removed 51 rows containing missing values (geom_point).
p + ylab ( "Highway mpg" )
## Warning: Removed 51 rows containing missing values (geom_point).
p + labs ( x = "City mpg" , y = "Highway" , colour = "Displacement" )
## Warning: Removed 51 rows containing missing values (geom_point).
p + xlab ( expression ( frac ( miles , gallon )))
## Warning: Removed 51 rows containing missing values (geom_point).
breaks và limits
p <- qplot ( cyl , wt , data = mtcars ) p
p + scale_x_continuous ( breaks = c ( 4 , 8 ))
p + scale_x_continuous ( limits = c ( 4 , 8 ))
p <- qplot ( wt , cyl , data = mtcars , colour = cyl ) p
p + scale_colour_gradient ( breaks = c ( 5.5 , 6.5 ))
p + scale_colour_gradient ( limits = c ( 5.5 , 6.5 ))
Chuyển đổi
# Khác nhau giữa log scale và log data: tên các trục khác nhau qplot ( log10 ( carat ), log10 ( price ), data = diamonds )
qplot ( carat , price , data = diamonds ) + scale_x_log10 () + scale_y_log10 ()
Thời gian và ngày tháng
plot <- qplot ( date , psavert , data = economics , geom = "line" ) + ylab ( "Personal savings rate" ) + geom_hline ( xintercept = 0 , colour = "grey50" ) plot
# Cần thêm thư viện 'scales' library ( scales ) plot + scale_x_date ( breaks = date_breaks ( "10 years" ))
plot + scale_x_date ( limits = as.Date ( c ( "2004-01-01" , "2005-01-01" )), labels = date_format ( "%Y-%m-%d" ) )
Màu sắc
# gradient và gradient2 f 2 d <- with ( faithful , MASS :: kde2d ( eruptions , waiting , h = c ( 1 , 10 ), n = 50 )) df <- with ( f 2 d , cbind ( expand.grid ( x , y ), as.vector ( z ))) names ( df ) <- c ( "eruptions" , "waiting" , "density" ) erupt <- ggplot ( df , aes ( waiting , eruptions , fill = density )) + geom_tile () + scale_x_continuous ( expand = c ( 0 , 0 )) + scale_y_continuous ( expand = c ( 0 , 0 )) erupt + scale_fill_gradient ( limits = c ( 0 , 0.04 ))
erupt + scale_fill_gradient ( limits = c ( 0 , 0.04 ), low = "white" , high = "black" )
erupt + scale_fill_gradient2 ( limits = c ( -0.04 , 0.04 ), midpoint = mean ( df $ density ))
# gradientn library ( vcd )
## Error in library(vcd): there is no package called 'vcd'
fill_gradn <- function ( pal ) { scale_fill_gradientn ( colours = pal ( 7 ), limits = c ( 0 , 0.04 )) } # Tải thêm thư viện `colorspace` library ( colorspace ) erupt + fill_gradn ( rainbow_hcl )
erupt + fill_gradn ( diverge_hcl )
erupt + fill_gradn ( heat_hcl )
# 3 ColorBrewer palettes point <- qplot ( brainwt , bodywt , data = msleep , log = "xy" , colour = vore ) area <- qplot ( log10 ( brainwt ), data = msleep , fill = vore , binwidth = 1 ) # Chú ý 'pal=' không làm việc, phải dùng 'palette=', không hiểu # tại sao. point + scale_colour_brewer ( palette = "Set1" )
## Warning: Removed 27 rows containing missing values (geom_point).
point + scale_colour_brewer ( palette = "Set2" )
## Warning: Removed 27 rows containing missing values (geom_point).
point + scale_colour_brewer ( palette = "Pastel1" )
## Warning: Removed 27 rows containing missing values (geom_point).
area + scale_fill_brewer ( palette = "Set1" )
area + scale_fill_brewer ( palette = "Set2" )
area + scale_fill_brewer ( palette = "Pastel1" )
Tùy chỉnh
# Màu tùy chỉnh plot <- qplot ( brainwt , bodywt , data = msleep , log = "xy" ) plot + aes ( colour = vore ) + scale_colour_manual ( values = c ( "red" , "orange" , "yellow" , "green" , "blue" ))
## Warning: Removed 27 rows containing missing values (geom_point).
colours <- c ( carni = "red" , "NA" = "orange" , insecti = "yellow" , herbi = "green" , omni = "blue" ) plot + aes ( colour = vore ) + scale_colour_manual ( values = colours )
## Warning: Removed 27 rows containing missing values (geom_point).
# Hình dạng tùy chỉnh plot + aes ( shape = vore ) + scale_shape_manual ( values = c ( 1 , 2 , 6 , 0 , 23 ))
## Warning: Removed 32 rows containing missing values (geom_point).
# Đa màu sắc huron <- data.frame ( year = 1875 : 1972 , level = LakeHuron ) ggplot ( huron , aes ( year )) + geom_line ( aes ( y = level - 5 ), colour = "blue" ) + geom_line ( aes ( y = level + 5 ), colour = "red" )
## Don't know how to automatically pick scale for object of type ts. Defaulting to continuous
ggplot ( huron , aes ( year )) + geom_line ( aes ( y = level - 5 , colour = "below" )) + geom_line ( aes ( y = level + 5 , colour = "above" ))
## Don't know how to automatically pick scale for object of type ts. Defaulting to continuous
ggplot ( huron , aes ( year )) + geom_line ( aes ( y = level - 5 , colour = "below" )) + geom_line ( aes ( y = level + 5 , colour = "above" )) + scale_colour_manual ( "Direction" , values = c ( "below" = "blue" , "above" = "red" ))
## Don't know how to automatically pick scale for object of type ts. Defaulting to continuous
Scale đồng nhất
x <- colors () luv <- as.data.frame ( convertColor ( t ( col2rgb ( x )), "sRGB" , "Luv" )) qplot ( u , v , data = luv , colour = x , size = I ( 3 )) + scale_colour_identity () + coord_equal ()
Chú giải
# Legends produced by geom: point, line, point and line, and bar. p <- ggplot ( diamonds [ 1 : 100 , ], aes ( price , carat , colour = cut )) p + geom_point ()
p + geom_line ()
p + geom_point () + geom_line ()
p + geom_bar ( binwidth = 100 ) + aes ( fill = cut , y = ..count.. )
# Colour legend, shape legend, colour + shape legend. p <- ggplot ( diamonds [ 1 : 100 , ], aes ( price , carat )) + geom_point () p + aes ( colour = cut )
p + aes ( shape = cut )
p + aes ( shape = cut , colour = cut )