本文介绍了在ggplot2中添加第二个x轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 在图形包中,可以将第二个x轴(表示分布的百分位数)添加到直方图中,如下所示: x hist(x,main =,xlab =Bias) perc 轴(1,at = perc,labels = c(0,10%,20%,30%,40%) ,50%,60%,70%,80%,90%,100%),cex = 0.5,pos = -90) 当然,这看起来很尴尬。那么如何修改下面的ggplot2代码来添加第二个x轴,使百分位数减半,而第一个x轴应该指示原始数值?: library(ggplot2) theme_classic(base_size = 12,base_family =)x qplot(x,main = ,xlab =Bias) perc 有什么帮助?非常感谢!解决方案我并不完全知道你在做什么,因为你的第一个例子实际上并没有产生你所描述的内容。 但简单地将百分比与x轴上的原始值相加,最简单的策略可能是简单地将两者结合 dat perc l )> ggplot(dat,aes(x = x))+ geom_histogram()+ scale_x_continuous(breaks = perc,labels = 1) In the "graphics" package one can add a second x-axis (indicating the percentiles of the distribution) to a histogram as follows: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?: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! 解决方案 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: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) 这篇关于在ggplot2中添加第二个x轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-29 04:26