Home > Software engineering >  How to make a base R scatter Plot from a data subset
How to make a base R scatter Plot from a data subset

Time:11-30

I am using R Base plotting. I need to subset for two columns, whereby one where gender=Female and the other where Measure.Variables=Life Expectancy. Since the Measure.Variables column has two values "Life Expectancy" and "Mortality".

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. I have attached a picture with the breaks and limits I want to add.

graph picture

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). 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.

Following is the output of the data

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"))

CodePudding user response:

Easier to se the data= argument, and separate selection with & in subset. For axis customization, deactivate axis texts using xaxt='n' and yaxt='n', and build your own with 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))

enter image description here

Note, that I used "Both Sexes" here, since "Male" wasn't included in your sample data.

Also, I used 40, 50, 52 and 1900, 1902, 1904 to demonstrate axis customizations.

In your case I would try

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

CodePudding user response:

It will be fine with adding some arguments to your code and then using 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))
axis(2, at = seq(30, 80, by=10))
  • Related