美文网首页
ggplot2_参数简介(一)

ggplot2_参数简介(一)

作者: cppcwang | 来源:发表于2017-10-23 14:29 被阅读193次
    1. aes Construct aesthetic mappings

    aes用于定义x, y,并且可以对响应的数据进行计算后绘图

    aes(x = mpg, y = wt)
    aes(mpg, wt)
    # You can also map aesthetics to functions of variables
    aes(x = mpg ^ 2, y = wt / cyl)
    # aes is almost always used with ggplot() or a layer
    ggplot(mpg, aes(displ, hwy)) + geom_point()
    ggplot(mpg) + geom_point(aes(displ, hwy))
    
    1. aes_colour_fill_alpha Colour related aesthetics: colour, fill and alpha

    该命令定义颜色,填充和希腊字母

    # Bar chart example
    c <- ggplot(mtcars, aes(factor(cyl)))
    # Default plotting
    c + geom_bar()
    # Combining both, you can see the changes more clearly
    c + geom_bar(fill = "white", colour = "red")
    k <- ggplot(mtcars, aes(factor(cyl), fill = factor(vs)))
    k + geom_bar()
    # Fill aesthetic can also be used with a continuous variable,连续变量
    m <- ggplot(faithfuld, aes(waiting, eruptions))
    m + geom_raster()
    m + geom_raster(aes(fill = density))
    # Some geoms don't use both aesthetics (i.e. geom_point or geom_line)
    layout(matrix(c(1, 1, 2, 3), 2, 2, byrow = TRUE))
    b <- ggplot(economics, aes(x = date, y = unemploy))
    b + geom_line()
    b + geom_line(colour = "green")
    b + geom_point()
    b + geom_point(colour = "red")
    # For large datasets with overplotting the alpha
    # aesthetic will make the points more transparent
    df <- data.frame(x = rnorm(5000), y = rnorm(5000))
    h <- ggplot(df, aes(x,y))
    h + geom_point()
    h + geom_point(alpha = 0.5)
    h + geom_point(alpha = 1/10)
    # Alpha can also be used to add shading
    j <- b + geom_line()
    j
    yrng <- range(economics$unemploy)
    j <- j + geom_rect(aes(NULL, NULL, xmin = start, xmax = end, fill = party),
    ymin = yrng[1], ymax = yrng[2], data = presidential)
    j
    j + scale_fill_manual(values = alpha(c("blue", "red"), .3))
    
    1. aes_group_order Aesthetics: grouping,分组
    #柱状图
    p <- ggplot(mtcars, aes(wt, mpg))
    p + geom_point(aes(colour = factor(cyl)), size = 4)
    # Or you can use shape to distinguish the data
    p + geom_point(aes(shape = factor(cyl)), size = 4)
    # Using fill
    a <- ggplot(mtcars, aes(factor(cyl)))
    a + geom_bar()
    a + geom_bar(aes(fill = factor(cyl)))
    a + geom_bar(aes(fill = factor(vs)))
    
    # Using facets
    k <- ggplot(diamonds, aes(carat, ..density..)) + geom_histogram(binwidth = 0.2)
    k + facet_grid(. ~ cut)
    # There are three common cases where the default is not enough, and we
    # will consider each one below. In the following examples, we will use a simple
    # longitudinal dataset, Oxboys, from the nlme package. It records the heights
    # (height) and centered ages (age) of 26 boys (Subject), measured on nine
    # occasions (Occasion).
    # Multiple groups with one aesthetic
    h <- ggplot(nlme::Oxboys, aes(age, height))
    # A single line tries to connect all the observations
    h + geom_line()
    # The group aesthetic maps a different line for each subject
    h + geom_line(aes(group = Subject))
    # Different groups on different layers
    h <- h + geom_line(aes(group = Subject))
    # Using the group aesthetic with both geom_line() and geom_smooth()
    # groups the data the same way for both layers
    h + geom_smooth(aes(group = Subject), method = "lm", se = FALSE)
    # Changing the group aesthetic for the smoother layer
    # fits a single line of best fit across all boys
    h + geom_smooth(aes(group = 1), size = 2, method = "lm", se = FALSE)
    # Overriding the default grouping
    # The plot has a discrete scale but you want to draw lines that connect across
    # groups. This is the strategy used in interaction plots, profile plots, and parallel
    # coordinate plots, among others. For example, we draw boxplots of height at
    # each measurement occasion
    
    # 箱线图
    boysbox <- ggplot(nlme::Oxboys, aes(Occasion, height))
    boysbox + geom_boxplot()
    # There is no need to specify the group aesthetic here; the default grouping
    # works because occasion is a discrete variable. To overlay individual trajectories
    # we again need to override the default grouping for that layer with aes(group = Subject)
    boysbox <- boysbox + geom_boxplot()
    boysbox + geom_line(aes(group = Subject), colour = "blue")
    
    1. aes_linetype_size_shape,线的类型
    # Line types should be specified with either an integer, a name, or with a string of
    # an even number (up to eight) of hexadecimal digits which give the lengths in
    # consecutive positions in the string.
    # 0 = blank, 1 = solid, 2 = dashed, 3 = dotted, 4 = dotdash, 5 = longdash, 6 = twodash
    # Data
    df <- data.frame(x = 1:10 , y = 1:10)
    f <- ggplot(df, aes(x, y))
    f + geom_line(linetype = 2)
    f + geom_line(linetype = "dotdash")
    # An example with hex strings, the string "33" specifies three units on followed
    # by three off and "3313" specifies three units on followed by three off followed
    # by one on and finally three off.
    f + geom_line(linetype = "3313")
    # Mapping line type from a variable
    ggplot(economics_long, aes(date, value01)) +
    geom_line(aes(linetype = variable))
    # Size examples
    # Should be specified with a numerical value (in millimetres),
    # or from a variable source
    p <- ggplot(mtcars, aes(wt, mpg))
    p + geom_point(size = 4)
    p + geom_point(aes(size = qsec))
    p + geom_point(size = 2.5) +
    geom_hline(yintercept = 25, size = 3.5)
    # Shape examples
    # Shape takes four types of values: an integer in [0, 25],
    # a single character-- which uses that character as the plotting symbol,
    # a . to draw the smallest rectangle that is visible (i.e., about one pixel)
    # an NA to draw nothing
    p + geom_point()
    p + geom_point(shape = 5)
    p + geom_point(shape = "k", size = 3)
    p + geom_point(shape = ".")
    p + geom_point(shape = NA)
    # Shape can also be mapped from a variable
    p + geom_point(aes(shape = factor(cyl)))
    # A look at all 25 symbols
    df2 <- data.frame(x = 1:5 , y = 1:25, z = 1:25)
    s <- ggplot(df2, aes(x, y))
    s + geom_point(aes(shape = z), size = 4) +
    scale_shape_identity()
    # While all symbols have a foreground colour, symbols 19-25 also take a
    # background colour (fill)
    s + geom_point(aes(shape = z), size = 4, colour = "Red") +
    scale_shape_identity()
    s + geom_point(aes(shape = z), size = 4, colour = "Red", fill = "Black") +
    scale_shape_identity()
    
    1. aes_position 坐标轴位置
    # Generate data: means and standard errors of means for prices
    # for each type of cut
    dmod <- lm(price ~ cut, data = diamonds)
    cuts <- data.frame(cut = unique(diamonds$cut), predict(dmod, data.frame(cut =
    unique(diamonds$cut)), se = TRUE)[c("fit", "se.fit")])
    se <- ggplot(cuts, aes(x = cut, y = fit, ymin = fit - se.fit,
    ymax = fit + se.fit, colour = cut))
    se + geom_pointrange()
    p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
    p + annotate("rect", xmin = 2, xmax = 3.5, ymin = 2, ymax = 25,
    fill = "dark grey", alpha = .5)
    # Geom_segment examples
    p + geom_segment(aes(x = 2, y = 15, xend = 2, yend = 25),
    arrow = arrow(length = unit(0.5, "cm")))
    p + geom_segment(aes(x = 2, y = 15, xend = 3, yend = 15),
    arrow = arrow(length = unit(0.5, "cm")))
    p + geom_segment(aes(x = 5, y = 30, xend = 3.5, yend = 25),
    arrow = arrow(length = unit(0.5, "cm")))
    # You can also use geom_segment to recreate plot(type = "h") :
    counts <- as.data.frame(table(x = rpois(100, 5)))
    counts$x <- as.numeric(as.character(counts$x))
    with(counts, plot(x, Freq, type = "h", lwd = 10))
    ggplot(counts, aes(x, Freq)) +
    geom_segment(aes(yend = 0, xend = x), size = 10)
    

    相关文章

      网友评论

          本文标题:ggplot2_参数简介(一)

          本文链接:https://www.haomeiwen.com/subject/gxkauxtx.html