简体   繁体   English

ggplot2条形图中的偏移x轴

[英]Offset x axis in ggplot2 bar plot

I'd like to create a ggplot2 plot with the bars being on the negative side and the positive side of the x axis like so: 我想创建一个ggplot2图,其条形位于x轴的负侧和正侧,如下所示:

df <- data.frame(
  var1 = c("a", "b", "c", "d", "e", "f", "g"),
  var2 = c(-3, -2, -1, 0, 1, 2, 3)
)

ggplot(df, aes(x=var1, y=var2, fill=var1)) +
  geom_bar(stat="identity", position="dodge") +
  guides(fill=FALSE)

在此处输入图片说明

with the slight difference that the values are all positive. 这些值都是正数,略有不同。 as an example we add 10 to each data point in df$var2 and now want to display the relative change around the mean of all values - value 10. the plot should look the same with the exception of the values for the y axis. 例如,我们向df$var2每个数据点添加10,现在要显示所有值的均值-值10的相对变化。除了y轴的值外,图应看起来相同。

df2 <- df
df2$var2 <- df2$var2 + 10

How can I plot df2 to look like df? 如何绘制df2使其看起来像df? I don't like to change the labels on the y axis using breaks= and labels= as I have many different plots with ever changing values. 我不喜欢使用breaks=labels=来更改y轴上的labels=因为我有许多不同的图,其值不断变化。

Any help is highly apprechiated! 非常感谢您的帮助!

EDIT: To comment on @alistaire 's concerns: I generally agree, nevertheless, I think there are circumstances where such a graph is suited and not necessarily misleading. 编辑:对@alistaire的担忧发表评论:尽管如此,我总体上同意,但我认为在某些情况下此类图是合适的,并不一定会引起误解。 If - for instances - I like to display the relative enrichment or depletion around a mean, but at the same time want to convey the information what the actual mean is , than I think such a graph can be quite helpful. 如果-例如-我想显示均值周围的相对富集或耗竭,但同时希望传达信息,则实际均值是 ,比我认为这样的图可能会有所帮助。

For example: See this figure in Nature Genetics (doi:10.1038/ng.3286) 例如:请参阅《自然遗传学》(doi:10.1038 / ng.3286)中的此图

在此处输入图片说明

Legend(a): Percentage of fragments containing GWAS SNPs that interact with a promoter, segregated by individual disease or trait subclass. 图例(a): 按个别疾病或性状亚类分类的 ,含有与启动子相互作用的GWAS SNP 的片段的百分比 The histogram shows the relative enrichment or depletion of each subclass, as compared to the total catalog of 8,808 SNPs analyzed, with data taken from 1,651 independent studies. 直方图显示每个子类的相对富集或耗竭,与所分析的8,808个SNP的总目录相比,数据来自1,651项独立研究。 The average association across all SNPs is shown as a percentage. 所有SNP平均关联 以百分比显示。

To add to @alistaire's comment, with which I agree, you could also try using different geoms for the actual values and the differences, for example something like 要添加到我同意的@alistaire的评论中,您还可以尝试使用不同的几何值作为实际值和差异,例如类似

ggplot(df2, aes(x = var1, y = var2)) + 
  geom_bar(stat = "identity", fill = "grey70") + 
  geom_errorbar(aes(ymin = pmin(var2, mean(var2)), 
                    ymax = pmax(var2, mean(var2)), 
                    colour = var1), size = 1) +
  guides(colour = FALSE)

which would give you a plot like 这会给你一个情节 在此处输入图片说明

I have the same problem (legitimate imho : I want to plot seasonal ratios with an axis at 1 (neutral element…)) 我有同样的问题(合法的恕我直言:我想绘制一个轴为1(中性元素……)的季节性比率)

Using the barplot function in the graphics package 使用graphics包中的barplot功能

    barplot(df$var2-10,offset=10)

Works fine for me. 对我来说很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM