简体   繁体   English

如何从数据子集中创建基础 R 分散 Plot

[英]How to make a base R scatter Plot from a data subset

I am using R Base plotting.我正在使用 R 基础绘图。 I need to subset for two columns, whereby one where gender=Female and the other where Measure.Variables=Life Expectancy.我需要对两列进行子集化,其中一列是性别=女性,另一列是 Measure.Variables=预期寿命。 Since the Measure.Variables column has two values "Life Expectancy" and "Mortality".由于 Measure.Variables 列有两个值“预期寿命”和“死亡率”。

Moreover, I am trying to manually set the breaks and limits for the y and x axis and but I am unable to do so.此外,我正在尝试手动设置 y 和 x 轴的中断和限制,但我无法这样做。 I have attached a picture with the breaks and limits I want to add.我附上了一张我想添加的休息和限制的图片。

图形 图片

Could you please help me with this also.你能帮我解决这个问题吗? I want to set the breaks for y axis to breaks=c(30,40,50,60,70,80) and for x axis as breaks=c(1900,1920,1940,1960,1980,2000).我想将 y 轴的中断设置为 break=c(30,40,50,60,70,80),将 x 轴的中断设置为 break=c(1900,1920,1940,1960,1980,2000)。 I want these limits to appear regardless whether data is available.无论数据是否可用,我都希望出现这些限制。

I am using the following code and its giving me an error when I add the second condition in the subset statement.我正在使用以下代码,当我在子集语句中添加第二个条件时,它给了我一个错误。 Otherwise, it works fine without the Measure.Variables==Life Expectancy command.否则,它可以在没有 Measure.Variables==Life Expectancy 命令的情况下正常工作。

Following is the output of the data以下是output的数据

structure(list(Measure.Variables = c("Life Expectancy", "Life Expectancy", 
"Life Expectancy", "Mortality", "Life Expectancy", "Life Expectancy"
), Race = c("All Races", "All Races", "All Races", "All Races", 
"All Races", "All Races"), Sex = c("Both Sexes", "Both Sexes", 
"Both Sexes", "Both Sexes", "Both Sexes", "Both Sexes"), Year = 1900:1905, 
    Average.Life.Expectancy = c(47.3, 49.1, 51.5, 50.5, 47.6, 
    48.7), Mortality = c(NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_)), row.names = c(NA, 6L), class = "data.frame")

I am using the following code我正在使用以下代码

with(subset(LF, Sex == "Male", Measure.Variables == "Life Expectancy"), 
     plot(Year, Average.Life.Expectancy, col="red", pch=17,
          main="Male Life Expectancy", ylab="Life Expectancy"))

Edited in response to Adele, the y values still do not show.为响应阿黛尔而编辑,y 值仍未显示。 please look at this picture Graph after Adele's suggestion在阿黛尔的建议下看这张图

Easier to se the data= argument, and separate selection with & in subset .更容易设置data=参数,并在subset中使用&单独选择。 For axis customization, deactivate axis texts using xaxt='n' and yaxt='n' , and build your own with axis() .对于轴自定义,使用xaxt='n'yaxt='n'停用轴文本,并使用axis()构建自己的。

plot(Year ~ Average.Life.Expectancy,
     data=subset(dat, Sex == "Both Sexes" & Measure.Variables == "Life Expectancy"),
     col="red", pch=17, 
     main="Male Life Expectancy", ylab="Life Expectancy", xaxt='n', yaxt='n')
axis(1, at=c(48,50,52))
axis(2, at=c(1900, 1902, 1904))

在此处输入图像描述

Note, that I used "Both Sexes" here, since "Male" wasn't included in your sample data.请注意,我在这里使用了"Both Sexes" ,因为"Male"未包含在您的示例数据中。

Also, I used 40, 50, 52 and 1900, 1902, 1904 to demonstrate axis customizations.另外,我使用 40、50、52 和 1900、1902、1904 来演示轴自定义。

In your case I would try在你的情况下,我会尝试

axis(1, at=3:8*10)
axis(2, at=seq(1900, 2000, 20))

It will be fine with adding some arguments to your code and then using axis() :在您的代码中添加一些 arguments 然后使用axis()会很好:

with(subset(LF, Sex == "Male", Measure.Variables == "Life Expectancy"), 
     plot(LF$Year, LF$Average.Life.Expectancy, col="red", pch=17,
          main="Male Life Expectancy", ylab="Life Expectancy"
          ,xlim = c(1900, 2000), ylim = c(30, 80)
          , xaxt='n', yaxt='n'
          )
     )

axis(1, at = seq(1900, 2000, by=20), las = 2)
axis(2, at = seq(30, 80, by=10))

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

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