R 折线图上的两个数据集,但使用相同的 X 和 Y 轴? [英] Two data sets on R line graph, but using same X and Y axis?

查看:61
本文介绍了R 折线图上的两个数据集,但使用相同的 X 和 Y 轴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 R 中的图表上绘制两条线.数据与死行有关,CSV 包含三列:第一列是年份,第二列是死行人口,第三列是死亡人数当年处决.

I'm trying to plot two lines on a graph in R. The data is related to death row, with the CSV having three columns: first column is the year, second is death row population and third column is the number of executions that year.

我已经到了可以用相同的 X 轴绘制两条线的地步,但是由于值的范围相互重叠,Y 被弄乱了.

I've gotten to the point where I can draw two lines with the X-axis the same, but the Y is messed up as the range of values overlap each other.

举个例子,每一年都是这样的:

As an example, each given year is like this:

...
Year: 1968 Population: 1244 Executions: 34
Year: 1969 Population: 1456 Executions: 11
...

注意人口和处决之间的巨大差异.

Note the big difference between population and executions.

我一直在运行这个:

deathrow <- read.csv("death_row_by_year.csv", sep=",", header=T)
plot(deathrow$Year, deathrow$Population, type="l", col="red")
par(new=T)
plot(deathrow$Year, deathrow$Executions, type="l", col="green")

无论如何,我可以使用绘制人口的 Y 轴绘制执行数字?

Anyway I can plot the execution numbers using the Y axis from plotting population?

推荐答案

我会这样做:

deathrow <- read.csv("death_row_by_year.csv", sep=",", header=T)
plot(range(deathrow$Year, na.rm=T), range(c(deathrow$Population, deathrow$Executions)), na.rm=T), type='n')
lines(deathrow$Year, deathrow$Population, col="red")
lines(deathrow$Year, deathrow$Executions, col="green")

这里最重要的部分是使用 type='n' 调用 plot,所以没有绘制点,但轴设置到正确的范围.可以说,您也可以在对 linesxlim=range(...)ylim=range(...) 指定这些限制代码>.

The most important piece here is the call to plot with type='n', so there is no point plotted but the axis are set up to the right limits. Arguably, you may also specify these limits with xlim=range(...) and ylim=range(...) in the calls to lines.

这篇关于R 折线图上的两个数据集,但使用相同的 X 和 Y 轴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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