In the "graphics" package one can add a second x-axis (indicating the percentiles of the distribution) to a histogram as follows:
在“图形”包中,可以向直方图添加第二个x轴(表示分布的百分位数),如下所示:
x <- rnorm(1000)
hist(x, main="", xlab="Bias")
perc <- quantile(x, seq(from=.00, to=1, by=.1))
axis(1,at=perc,labels=c("0","10%","20%","30%","40%","50%","60%","70%","80%","90%","100%"),cex=0.5, pos= -90)
That looks awkward, of course. So how can I modify the following ggplot2 code to add a second x-axis, shwing the percentiles, while the first x-axis should indicate the raw values?:
当然,这看起来很尴尬。那么我怎样才能修改下面的ggplot2代码来添加第二个x轴,甩掉百分位数,而第一个x轴应该指示原始值?:
library(ggplot2)
theme_classic(base_size = 12, base_family = "")
x <- rnorm(1000)
qplot(x, main="", xlab="Bias")
perc <- quantile(x, seq(from=.00, to=1, by=.1))
Any help? Many thanks in advance!
有帮助吗?提前谢谢了!
2 个解决方案
#1
9
I'm not entirely certain what you're after, since your first example doesn't actually produce what you describe.
我不完全确定你所追求的是什么,因为你的第一个例子并没有真正产生你描述的东西。
But in terms of simply adding the percentage along with the raw value along the x axis, the easiest strategy would probably be to simply combine the two with a line break in a single set of labels:
但就简单地沿x轴添加百分比和原始值而言,最简单的策略可能是简单地将两者与一组标签中的换行符组合:
dat <- data.frame(x = rnorm(1000))
perc <- quantile(dat$x,seq(from = 0,to = 1,by = 0.1))
l <- paste(round(perc,1),names(perc),sep = "\n")
> ggplot(dat,aes(x = x)) +
geom_histogram() +
scale_x_continuous(breaks = perc,labels = l)
#2
9
Here's another approach which uses annotate(...)
and does not require that the two scales have the same breaks.
这是另一种使用注释(...)的方法,并不要求两个尺度具有相同的间隔。
library(ggplot2)
library(grid)
set.seed(123)
x <- rnorm(1000)
perc <- quantile(x, seq(from=.00, to=1, by=.1))
labs <- gsub("\\%","",names(perc)) # strip "%" from names
yval <- hist(x,breaks=30,plot=F)$count
yrng <- diff(range(yval))
g1 <- ggplot() +
geom_histogram(aes(x=x))+
xlim(range(x))+
coord_cartesian(ylim=c(0,1.1*max(yval)))+
labs(x="")+
annotate(geom = "text", x = perc, y = -0.1*yrng, label = labs, size=4) +
annotate(geom = "text", x=0, y=-0.16*yrng, label="Bias", size=4.5)+
theme(plot.margin = unit(c(1, 1, 2, 1), "lines"))
g2 <- ggplot_gtable(ggplot_build(g1))
g2$layout$clip[g2$layout$name == "panel"] <- "off"
grid.draw(g2)
This adds the second x-axis and the label using annotate(...)
. The last three lines of code turn off clipping of the viewport. Otherwise the annotations aren't visible.
这会使用注释(...)添加第二个x轴和标签。最后三行代码关闭了视口的剪切。否则注释不可见。
Credit to @Henrik for his answer to this question.
感谢@Henrik对这个问题的回答。
#1
9
I'm not entirely certain what you're after, since your first example doesn't actually produce what you describe.
我不完全确定你所追求的是什么,因为你的第一个例子并没有真正产生你描述的东西。
But in terms of simply adding the percentage along with the raw value along the x axis, the easiest strategy would probably be to simply combine the two with a line break in a single set of labels:
但就简单地沿x轴添加百分比和原始值而言,最简单的策略可能是简单地将两者与一组标签中的换行符组合:
dat <- data.frame(x = rnorm(1000))
perc <- quantile(dat$x,seq(from = 0,to = 1,by = 0.1))
l <- paste(round(perc,1),names(perc),sep = "\n")
> ggplot(dat,aes(x = x)) +
geom_histogram() +
scale_x_continuous(breaks = perc,labels = l)
#2
9
Here's another approach which uses annotate(...)
and does not require that the two scales have the same breaks.
这是另一种使用注释(...)的方法,并不要求两个尺度具有相同的间隔。
library(ggplot2)
library(grid)
set.seed(123)
x <- rnorm(1000)
perc <- quantile(x, seq(from=.00, to=1, by=.1))
labs <- gsub("\\%","",names(perc)) # strip "%" from names
yval <- hist(x,breaks=30,plot=F)$count
yrng <- diff(range(yval))
g1 <- ggplot() +
geom_histogram(aes(x=x))+
xlim(range(x))+
coord_cartesian(ylim=c(0,1.1*max(yval)))+
labs(x="")+
annotate(geom = "text", x = perc, y = -0.1*yrng, label = labs, size=4) +
annotate(geom = "text", x=0, y=-0.16*yrng, label="Bias", size=4.5)+
theme(plot.margin = unit(c(1, 1, 2, 1), "lines"))
g2 <- ggplot_gtable(ggplot_build(g1))
g2$layout$clip[g2$layout$name == "panel"] <- "off"
grid.draw(g2)
This adds the second x-axis and the label using annotate(...)
. The last three lines of code turn off clipping of the viewport. Otherwise the annotations aren't visible.
这会使用注释(...)添加第二个x轴和标签。最后三行代码关闭了视口的剪切。否则注释不可见。
Credit to @Henrik for his answer to this question.
感谢@Henrik对这个问题的回答。