可视化 | ggplot2 2.2.0 新版本11月发布

浏览: 1615

最新版本安装

# install.packages("devtools")
devtools::install_github("hadley/ggplot2")

如果发现错误,请告知,并暂时使用旧版

install.packages("ggplot2")

ggplot2 2.2.0 主要更新内容包括以下几点:

(1)Subtitles and captions

(2)A large rewrite of the facetting system

(3)Improved theme options

(4)Better stacking

(5)Numerous bug fixes and minor improvements

Subtitles and captions

ggplot(mpg, aes(displ, hwy)) +
 geom_point(aes(color = class)) +
 geom_smooth(se = FALSE, method = "loess") +
 labs(
   title = "Fuel efficiency generally decreases with engine size",
   subtitle = "Two seaters (sports cars) are an exception because of their light weight",
   caption = "Data from fueleconomy.gov"
 )

Clipboard Image.png

These are controlled by the theme settings plot.subtitle and plot.caption.

The plot title is now aligned to the left by default. To return to the previous centering, use theme(plot.title = element_text(hjust = 0.5)).

Facets

The facet and layout implementation has been moved to ggproto and received a large rewrite and refactoring. This will allow others to create their own facetting systems, as descrbied in the Extending ggplot2 vignette. Along with the rewrite a number of features and improvements has been added, most notably:

  • Functions in facetting formulas, thanks to Dan Ruderman.

    ggplot(diamonds, aes(carat, price)) + 
     geom_hex(bins = 20) +
     facet_wrap(~cut_number(depth, 6))

Clipboard Image.png

  • Axes were dropped when the panels in facet_wrap() did not completely fill the rectangle. Now, an axis is drawn underneath the hanging panels:

  • ggplot(mpg, aes(displ, hwy)) + 
     geom_point() +
     facet_wrap(~class)

    Clipboard Image.png

  • It is now possible to set the position of the axes through the position argument in the scale constructor:

    ggplot(mpg, aes(displ, hwy)) + 
     geom_point() +
     scale_x_continuous(position = "top") +
     scale_y_continuous(position = "right")

    Clipboard Image.png

  • You can display a secondary axis that is a one-to-one transformation of the primary axis with the sec.axis argument:

    ggplot(mpg, aes(displ, hwy)) + 
     geom_point() +
     scale_y_continuous(
       "mpg (US)",
       sec.axis = sec_axis(~ . * 1.20, name = "mpg (UK)")
     )

    Clipboard Image.png

  • Strips can be placed on any side, and the placement with respect to axes can be controlled with the strip.placement theme option.

    ggplot(mpg, aes(displ, hwy)) + 
     geom_point() +
     facet_wrap(~ drv, strip.position = "bottom") +
     theme(
       strip.placement = "outside",
       strip.background = element_blank(),
       strip.text = element_text(face = "bold")
     ) +
     xlab(NULL)

    Clipboard Image.png

Theming

  • Blank elements can now be overridden again so you get the expected behavior when setting e.g. axis.line.x.

  • element_line() gets an arrow argument that lets you put arrows on axes.

    arrow <- arrow(length = unit(0.4, "cm"), type = "closed")

    ggplot(mpg, aes(displ, hwy)) +
     geom_point() +
     theme_minimal() +
     theme(
       axis.line = element_line(arrow = arrow)
     )
  • Control of legend styling has been improved. The whole legend area can be aligned according to the plot area and a box can be drawn around all legends:

    ggplot(mpg, aes(displ, hwy, shape = drv, colour = fl)) + 
     geom_point() +
     theme(
       legend.justification = "top",
       legend.box.margin = margin(3, 3, 3, 3, "mm"),
       legend.box.background = element_rect(colour = "grey50")
     )

    Clipboard Image.png

  • panel.margin and legend.margin have been renamed to panel.spacing and legend.spacing respectively as this better indicates their roles. A new legend.margin has been actually controls the margin around each legend.

  • When computing the height of titles ggplot2, now inclues the height of the descenders (i.e. the bits g and y that hang underneath). This makes improves the margins around titles, particularly the y axis label. I have also very slightly increased the inner margins of axis titles, and removed the outer margins.

  • The default themes has been tweaked by Jean-Olivier Irisson making them better match theme_grey().

  • Lastly, the theme() function now has named arguments so autocomplete and documentation suggestions are vastly improved.

Stacking bars

position_stack() and position_fill() now stack values in the reverse order of the grouping, which makes the default stack order match the legend.

avg_price <- diamonds %>% 
 group_by(cut, color) %>%
 summarise(price = mean(price)) %>%
 ungroup() %>%
 mutate(price_rel = price - mean(price))

ggplot(avg_price) +
 geom_col(aes(x = cut, y = price, fill = color))

Clipboard Image.png

(Note also the new geom_col() which is short-hand for geom_bar(stat = "identity"), contributed by Bob Rudis.)

Additionally, you can now stack negative values:

ggplot(avg_price) + 
 geom_col(aes(x = cut, y = price_rel, fill = color))

Clipboard Image.png

The overall ordering cannot necessarily be matched in the presence of negative values, but the ordering on either side of the x-axis will match.

If you want to stack in the opposite order, try forcats::fct_rev():

ggplot(avg_price) + 
 geom_col(aes(x = cut, y = price, fill = fct_rev(color)))

Clipboard Image.png

R语言中文社区.jpg

推荐 1
本文由 R语言中文社区 创作,采用 知识共享署名-相同方式共享 3.0 中国大陆许可协议 进行许可。
转载、引用前需联系作者,并署名作者且注明文章出处。
本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责。本站是一个个人学习交流的平台,并不用于任何商业目的,如果有任何问题,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

0 个评论

要回复文章请先登录注册