如何在R中叠加条形图? [英] How to superimpose bar plots in R?

查看:315
本文介绍了如何在R中叠加条形图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个类似于下图的图形(取自Ro,Russell和& Lavie,2001年)。在他们的图表中,他们绘制了反应时间条内误差(即准确性)的条形图。基本上,我正在寻找一种在条形图中绘制条形的方法。

I'm trying to create a figure similar to the one below (taken from Ro, Russell, & Lavie, 2001). In their graph, they are plotting bars for the errors (i.e., accuracy) within the reaction time bars. Basically, what I am looking for is a way to plot bars within bars.

我知道创建这样的图形会遇到很多挑战。首先,哈德利指出,无法在ggplot2中创建具有两个比例的图形,因为这些图形从根本上来说是有缺陷的(请参阅

I know there are several challenges with creating a graph like this. First, Hadley points out that it is not possible to create a graph with two scales in ggplot2 because those graphs are fundamentally flawed (see Plot with 2 y axes, one y axis on the left, and another y axis on the right)

不过,带有重叠条形图的图形似乎可以解决这个双重问题,我正在尝试寻找一种在其中创建图形的方法R。任何帮助将不胜感激。

Nonetheless, the graph with superimposed bars seems to solve this dual sclaing problem, and I'm trying to figure out a way to create it in R. Any help would be appreciated.

推荐答案

在基本R中,使用 par( new = T)添加到现有图形中

It's fairly easy in base R, by using par(new = T) to add to an existing graph

set.seed(54321) # for reproducibility

data.1 <- sample(1000:2000, 10)
data.2 <- sample(seq(0, 5, 0.1), 10)

# Use xpd = F to avoid plotting the bars below the axis
barplot(data.1, las = 1, col = "black", ylim = c(500, 3000), xpd = F)
par(new = T)
# Plot the new data with a different ylim, but don't plot the axis
barplot(data.2, las = 1, col = "white", ylim = c(0, 30), yaxt = "n")
# Add the axis on the right
axis(4, las = 1)

这篇关于如何在R中叠加条形图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆