Data Frame and Hubble's Plot
We can combine vectors into a spreadsheet-like format that R calls a data.frame (copy and paste) dist = c(0.032,0.034,0.214,0.263,0.275,0.275,0.45,0.5,0.5, 0.63,0.8,0.9, 0.9,0.9,0.9,1,1.1,1.1,1.4,1.7,2,2,2,2) vel = c(170, 290, -130, -70,-185,-220, 200, 290, 270, 200, 300, -30, 650, 150, 500, 920, 450, 500, 500, 960, 500, 850, 800, 1090) hubble = data.frame(dist, vel)
Result
Plot the Hubble Data
Result of plotting Hubble Data
Note we can export the plot
Fitting the Hubble data.frame to a linear model
Summary of linear model from R and the linear fit from Excel for comparison The intercept and slope are the “coefficients”.
Using abline() to add linear model result to plot
Get help on a function
Adding the equation to the plot Text(middle_x, middle_y, text_to_display) Note that 0.5 is near the middle of the displayed equation horizontally And that 800 is near the middle of the displayed equation vertically