ggplot2 plots not showing in Plots pane

I am fairly new to R, so I'm sorry if this is an obvious or basic question. I am trying to create a scatterplot of salinity over the course of one year. I executed this code line by line, but when I run ggplot, the only thing that appears in the Plots pane is a grey rectangle where the plot would usually be. I am currently using the latest update of RStudio.

I have already restarted RStudio and used dev.off(). I'm not sure what's going on.

'''
##Reset R's brain
rm(list=ls())

#set working directory
setwd("C:/Users/jelli/OneDrive/Documents/Rprojects/Thesis")

getwd()

#----------------------------------------------------------------------##
#set up data

thesisdata <- read.csv("Jo_TidalCreeks_practice.csv")

ls()
thesisdate <- as.Date(thesisdata$date, format = "%m/%d/%Y")

#------------------------------------------
ggplot TRIAL
#-------------------------------------------
p <- ggplot(aes(thesisdate, thesisdata$salinity, color=thesisdata$stream_location) +
geom_point() +
facet_grid(rows = vars(thesisdata$water_system), cols = vars(thesisdata$session),
scales = "free") +
theme_classic())

print(p)

'''

A screenshot isn't very useful, try to give us a better dataset following this isntruction:

Second: The dataset comes outside the aes() and you don't have to use the dollar-notation but can call the columns directly, also you need to define a x and y value for geom_point()

p <- ggplot(thesisdate, 
            aes(x = date ,  # (???)
                y = salinity, color=stream_location) +
geom_point() +
facet_grid(rows = vars(water_system), cols = vars(session),
           scales = "free") +
theme_classic())

When I run this code, I get an error.

'''
Error in ggplot():
! mapping should be created with aes().
:heavy_multiplication_x: You've supplied a object
Run rlang::last_trace() to see where the error occurred.

Backtrace:

  1. ├─ggplot2::ggplot(...)
  2. └─ggplot2:::ggplot.default(...)
  3. └─cli::cli_abort(...)
  4. └─rlang::abort(...)
    

'''

What does this mean? I'm not sure where the error is.

did you add the correct column that should be plotted on x?
also I missed another error in your example, one closing parenthesis is at the wrong position.
try this:

p <- ggplot(thesisdate, 
            aes(x = date ,  # ???
                y = salinity, color=stream_location)) +
geom_point() +
facet_grid(rows = vars(water_system), cols = vars(session),
           scales = "free") +
theme_classic()

if this doesn't work, try without the facet_grid() first.

1 Like

thesisdate appears to be a vector while thesisdata is a data frame. I think that thesisdata should be the first argument in ggplot(), not thesisdate.

Rather than create a new vector, convert the existing date variable in the thesisdata data frame to the Date format

thesisdata <- read.csv("Jo_TidalCreeks_practice.csv")
thesisdata$date <- as.Date(thesisdata$date, format = "%m/%d/%Y")

This worked! Thank you so much!

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.