Issues plotting in R

Hi everyone,

For some reason, I was able to create the barplot but not anymore using the following code:

abalone <- read_csv("abalone.csv")

abalone %>%
group_by(Sex)
summarize(Rings_freq = mean(as.factor(Rings)) %>%
ggplot(mapping = aes(x = Sex, y = Rings_freq)) +
geom_col()

It should be very straightforward but it is not working. This happened when I started playing with scriptf to define 2 decimal places or digits=4 or 5. Also, I wanted to use arrange(desc(Rings_freq)) on the plot but it did not work. Any suggestions?

This is the question:

Use a bar plot to showthe number of abalone observed for each age (ring number). What appears to be the most prevalent number of rings?

Note:Remember to convert the integer-valued feature Rings to a factor using the as.factor()function prior to plotting to explicitly tell R that each integer value for the feature Rings is a separate category.

Hi @yangarita2017, could you apply the dput() function to abalone and paste the output here between a pair of triple backticks, like this?

```
[ paste output of dput(abalone) here]
```

That would help folks understand what you're trying to do.

1 Like

Hi, and welcome!

Please see the FAQ: What's a reproducible example (`reprex`) and how do I do one? Using a reprex, complete with representative data will attract quicker and more answers. Also, please see the homework policy.

See if this gets you back on track

suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(ggplot2)) 
iris %>% 
  group_by(Species) %>% 
  summarize(Sepal.Length_freq = mean(Sepal.Length)) %>% ggplot(aes(Species,Sepal.Length_freq)) + geom_col()

Created on 2020-03-08 by the reprex package (v0.3.0)

Do you want me to paste the output from the console? there are 4177 observations and 9 variables. It seems chaotic so I was reading about how to use reprex package but when I try to save a copy it says:

abalone
#> Error in eval(expr, envir, enclos): object 'abalone' not found

Hi, welcome!

Try to follow this guide to make a reprex

Hi,

I will read the policy. I am just trying to understand why I was able to see the plot and now I cannot see it even using the RStudio software downloaded on my computer.

This code is very similar and attempted it but I cannot still see the plot.

abalone <- read_csv("abalone.csv")

abalone %>%
group_by(Sex)
summarize(Rings_freq = mean(Rings)) %>%
ggplot(mapping= aes(x=Sex,y=as.factor(Rings_freq)) +
geom_col()

reprex() Error in reprex() : could not find function "reprex"

install.packages("reprex")
Error in install.packages : Updating loaded packages

Restarting R session...

install.packages("reprex")
Installing package into ‘/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.6’
(as ‘lib’ is unspecified)
trying URL 'http://package-proxy/src/contrib/reprex_0.3.0.tar.gz'
Content type 'application/x-tar' length 426210 bytes (416 KB)
==================================================
downloaded 416 KB

  • installing binary package ‘reprex’ ...
  • DONE (reprex)

The downloaded source packages are in
‘/tmp/RtmpwWt5po/downloaded_packages’

reprex()
Error in reprex() : could not find function "reprex

You have to load the package first with

library(reprex)

Also, we don't have access to your csv file so we can't reproduce your code, please read the part of the reprex guide that explains how to share sample data on a copy/paste friendly format.

Would this work?

dput(head(abalone, 9)[c("Sex", "Rings")])
structure(list(Sex = c("M", "M", "F", "M", "I", "I", "F", "F",
"M"), Rings = c(15, 7, 9, 10, 7, 8, 20, 16, 9)), row.names = c(NA,
9L), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"))

would this work?

dput(head(abalone, 9)[c("Sex", "Rings")])
structure(list(Sex = c("M", "M", "F", "M", "I", "I", "F", "F",
"M"), Rings = c(15, 7, 9, 10, 7, 8, 20, 16, 9)), row.names = c(NA,
9L), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"))

Is this what you want to do? you were missing some parentheses and pipe operators

library(tidyverse)

abalone <- structure(list(Sex = c("M", "M", "F", "M", "I", "I", "F", "F",
                                  "M"), Rings = c(15, 7, 9, 10, 7, 8, 20, 16, 9)), row.names = c(NA,
                                                                                                 9L), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"))
abalone %>%
    group_by(Sex) %>% 
    summarize(Rings_freq = mean(Rings)) %>%
    ggplot(mapping = aes(x = reorder(Sex, Rings_freq), y = Rings_freq)) +
    geom_col() +
    labs(x = "Sex")

yes, it is! what happened with the as.factor() function to convert the numerical variables Rings into categorical?

I think there is something wrong with RStudio cloud from my end because even with this code I cannot see the plot under 'Plots'

Thank you so much!

You can try restarting your R session with Ctrl+Shift+F10 and run the code again..

Also, I think you are supposed to do this plot instead.

library(tidyverse)

abalone <- structure(list(Sex = c("M", "M", "F", "M", "I", "I", "F", "F",
                                  "M"), Rings = c(15, 7, 9, 10, 7, 8, 20, 16, 9)), row.names = c(NA,
                                                                                                 9L), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"))
abalone %>%
    count(Rings) %>% 
    ggplot(mapping = aes(x = as.factor(Rings), y = n)) +
    geom_col()

I think you are correct. My apologies for the confusion.

#Q3
#Library used
library(tidyverse)

#Code to create a bar plot.
abalone <- read_csv("abalone.csv")

#Goal:Create a new variable called adult to
#distinguish between adult and infant abalone.
#Then, plot to visualize the relative proportions
#of adult/infant using a stacked bar plot

abalone %>%
mutate(adult = (Sex == 'M') | (Sex == 'F')) %>%
group_by(Sex) %>%
summarize(prop_adult = mean(adult)) %>%
count(Rings) %>%
ggplot(mapping = aes(x = as.factor(Rings), y = prop_adult)) +
barplot(count(Rings))

#Error
Error: object 'Sex' not found

#Dont know what is wrong

#Q4

#Goal: Create a new categorical feature called adult (non-infant) to distinguish between adult and infant abalone. Use box plots with appropriate notches to examine whether the median numbers of rings are different between adult and infant abalone.

abalone %>%
mutate(adult = (Sex == 'M') | (Sex == 'F')) %>%
group_by(Sex) %>%
summarize(prop_adult = mean(adult)) %>%
count(Rings) %>%
ggplot(mapping = aes(x = as.factor(Rings), y = prop_adult)) +
geom_boxplot(notch = TRUE)

#Error
Error: object 'Sex' not found

#Q5

#Goal: Considering only adult (non-infant) abalone, use box plots with appropriate notches to examine whether the median numbers of rings
#are different between male and female abalone

abalone %>%
mutate(adult = (Sex == 'M') | (Sex == 'F') & (Sex != 'I')) %>%
group_by(Sex) %>%
summarize(prop_adult = mean(adult)) %>%
count(Rings) %>%
ggplot(mapping = aes(x = as.factor(Rings), y = prop_adult)) +
geom_boxplot(notch = TRUE)

#Error
Error: object 'Sex' not found

#Q6

#Goal: Considering only adult abalone, use box plots with appropriate
#notches to examine whether the median proportion of an abalone’s
#weight being meat is different between male and female abalone. The proportion of an abalone’s weight being meat is
#the abalone’s meat weight divided by its entire weight

abalone %>%
mutate(female = (Sex == 'F'), male = (Sex == 'M'))) %>%
group_by(WholeWeight,ShellWeight) %>%
summarize(prop_weight_F = median(female = ShuckedWeight/WholeWeight)) %>% #No idea
count(ShuckedWeight/WholeWeight) %>%
ggplot(mapping = aes(x = as.factor(Rings), y = prop_adult)) +
geom_boxplot(notch = TRUE)

Please have a look at our homework policy, homework inspired questions are welcome but they should not include verbatim instructions from your course.

Oh, my apologies for this! Thank you for pointing it out.

Hi there! It's strange because I have exactly the same setup for the code as you do yet for me it mentions that my code has an error. I'm using the "abalone.csv" file. I'm so confused though, can anyone help?

abalone %>%

  • mutate(adult = (Sex == 'M') | (Sex == 'F')) %>%
    
  • group_by(Sex) %>%
    
  • summarize(prop_adult = mean(adult)) %>%
    
  • count(Rings) %>%
    
  • ggplot(mapping = aes(x = as.factor("Rings"), y = prop_adult)) +
    
  • geom_boxplot(notch = TRUE)
    

Error: Column Rings is unknown
I'm so lost