[英]R - how can I plot such ggplot2
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. 我想制作一个类似于图B或C的图形。
To have p-values on y-axis, odds ratio on x axis (divided by such vertical line) and Names next to plotted symbol. 要在y轴上具有p值,请在x轴上用比值比(由此类垂直线除以),并在绘图符号旁边添加名称。 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: 我真的希望有人会帮助我,因为我的R绘图知识使我只能像这样进行绘图:
plot(data$V2,data$V3, main="Ratio", xlab="p-value",ylab="Odds ratio (Log2)")
.
。
You should refer to ?plot
, ?abline
, and ?text
. 您应该参考
?plot
, ?abline
和?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 the plot
call above, because unless we do this, the top axis intercepts slightly higher than y=1, perhaps giving an impression of lower p-values. 我在上面的
plot
调用中添加了yaxs='i'
,因为除非我们这样做,否则上轴的截距略高于y = 1,也许会给人以较低的p值的印象。
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
). 相反,我们以axis
axes=F
抑制轴1和2(默认的x和y轴),而是绘制轴3(顶部轴;请参见?axis
)。 We then use mtext
to plot the label for axis 3. To plot a label at the left edge of the plot, you can either use the ylab
argument (which I've set equal to ''
) in the plot
function, or use mtext('p-value', 2)
. 然后,我们使用
mtext
绘制的轴3.标签,在情节的左边缘绘制一个标签,你可以使用ylab
参数(我已经设置为''
)中plot
功能,或使用mtext('p-value', 2)
。
Alternatively, if you want the central vertical line to have ticks and labels corresponding to y-values, . 或者,如果您希望中心垂直线具有与y值相对应的刻度和标签,则。 Then, use
segments
to add tick marks, and text
to add tick labels, eg: 然后,使用
segments
添加刻度线,并使用text
添加刻度线标签,例如:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.