R - 我如何绘制这样的ggplot2 [英] R - how can I plot such ggplot2
问题描述
名称Odds_Ratio_(log2)p值
Ann -1.80494 0.05
Lucy 2.51017 0.1
Sally -1.97779 0.01
...
我想使图形看起来像图B或C.
要在y轴上有p值,在x轴上有优势比(除以这样的垂直线)和绘制符号旁边的名称。我没有分数大小,所以这里不可能改变符号大小。
我该怎么做?
我真的希望有人能帮助我,因为我的R绘制知识让我只能像这样绘制图:
plot(数据$ V2,数据$ V3,main =Ratio ,xlab =p-value,ylab =赔率(Log2))
。 >?abline 和?text
。
d < - data.frame(Name = LETTERS,Odds_Ratio_log2 = $ runif(26,-8,8),
p_value = runif(26))
plot(d $ Odds_Ratio_log2,d $ p_value,pch = 20,xlim = c(-8,8), ylim = c(0,1),
axes = F,xlab ='',ylab ='',yaxs ='i')
abline(v = 0,lwd = 3)
轴(3,lwd = 3,at = seq(-8,8,1),cex.axis = 0.8,lwd.ticks = 1)
mtext('Odds(Log2)',3,line = 2.5)
text(d $ Odds_Ratio_log2,d $ p_value,d $ Name,pos = 4,cex = 0.7)
< hr>
更新:
我已经添加 yaxs ='i'
到上面的 plot
调用,因为除非我们这样做,否则顶轴的截取略高于y = 1,可能会给出p值更低的印象。 p>
请注意,该图不是旋转的,本身。相反,我们使用 axes = F
来禁止轴1和轴2(默认的x轴和y轴),取而代之的是绘制轴3(顶轴;参见?轴
)。然后,我们使用 mtext
绘制轴3的标签。要在图的左边绘制标签,可以使用 ylab
参数(我已将其设置为'
)放入图
函数中,或者使用 mtext('p-value',2)
。
或者,如果您希望中央垂直线具有与y值对应的刻度和标签,。然后,使用段
添加刻度标记,
段(-0.1,seq(0,1,0.1),0,seq(0,1,0.1),lwd = 2)
text(rep(0,10),seq(0,1,0.1),seq(0,1,0.1),cex = 0.7,pos = 2)
最终结果如下所示:
opar< ; - par(no.readonly = TRUE)
pre>
d< - data.frame(Name = LETTERS,Odds_Ratio_log2 = runif(26,-8,8),
p_value = runif(26))
plot(d $ Odds_Ratio_log2,d $ p_value,pch = 20,xlim = c(-8,8),ylim = c(0,1),
axes = F,xlab ='',ylab = '',yaxs ='i',col ='gray20')
abline(v = 0,lwd = 3)
axis(3,lwd = 3,at = seq(-8, 1),cex.axis = 0.8,lwd.ticks = 1)
mtext('Odds(Log2)',3,line = 2.5)
text(d $ Odds_Ratio_log2,d $ p_value,d $名称,pos = 4,offset = 0.3,cex = 0.7)
par(xpd = NA)
段(-0.1,seq(0.1,0.9,0.1),0,seq 0.1,0.9,0.1),lwd = 2)
text(rep(0,10),seq(0.1,0.9,0) .1),seq(0.1,0.9,0.1),
cex = 0.7,pos = 2,offset = 0.3)
par(opar)
I have data that looks like this:
Name Odds_Ratio_(log2) p-value Ann -1.80494 0.05 Lucy 2.51017 0.1 Sally -1.97779 0.01 ...
And I want to make graph that would look something like Figure B or C.
To have p-values on y-axis, odds ratio on x axis (divided by such vertical line) and Names next to plotted symbol. I don't have fractional size so it's impossible to change symbol size here.
How can I do it?
I really hope that someone will help me with this, as my R plotting knowledge lets me to make plots only like this:plot(data$V2,data$V3, main="Ratio", xlab="p-value",ylab="Odds ratio (Log2)")
.
解决方案You should refer to
?plot
,?abline
, and?text
. That said, here's one way to do it with base functions.d <- data.frame(Name=LETTERS, Odds_Ratio_log2=runif(26, -8, 8), p_value=runif(26)) plot(d$Odds_Ratio_log2, d$p_value, pch=20, xlim=c(-8, 8), ylim=c(0, 1), axes=F, xlab='', ylab='', yaxs='i') abline(v=0, lwd=3) axis(3, lwd=3, at=seq(-8, 8, 1), cex.axis=0.8, lwd.ticks=1) mtext('Odds (Log2)', 3, line=2.5) text(d$Odds_Ratio_log2, d$p_value, d$Name, pos=4, cex=0.7)
UPDATE:
I've added
yaxs='i'
to theplot
call above, because unless we do this, the top axis intercepts slightly higher than y=1, perhaps giving an impression of lower p-values.Note that the plot is not "rotated", per se. Rather, we suppress axis 1 and 2 (the default x and y axes), with
axes=F
, and instead we plot axis 3 (the top axis; see?axis
). We then usemtext
to plot the label for axis 3. To plot a label at the left edge of the plot, you can either use theylab
argument (which I've set equal to''
) in theplot
function, or usemtext('p-value', 2)
.Alternatively, if you want the central vertical line to have ticks and labels corresponding to y-values, . Then, use
segments
to add tick marks, andtext
to add tick labels, e.g.:segments(-0.1, seq(0, 1, 0.1), 0, seq(0, 1, 0.1), lwd=2) text(rep(0, 10), seq(0, 1, 0.1), seq(0, 1, 0.1), cex=0.7, pos=2)
The end result will look something like this:
opar <- par(no.readonly = TRUE) d <- data.frame(Name=LETTERS, Odds_Ratio_log2=runif(26, -8, 8), p_value=runif(26)) plot(d$Odds_Ratio_log2, d$p_value, pch=20, xlim=c(-8, 8), ylim=c(0, 1), axes=F, xlab='', ylab='', yaxs='i', col='gray20') abline(v=0, lwd=3) axis(3, lwd=3, at=seq(-8, 8, 1), cex.axis=0.8, lwd.ticks=1) mtext('Odds (Log2)', 3, line=2.5) text(d$Odds_Ratio_log2, d$p_value, d$Name, pos=4, offset=0.3, cex=0.7) par(xpd=NA) segments(-0.1, seq(0.1, 0.9, 0.1), 0, seq(0.1, 0.9, 0.1), lwd=2) text(rep(0, 10), seq(0.1, 0.9, 0.1), seq(0.1, 0.9, 0.1), cex=0.7, pos=2, offset=0.3) par(opar)
这篇关于R - 我如何绘制这样的ggplot2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!