ggplot error 'data' must be data frame...

Hello,

Trying to plot a simple x y graph using ggplot2. The data I want to plot are below, which (I think??) are in an OK data frame format. I can import it OK using either read.csv or read.excel.

> print(df)
# A tibble: 7 x 5
Sample_ID   Carbon_pct  d13C Depth_cm Sample_type
  <chr>            <dbl> <dbl>    <dbl>       <dbl>
1 SUERC-84526        1.3 -28.1       28           1
2 SUERC-84527       45   -29.1       45           1
3 SUERC-86978       17.9 -27.4       28           2
4 SUERC-86979       29.1 -27.7       45           2
5 SUERC-88308       51.3 -29.1       45           3
6 SUERC-29503       14.1 -28.3       28           4
7 SUERC-29506       34.6 -27.7       53           4

I'd like to plot the d13C on the x axis and depth_cm on the y axis, with a small error too. I'd like to colour each point by their sample_type. Here is the code I used below:

ggplot(data = df, aes(x=df$d13C, y=df$Depth_cm, xmin=0.1, xmax=0.1, ymin=0, ymax=0))+
  geom_pointrange(aes(colour=df$Sample_type), shape=15, size=0.75, alpha(0.7))+
  xlim(-30,-25)+
  scale_y_continuous(trans = 'reverse')+
  labs(x='d13C', y="Depth(cm)")

I get an error message "Error: data must be a data frame, or other object coercible by fortify(), not a character vector" .

I am a bit confused because 1) first time I tried it with this same(?) code and data frame plotted just fine and 2) I've used this data frame format plenty of times with ggplot functions before and it's been OK.

Is there something wrong with the data frame? Does ggplot not like integers? I've manually created a data frame (with data.frame function) from each column as a vector but same error message pops up.

Any help would be very appreciated. Thanks

The first thing to try is to remove the df$ parts:

ggplot(data = df, aes(x=d13C, y=Depth_cm, xmin=0.1, xmax=0.1, ymin=0, ymax=0))+
  geom_pointrange(aes(colour=Sample_type), shape=15, size=0.75, alpha(0.7))+
  xlim(-30,-25)+
  scale_y_continuous(trans = 'reverse')+
  labs(x='d13C', y="Depth(cm)")

Thanks for your tip, although I get the same error message unfortunately.

That error message wasn't particularly enlightening in this case, so the problem was a little hard to see. :slightly_smiling_face:

The problem is with alpha. You have alpha(0.7) where you likely meant to write alpha = 0.7.

When I make this change to the geom_pointrange() code things appear to work.

2 Likes

Ok, looking at the documentation your parameters do not appear to match those accepted:

You should define only x, ymin and ymax in aes().

1 Like

Thanks, seems like this was the problem after all! Feeling a bit silly but thanks for your help!

Thanks, this will make my code tidier...!

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