自定义符号的{r}符号背景

时间:2016-06-04 19:57:55

标签: r plot graphics

我有一个R图,我使用这些值作为符号。这些点也有错误条: enter image description here

显然,问题在于错误栏(我使用箭头)穿过数字而且看起来很丑陋并且难以阅读。

这是我的代码,任何想法?

x = c(45.58333, 89.83333, 114.03333,138.65000,161.50000,185.15000,191.50000)
y_mean = c(3.350000,6.450000,7.200000,7.033333,8.400000,7.083333,6.750000)
y_sd = c(0.1802776,0.1732051,0.2500000,0.2020726,0.3500000,0.2020726,0.1000000)

values = data.frame(x, y_mean, y_sd)

plot(values$x, values$y_mean, type="n")

arrows(values$x, values$y_mean - values$y_sd, 
  values$x, values$y_mean + values$y_sd, 
  length=0.05, angle=90,
  code=3, col="red")

lines(values$x, values$y_mean, type="b",
  pch=" ",
  col="red", bg="white")

text(values$x, values$y_mean, label=round(values$y_mean), col="red")

编辑: 我按照要求执行了上面显示的确切代码: enter image description here

3 个答案:

答案 0 :(得分:3)

我会玩水平对齐并添加小点来跟踪原始位置

points(values$x, values$y_mean, pch=19, col="red", cex=0.5)
text(values$x, values$y_mean, label=round(values$y_mean), col="red", adj = -0.2)

enter image description here

答案 1 :(得分:2)

一个想法是在绘制文本之前将绘制文本的绘图内容白化, 。这可以使用rect()完成。虽然你冒险使用这种方法完全排除误差线。

我们可以使用strwidth() and strheight()来获得白化矩形的合适大小。

x <- c(45.58333, 89.83333, 114.03333,138.65000,161.50000,185.15000,191.50000);
y_mean <- c(3.350000,6.450000,7.200000,7.033333,8.400000,7.083333,6.750000);
y_sd <- c(0.1802776,0.1732051,0.2500000,0.2020726,0.3500000,0.2020726,0.1000000);
xlim <- range(x);
ylim <- c(min(y_mean-y_sd),max(y_mean+y_sd));
plot(NA,xlim=xlim,ylim=ylim,xlab='x',ylab='y');
arrows(x,y_mean-y_sd,x,y_mean+y_sd,length=0.05,angle=90,code=3,col='red');
lines(x,y_mean,type='b',pch=' ',col='red',bg='white');
ls <- as.character(round(y_mean));
ex <- 0.4; ## whiteout expansion factor
lsw <- strwidth(ls); w <- lsw/2*(1+ex);
lsh <- strheight(ls); h <- lsh/2*(1+ex);
rect(x-w,y_mean-h,x+w,y_mean+h,col='white',border=NA);
text(x,y_mean,ls,col='red');

plot

答案 2 :(得分:2)

只需应用这些更改:

plot(values$x, values$y_mean, type="n", 
xlim = c(min(values$x), max(values$x) + 20), 
ylim = c(min(values$y_mean)-1, max(values$y_mean)+1))

text(values$x, values$y_mean, label=round(values$y_mean), col="blue",  pos = 3)