I'm having this weird glitch with ggplot where my 913 and 995 values are being placed all the way to the right of the scatterplot. I have no clue why it's doing this. My code is down below and a link to the image it projects. Really appreciate anyone's help! I'm a newbie
So, there's three main differences between this plot and yours: 1) the points are now in numerical order and so are arranged properly, 2) the axes have even spacings between labels, rather than a label for each data point (this is a telltale sign you have non-numeric format data), and 3) the geom_smooth()
line has appeared - I assume this is because R now has numbers to work with it is able to run a linear model on the data.
Hope this helps. I would highly recommend reading and working through the examples in the R for Data Science book if you want to ease the learning curve of R, and reduce your stress when starting out.
CodePudding user response:
That is because, both mass and rate columns are character in the data frame bmr, subject and rate has length 19 and gender and mass has length 16 correct these lengths if these are not correct.
to get the desired plot, you can convert rate and mass to numeric first or, you can covert these variables to numeric within the ggplot2
library(tidyverse)
subject = c(1:19)
gender = c("M","M","F","F","F","F","M","F","F","M","F","F","F","F","M","M")
mass = c(62.0, 62.9, 36.1, 54.6, 48.5, 42.0, 47.4, 50.6, 42.0, 48.7, 40.3, 34.5, 51.1, 41.2, 51.9, 46.9)
rate = c(1792, 1666,995, 1425, 1396, 1418, 1362, 1502, 1256, 1614, 1189, 913, 1460, 1124, 1052, 1347, 1204, 1867, 1439)
bmr = as.data.frame(cbind(subject, gender, mass, rate))
print(bmr)
str(bmr)
ggplot(data = bmr, aes(x = as.numeric(rate), y = as.numeric(mass)))
geom_point(size=3)
geom_smooth(method=lm, se=FALSE)