负值和正值以非零值为中心的条形图 [英] Bar plot with negative and positive values centered on a non-zero value

查看:47
本文介绍了负值和正值以非零值为中心的条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建优势比的条形图.解释性地,它们以1而不是0为中心.因此,我想制作一个条形图,其中小于1的值是负",大于1的值是正"(即距1的距离的图).

I am trying to create a bar graph of odds ratios. Interpretationally, they are centered around 1, not 0. Therefore, I would like to have a bar graph where values below 1 are "negative" and values above 1 are "positive" (i.e., a graph of distance from 1).

实际上,该图应类似于下图,但y轴上的标签应以1为中心,而不是0.

Effectively, the graph should look like the following plot except that the labels on the y axis should be centered around 1, not 0.

我想创建此图不是重新标记y轴,而是通过将其实际居中于1为中心.如果可以,怎么办?

I would like to create this graph not be relabeling of the y axis, but by actually centering it around 1. Is this possible; and if so, how?

创建示例图的代码:

data <- matrix(c(rnorm(5, 1, .5)), 5, 1)
data <- as.data.frame(data)
data[,2] <- data[,1] - .1
data[,3] <- data[,1] + .1
data <- cbind(c(letters[1:5]), data)
names(data) <- c("Category", "OR", "Lower", "Upper")

data.zero <- cbind("Category"=data[,1], data[,2:4] - 1)

require(ggplot2)
ggplot() + 
  geom_bar(data = data.zero, aes(x=Category, y=OR, fill=Category), stat = "identity") +
  scale_fill_brewer(type = "seq", palette = 1) +  
  geom_errorbar(aes(x=Category, y=OR, ymin=Lower, ymax=Upper), 
            width=.1, position=position_dodge(), data=data.zero)

推荐答案

定义一个简单的函数以添加1,如下所示:

Define a simple function to add 1, like so:

plus1_formatter <- function(x) {x +1}

然后添加字符串

+ scale_y_continuous(labels=plus1_formatter)

转到上面的ggplot命令.

to your ggplot command above.

使其可重复设置 seed(123)

之前:

之后:

这篇关于负值和正值以非零值为中心的条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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