-1

I am fairly new to R and have been given a data set where I am given height (y axis), weight (x axis), and sex must make scatter-plots based on gender. How can I separate this data so that I have the female lengths and widths and the males lengths and widths?

So far I have been able to make a scatter-plot with both genders, but don't know how to make one for each gender separately.

Both genders scatter-plot:

plot( height ~ weight, data=x,
      main= "Height vs. Weight")
xcen
  • 652
  • 2
  • 6
  • 22
  • Please [include representative & minimal sample data](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Is `x` a `data.frame`? – Maurits Evers Dec 04 '19 at 00:02

1 Answers1

0

Use ggplot2 and dplyr

# If you don't have these packages installed
#install.packages("ggplot2")
#install.packages("dplyr")
library(ggplot2)
library(dplyr)
x %>%
ggplot() +
geom_point(aes(x = weight, y = height)) +
facet_wrap(~sex) +
labs(title = "Height vs. Weight")
Hansel Palencia
  • 1,006
  • 9
  • 17
  • 1
    If you meant `magrittr::%>%`, then (1) you have a typo, and (2) please include `magrittr` or `dplyr` in your packages. (If you meant something else, then see #2 above.) (I don't think you need it, as `ggplot(x) + ...` works just as well.) – r2evans Dec 02 '19 at 23:44
  • 2
    It's an optional thing, I personally just like using the pipe out of preference. Though you're right it's not necessary. – Hansel Palencia Dec 02 '19 at 23:46
  • 2
    I don't argue, and you still have a typo. (I often include `magrittr` when I think that it adds something by breaking out each step of a sequence of calls, but I suggest that this does not provide any value or such breakout. But that's just my opinion. This comment is mostly because your code is broken with the typo :-) – r2evans Dec 02 '19 at 23:48
  • 1
    Ahhh... I noticed the > was backwards, sorry about that my mistake. – Hansel Palencia Dec 02 '19 at 23:49
  • Every time I enter this code i'm met with these 2 errors:(1) Error in x %>% ggplot() : could not find function "%>%" (2) Error in library(ggplot2) : there is no package called ‘ggplot2’ – Pleasehelpme Dec 02 '19 at 23:51
  • try just putting the x inside of the ggplot(), as r2evans states, also you have to install both libraries. `install.packages("ggplot2")` and `install.packages("dplyr")` – Hansel Palencia Dec 02 '19 at 23:52
  • After installing the packages and running the code once more I've run into the error: Error: Mapping should be created with `aes() or `aes_()`. – Pleasehelpme Dec 03 '19 at 00:00