我能改变x轴在ggplot2中与y轴相交的位置吗?

时间:2022-10-19 00:16:39

I'm plotting some index data as a bar chart. I'd like to emphasise the "above index" and "below index"-ness of the numbers by forcing the x-axis to cross at 100 (such that a value of 80 would appear as a -20 bar.)

我将一些索引数据绘制成条形图。我想强调的是“上面的索引”和“下面的索引”——通过强迫x轴在100处交叉(这样,80的值就会显示为-20条)。

This is part of a much longer process, so it's hard to share data usefully. Here, though, is some bodge-y code that illustrates the problem (and the beginnings of my solution):

这是一个更长的过程的一部分,因此很难有效地共享数据。不过,这里有一些可以说明问题(以及我的解决方案的开始)的代码。

df <- data.frame(c("a","b","c"),c(118,80,65))
names(df) <- c("label","index")

my.plot <- ggplot(df,aes(label,index))
my.plot + geom_bar()

df$adjusted <- as.numeric(lapply(df$index,function(x) x-100))

my.plot2 <- ggplot(df,aes(label,adjusted))
my.plot2 + geom_bar()

I can, of course, change my index calculation to read: (value.new/value.old)*100-100 then title the chart appropriately (something like "xxx relative to index") but this seems clumsy.

当然,我可以将索引计算改为:(value.new/value.old)*100-100,然后适当地将图表标题(类似于“xxx相对于索引”),但这看起来很笨。

So, too, does the approach I've been testing (to run the simple calculation above, then re-label the y-axis.) Is that really the best solution?

我所测试的方法也一样(运行简单的计算,然后重新标记y轴)。这真的是最好的解决方案吗?

No doubt someone's going to tell me that this sort of axis manipulation is frowned upon. If this is the case, please could they point me in the direction of an explanation? At least then I'll have learned something.

毫无疑问,有人会告诉我,这种轴操纵是不允许的。如果是这样的话,请他们指教我如何解释?至少那时我会学到一些东西。

2 个解决方案

#1


1  

The coordinate system of you plot has the x-axis and the y-axis crossing at (0,0). This is just the way you define your coordinate system. You can of course draw a horizontal line at (x = 100), but to call this is x-axis is false.

你的坐标系中有x轴和y轴相交点(0,0)这就是你定义坐标系的方法。你当然可以画一条水平线(x = 100),但这是x轴是假的。

What you already proposed is to redefine your coordinate system by transforming the data. Whether or not this transformation is appropriate is easier to answer with a reproducible example from your side.

你已经提出了重新定义你的坐标系统转换数据。这种转变是否合适更容易回答可再生的例子从你身边。

#2


4  

This doesn't directly answer you question, but instead of missing about with the x-axis, why not make a single grid line a bit thicker? For example,

这并不能直接回答你的问题,但与x轴无关,为什么不让一个网格线稍微厚一点呢?例如,

dd = data.frame(x = 1:10, y = runif(10))
g = ggplot(dd, aes(x, y)) + geom_point()
g + geom_hline(yintercept=0.2, colour="white", lwd=3)

我能改变x轴在ggplot2中与y轴相交的位置吗?

Or as Paul suggested, with a black line and some text:

或者像Paul建议的那样,用一条黑线和一些文字:

    g + geom_hline(yintercept=0.2, colour="black", lwd=3) + 
        annotate("text", x = 2, y = 0.22, label = "Reference")

我能改变x轴在ggplot2中与y轴相交的位置吗?

#1


1  

The coordinate system of you plot has the x-axis and the y-axis crossing at (0,0). This is just the way you define your coordinate system. You can of course draw a horizontal line at (x = 100), but to call this is x-axis is false.

你的坐标系中有x轴和y轴相交点(0,0)这就是你定义坐标系的方法。你当然可以画一条水平线(x = 100),但这是x轴是假的。

What you already proposed is to redefine your coordinate system by transforming the data. Whether or not this transformation is appropriate is easier to answer with a reproducible example from your side.

你已经提出了重新定义你的坐标系统转换数据。这种转变是否合适更容易回答可再生的例子从你身边。

#2


4  

This doesn't directly answer you question, but instead of missing about with the x-axis, why not make a single grid line a bit thicker? For example,

这并不能直接回答你的问题,但与x轴无关,为什么不让一个网格线稍微厚一点呢?例如,

dd = data.frame(x = 1:10, y = runif(10))
g = ggplot(dd, aes(x, y)) + geom_point()
g + geom_hline(yintercept=0.2, colour="white", lwd=3)

我能改变x轴在ggplot2中与y轴相交的位置吗?

Or as Paul suggested, with a black line and some text:

或者像Paul建议的那样,用一条黑线和一些文字:

    g + geom_hline(yintercept=0.2, colour="black", lwd=3) + 
        annotate("text", x = 2, y = 0.22, label = "Reference")

我能改变x轴在ggplot2中与y轴相交的位置吗?