Translating between qplot and base graphics

Description

There are two types of graphics functions in base graphics, those that draw complete graphics and those that add to existing graphics.

Details

qplot() has been designed to mimic plot(), and can do the job of all other high-level plotting commands. There are only two graph types from base graphics that cannot be replicated with ggplot2: filled.contour() and persp()

Examples

# High-level plotting commands x <- runif(10) y <- 1:10 plot(x, y); dotchart(x, y)

qplot(x, y)

plot(x, y, type = "l")

qplot(x, y, geom = "line")

plot(x, y, type = "s")

qplot(x, y, geom = "step")

plot(x, y, type = "b")

qplot(x, y, geom = c("point", "line"))

boxplot(x, y)

qplot(x, y, geom = "boxplot")
Warning message:
Continuous x aesthetic -- did you forget aes(group=...)?

hist(x)

qplot(x, geom = "histogram")
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

# cdplot(factor(x), y) # qplot(x, fill = y, geom = "density", position = "fill") # coplot(y ~ x | a + b) # qplot(x, y, facets = a ~ b) # Many of the geoms are parameterised differently than base graphics. For # example, hist() is parameterised in terms of the number of bins, while # geom_histogram() is parameterised in terms of the width of each bin. hist(x, bins = 10)
Warning message:
"bins" is not a graphical parameter

Warning message:
"bins" is not a graphical parameter

Warning message:
"bins" is not a graphical parameter

Warning message:
"bins" is not a graphical parameter

qplot(x, geom = "histogram", binwidth = .1)

# qplot() often requires data in a slightly different format to the base # graphics functions. For example, the bar geom works with untabulated data, # not tabulated data like barplot(); the tile and contour geoms expect data # in a data frame, not a matrix like image() and contour(). barplot(table(x))

qplot(x, geom = "bar")

barplot(x)

qplot(seq_along(x), x, geom = "bar", stat = "identity")
Warning message:
`stat` is deprecated

Error: stat_count() must not be used with a y aesthetic.

# image(x) # qplot(X1, X2, data = melt(x), geom = "tile", fill = value) # contour(x) # qplot(X1, X2, data = melt(x), geom = "contour", fill = value) # Generally, the base graphics functions work with individual vectors, not # data frames like ggplot2. qplot() will try to construct a data frame if one # is not specified, but it is not always possible. If you get strange errors, # you may need to create the data frame yourself. df <- data.frame(x = x, y = y) with(df, plot(x, y))

qplot(x, y, data = df)

# By default, qplot() maps values to aesthetics with a scale. To override # this behaviour and set aesthetics, overriding the defaults, you need to use I(). plot(x, y, col = "red", cex = 1)

qplot(x, y, colour = I("red"), size = I(1))

# Low-level drawing # The low-level drawing functions which add to an existing plot are equivalent # to adding a new layer in ggplot2. # Base function ggplot2 layer # curve() geom_curve() # hline() geom_hline() # lines() geom_line() # points() geom_point() # polygon() geom_polygon() # rect() geom_rect() # rug() geom_rug() # segments() geom_segment() # text() geom_text() # vline() geom_vline() # abline(lm(y ~ x)) geom_smooth(method = "lm") # lines(density(x)) geom_density() # lines(loess(x, y)) geom_smooth() plot(x, y)
lines(x, y)

qplot(x, y) + geom_line()

# Or, building up piece-meal qplot(x, y)

last_plot() + geom_line()

# Legends, axes and grid lines # In ggplot2, the appearance of legends and axes is controlled by the scales. # Axes are produced by the x and y scales, while all other scales produce legends. # See ?theme for help changing the appearance of axes and legends. # The appearance of grid lines is controlled by the grid.major and grid.minor # theme options, and their position by the breaks of the x and y scales. # Colour palettes # Instead of global colour palettes, ggplot2 has scales for individual plots. Much # of the time you can rely on the default colour scale (which has somewhat better # perceptual properties), but if you want to reuse an existing colour palette, you # can use scale_colour_manual(). You will need to make sure that the colour # is a factor for this to work. palette(rainbow(5)) plot(1:5, 1:5, col = 1:5, pch = 19, cex = 4)

qplot(1:5, 1:5, col = factor(1:5), size = I(4))

last_plot() + scale_colour_manual(values = rainbow(5))

# In ggplot2, you can also use palettes with continuous values, # with intermediate values being linearly interpolated. qplot(0:100, 0:100, col = 0:100, size = I(4)) + scale_colour_gradientn(colours = rainbow(7))

last_plot() + scale_colour_gradientn(colours = terrain.colors(7))
Scale for 'colour' is already present. Adding another scale for 'colour',
which will replace the existing scale.

# Graphical parameters # The majority of par settings have some analogue within the theme system, or # in the defaults of the geoms and scales. The appearance plot border drawn # by box() can be controlled in a similar way by the panel.background and # plot.background theme elements. Instead of using title(), the plot title is # set with the title option. See ?theme for more theme elements. last_plot() + labs(title = "My Plot Title")

Translating between qplot and base graphics的更多相关文章

  1. R语言学习笔记——Base Graphics

    做exploratory data annalysis的作业,差点被虐死了,R从头开始,边做边学,最后搞到一点多才弄完,还有一个图怎么画都不对,最后发现是数据读取的时候有问题. 用来画图的数据来自:h ...

  2. Graphics for R

    https://cran.r-project.org/web/views/Graphics.html CRAN Task View: Graphic Displays & Dynamic Gr ...

  3. Unity 5.4 测试版本新特性---因吹丝停

    原文:http://blogs.unity3d.com/2016/03/15/enhanced-visuals-better-performance-and-more-the-unity-5-4-pu ...

  4. Android 随想录之 Android 系统架构

    应用层(Application) Android 的应用层由运行在 Android 设备上的所有应用程序共同构成(系统预装程序以及第三方应用程序). 系统预装应用程序包含拨号软件.短信.联系人.邮件客 ...

  5. Android核心分析之二十三Andoird GDI之基本原理及其总体框架

     Android GDI基本框架 在Android中所涉及的概念和代码最多,最繁杂的就是GDI相关的代码了.但是本质从抽象上来讲,这么多的代码和框架就干了一件事情:对显示缓冲区的操作和管理. GDI主 ...

  6. Android系统在超级终端下必会的命令大全(adb shell命令大全)

    . 显示系统中全部Android平台: android list targets . 显示系统中全部AVD(模拟器): android list avd . 创建AVD(模拟器): android c ...

  7. TabControl控件的美化

    文件下载:http://files.cnblogs.com/zfanlong1314/TabControlEX.rar 本文转载:http://www.cnblogs.com/lmlblog/arch ...

  8. android图形基础知识

    Android核心分析(23)-----Andoird GDI之基本原理及其总体框架 2010-06-13 22:49 18223人阅读 评论(18) 收藏 举报 AndroidGDI基本框架 在An ...

  9. Android Skia和2D图形系统 .

    Android Skia 和 2D 图形系统 1 Skia 概述 Skia 是 Google 一个底层的图形.图像.动画. SVG .文本等多方面的图形库,是 Android 中图形系统的引擎. Sk ...

随机推荐

  1. Accounting_会计基础

    会计基础 1.会计:是以货币为主要计量单位,反映和监督一个单位经济活动的一种经济管理工作. 2.会计核算职能:指以货币为主要计量单位,通过确认.记录.计算.报告等环节,对特定主体的经济活动进行记账.算 ...

  2. 共享内存简介和mmap 函数

    一.共享内存简介 共享内存区是最快的IPC形式,这些进程间数据传递不再涉及到内核,换句话说是进程不再通过执行进入内核的系统调用来传递彼此的数据. 即每个进程地址空间都有一个共享存储器的映射区,当这块区 ...

  3. Unix环境高级编程(十二)线程控制

    本章介绍了一个进程中多个线程之间如何保持数据的似有性及进程的系统调用如何与线程进行交互. 1.线程限制: Single Unix定义了一线线程操作的限制,和其他的限制一样,可以通过sysconf来查询 ...

  4. Unix环境高级编程(八)进程关系

    本章看后给人似懂非懂的感觉,主要是不知道实际当中如何去使用.通过前面几章的学习,每个进程都有一个父进程,当子进程终止时,父进程得到通知并取得子进程的退出状态.先将本章基本的知识点总结如下,日后再看时候 ...

  5. Aixs2 使用总结,持续更新中 ...

    参考博客:http://zhangjunhd.blog.51cto.com/113473/23692     消息交换模式. 目前Axis2支持三种模式:In-Only.Robust-In和In-Ou ...

  6. mysql 返回多列的方式

    SELECT * FROM (SELECT 'success' as _result) a,(SELECT @gid as gid) b;

  7. servlet中 getRealPath deprecated(被废弃)

    servlet中, HttpRequestServlent req;  req.getRealPath 被废弃,使用this.getServletContext().getRealPath()替代:

  8. Creating the Help Page in ASP.NET Web API

    Introduction In this article we will define the process of creating the help page in the ASP .NET We ...

  9. Centos编译Hadoop 2.x 源码

    1. 前言 Hadoop-2.4.0的源码目录下有个BUILDING.txt文件,它介绍了如何在Linux和Windows下编译源代码,本文基本是遵照BUILDING.txt指示来操作的,这里再做一下 ...

  10. 关于python调用zabbix api接口

    因公司业务需要,引进了自动化运维,所用到的监控平台为zbbix3.2,最近正在学习python,计划使用python调用zabbix api接口去做些事情,如生成报表,我想最基本的是要取得zabbix ...