PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, June 28, 2022

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

 June 28, 2022     graph, plot, r     No comments   

Issue

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

Edited in response to Adele, the y values still do not show. please look at this picture Graph after Adele's suggestion


Solution

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), las = 2)
axis(2, at = seq(30, 80, by=10))


Answered By - Adele
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing