ggplot graph not generating

Hi there!

I am new to R and programming in general. I am trying to create BW vs. Amt plot and differentiate the datapoint but Sex using data collected.

However when I input the code:

ggplot(R_Graphdf, aes(x=Amt, y=BW))
ggplot(R_Graphdf, aes(x="Amt", y="BW", color="Sex")) + geom_point()

There are no datapoints on the graph (see below)


What am I doing wrong? When I use the exact same code with a given dataset, I graph comes out perfect. Should I adjust how I format my dataset?

Thank you!

Dataset that is being used for the code:

ggplot(R_Graphdf, aes(x=Amt, y=BW, color=Sex)) + geom_point()

You should unquote variable name in ggplot(aes()). ggplot2 recognize "Amt", "BW" and "Sex" as character (just a single value) instead of variables in your data frame.

When I unquote the variables it gives me an error message. Am I missing a step prior to this code, or should I restructure my dataset?

ggplot(R_Graphdf, aes(x=Amt, y=BW, color=Sex)) + geom_point()
Error in FUN(X[[i]], ...) : object 'Amt' not found

I am not sure if I did something wrong in the previous step where I converted the file into a data frame using tibble:

R_Graph <- excel_sheets("~/Documents/R_Graph.xlsx")
R_Graphdf <- tibble(R_Graph)

Thank you!

Did you look at the data.frame you created to make sure it actually contains what you think it contains? This would be the first step in trouble shooting your code.

The function excel_sheets() only lists the sheets in your Excel file. You need the function read_xlsx(). This creates a tibble so you don't need to convert it.

To see what a function does, type ?excel_sheets in the console and you will get an explanation in the bottom right pane of RStudio.

1 Like

I think the same with @GeraldineK. You can use read_excel() or read_xlsx() (if the suffix is .xlsx) to read in that excel file, you many also need the argument sheet if the data isn't on the first sheet. Then try to plot it again.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.