更改以R为基准的折线图中特定数据点的符号 [英] Change the symbol of specific data points in line plot, base R

查看:45
本文介绍了更改以R为基准的折线图中特定数据点的符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改基本R中折线图上特定数据点的符号.

I want to change the symbol of particular data points on a line graph in base R.

这是我的情节的一个例子:

Here is an example of my plot:

month<-c("2010-08-01", "2010-09-01", "2010-10-01", "2010-12-01", "2011-01-01", "2011-02-01",
     "2011-03-01", "2011-04-01", "2011-05-01", "2011-06-01", "2011-07-01", "2011-09-01",
     "2011-11-01", "2012-01-01", "2012-02-01", "2012-03-01", "2012-05-01", "2012-07-01",
     "2012-08-01")
prevalence<-c(10,7.5,5.2,3.5,6.4,2.7,5.8,13.2,4.3,4.7,6.4,4.4,5.2,3.3,1.0,3.1,9.9,33.3,1.0)
df<-data.frame(month, prevalence)
df$month<-as.Date(df$month)
plot(df$month, df$prevalence,lwd = 1.8, ylim=c(0,40),pch=16, bty='n', 
 ylab="Prevalence (%)", xlab="Month",col='black',cex=1,cex.lab=1.0,cex.axis=1.0)
len = .07
axis(side = 1, at = df$month, labels=F, tck=-0.015)
axis(side=2, at=c(0,10,20,30,40,50), labels=c("", "", "", "", "", ""), tck=-0.015)
lines(df$month, df$prevalence, col='black', lwd=1.8)      

生成的图形:

倒数第二个点的y值比其他点高得多,我想将点更改为空心圆,以指示该数据点只有3个样本.由于我在一个绘图中有多个折线图要覆盖,因此尝试以另一种方式指出这种现象似乎很麻烦,例如在图形上手动覆盖符号.

The y-value for the second last point is much higher than the others, and I would like to change the point to an open circle to indicate that there were only 3 samples for this data point. As I have a several of these line graphs to overlay in the one plot, it would look to messy to try to indicate this another way like manually overlaying a symbol on the graph.

推荐答案

您可以为每个数据点指定一个单独的符号.为plot的pch-option提供一个数组而不是一个值

You can specify a separate symbol for every data point. Just provide an array instead of a single value for the pch-option of plot

symbol <- rep(16,nrow(df))
symbol[df$prevalence >30] <- 21
plot(df$month, df$prevalence,lwd = 1.8, ylim=c(0,40),pch=symbol, bty='n', 
 ylab="Prevalence (%)", xlab="Month",col='black',cex=1,cex.lab=1.0,cex.axis=1.0)

这篇关于更改以R为基准的折线图中特定数据点的符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆