简体   繁体   English

在R中的图上添加数据点

[英]Adding data points on a plot in r

Hi I have a simple plot of years and the number of births across those years. 嗨,我有一个简单的年份图和这些年份的出生数。 I want to create a graph/line that has the years on the x axis and the number on the y. 我想创建在x轴上具有年份并且在y上具有数字的图形/线。 My code so far is below. 到目前为止,我的代码如下。 when I run it I get the error Error in jitter(x) : 'x' must be numeric. 当我运行它时,出现错误jitter(x)中的错误:'x'必须是数字。 The plot is based on a dataframe df. 该图基于数据帧df。 Any help would be greatly appreciated. 任何帮助将不胜感激。

#plot(df$Number),(df$sex),xlab="x",ylab="y",type="l",main= "My graph")
plot(df$Year,df$Number)
x = df$Year
y = df$Number
#plot(x,y)

par(mar=c(3,3,2,1),mgp=c(2,0.7,0),las=1)
plot(x,y,type="n",xaxt="n",yaxt="n",las=1,xlab="Years",ylab="")
title("Number Of Births")
axis(1,at=pretty(x),labels=TRUE)
axis(2,at=pretty(y,n=10),las=1)
#grid()

points(jitter(x),y,col="blue",pch=20)

#points(jitter(df$Number))

See what class(x) or class(df$Year) says. 看看class(x)class(df$Year)怎么说。

Depending on the data, its easy to see Year being treated as a factor. 根据数据,很容易看出年份被视为一个因素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM