资讯 | ggplot2新版本 2.2.0正式发布

浏览: 1449

ggplot2新版本2.2.0已经正式发布,其主要特色如下:

1.副标题和字幕

2.分面系统重写

3.优化主题选项

4.优化堆积图

5.bug修复和优化

此次版本更新主要工作由Thomas Pederson完成。ggraph、ggforce和tweenr包都是由他开发。

ggplot2包安装:

install.packages("ggplot2")

一. 副标题和字幕

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

二. 分面

1.分面公式中使用函数

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

Clipboard Image.png

2.使用facet_wrap()函数绘制坐标轴在面板下方

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

Clipboard Image.png

3.使用position参数设置坐标轴位置

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

Clipboard Image.png

4.设置第二个坐标轴,借助 sec.axis

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

 5.图形位置控制,借助strip.placement 参数

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

三. 主题

1.主题函数theme()经过很大程度优化;

2.element_line()拥有arrow参数,可以在坐标轴设置箭头; 

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)
 )

Clipboard Image.png

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

Clipboard Image.png

四. 堆积图

1.position_stack() 和 position_fill()可以控制以相反的次序堆积值。

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

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

Clipboard Image.png

另外,还可以堆积负值。

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

Clipboard Image.png

标签也可以制作堆积图。Labels can also be stacked, but the default position is suboptimal:

series <- data.frame(
 time = c(rep(1, 4),rep(2, 4), rep(3, 4), rep(4, 4)),
 type = rep(c('a', 'b', 'c', 'd'), 4),
 value = rpois(16, 10)
)

ggplot(series, aes(time, value, group = type)) +
 geom_area(aes(fill = type)) +
 geom_text(aes(label = type), position = "stack")

Clipboard Image.png


Clipboard Image.png

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

1 个评论

都来玩啦

要回复文章请先登录注册