当已经定义了"breaks"(R,ggplot)时,对x轴标签进行排序 [英] Sequence x-Axis labels when when 'breaks' has already been defined (R, ggplot)
问题描述
我在数据上使用了scale()函数,以避免在进行混合模型时出现高度相关性.现在,我希望原始值出现在我的绘图中.所以我用 x * attr(x,'scaled:scale')+ attr(x,'scaled:center')
反转了缩放比例并将这些值放在我用来绘制的数据框的新列中.举例来说,我的数据现在看起来像这样,其中x是真实值,而x.s是缩放后的值:
I used scale() function on my data to avoid high correlation when doing a mixed model. Now I want the original values to appear in my plot. So I reversed the scaling with x * attr(x, 'scaled:scale') + attr(x, 'scaled:center')
and put the values in a new column of the dataframe that I use to plot. So as an example my data now looks something like this, where x is the real value and x.s the scaled value:
x <- sample(x=1:100, size = 50)
y <- sample(x=1:100, size = 50)
df <- as.data.frame(cbind(x,y))
df$x.s <- scale(df$x)
我现在想用ggplot绘制此图,但在x轴上显示x的值,而不是x.s的缩放值,所以我做了以下事情:
I want to plot this now with ggplot but show the values of x on x-axis and not the scaled values of x.s, so I did the following:
ggplot(df, aes(x = x.s, y = y))+
geom_point()+
scale_x_continuous(labels = df$x, breaks = df$x.s)+
labs(x = "Canopy openness [%]", y = "Rarefied richness") +
theme_bw()
到目前为止,该方法仍然有效,并且输出看起来像这样:
This works so far and the output looks something like this:
我现在的问题是,我希望x轴上的刻度线均匀分布,这通常用 breaks = seq(0,100,10)
进行,但是已经定义了breaks以避免错误. f(...,self = self)中的错误:中断和标签的长度不同
,现在我不知道该怎么做,任何帮助将不胜感激!
My problem now is, that i want the ticks on the x-Axis spread evenly which I would usually do with breaks=seq(0,100,10)
, but breaks is already defined to avoid error Error in f(..., self = self) : Breaks and labels are different lengths
, now I can't figure out how to do this, any help would be appreciated!
如果我在x轴上使用x,则在实际数据集中,使用CI进行的预测回归将不再适用.这是我的数据集1中的图,具有标定值(x.s):
If I use x on x-axis, in real Dataset my prediction regression with CI won´t fit anymore. Here are the Plots from my Dataset 1: with scaled values (x.s):
和2:如果我在x轴上使用x而不是x.s
and 2: If I use x instead of x.s on x-axis
推荐答案
在 x
和 xs
之间存在一对一的线性映射关系,可以这样做的是在 x
的比例尺中指定所需的标签,并在 xs
的比例尺中指定相应的中断点:
There's a 1-to-1 linear mapping relationship between x
and x.s
, so one way you can go about this is to specify the desired labels in x
's scale, and the corresponding breaks in x.s
's scale:
ggplot(df, aes(x = x.s, y = y))+
geom_point()+
scale_x_continuous(labels = seq(0, 100, 10),
breaks = predict(lm(x.s ~ x, data = df),
newdata = data.frame(x = seq(0, 100, 10)))) +
labs(x = "Canopy openness [%]", y = "Rarefied richness") +
theme_bw()
这篇关于当已经定义了"breaks"(R,ggplot)时,对x轴标签进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!