0

I'm running the function

ggplot(datfr, aes(x = dat1[1:951,], y = dat2[1:951,])) + 
geom_point()

and getting the error

Don't know how to automatically pick scale for object of type data.frame. 
Defaulting to continuous.
Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous.
Error: Aesthetics must be either length 1 or the same as the data (951): x, y

The last error I thought only came up when the x and y values of data were different sizes, but setting the filters [1:951,] should fix that, and when I use nrow on the two datasets it returns the same number of rows. What's happening here?

jazaniac
  • 145
  • 1
  • 12
  • 1
    What's in `dat1` and `dat2` exactly? How many columns do those data.frames have? And how does that relate to `datfr`? It would be easier to help with a proper [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). This looks like a very unusual use of `ggplot`. – MrFlick Nov 21 '17 at 22:33

2 Answers2

1

I'm assuming that you meant to type (no comma after 951):

ggplot(datfr, aes(x = dat1[1:951], y = dat2[1:951])) + 
geom_point()

Otherwise, you get an error about the incorrect number of dimensions.

With the edited code, I can reproduce the error about length 1 or same length of data. I'm not really sure about why you get that error, but a solution for this example is to move where you do your subsetting, e.g.,

ggplot(datfr[1:951,], aes(x = dat1, y = dat2)) + 
geom_point()
0

I have been able to reproduce the full error as above using a reproducible dataset:

mpg_data <- as.data.frame(mpg)

ggplot(mpg_data, aes(x = mpg_data[1:10,], y = mpg_data[1:10,])) + 
  geom_point()

Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous.
Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous.
Error: Aesthetics must be either length 1 or the same as the data (234): x, y

It appears you are providing a dataframe to the aesthetics of ggplot. As explained. The arguments within the aes function should refer to columns within the dataframe provided in the data argument.

Assuming you are trying to subset the dataset before plotting, it is best either doing that before you build the ggplot or within the data argument of the function:

ggplot(data = mpg_data[1:10,], aes(x = cty, y = cyl)) + 
  geom_point()

I would recommend reading up about using ggplot. There are loads of online resources which are very helpful including this: http://ggplot2.tidyverse.org/reference/

Michael Harper
  • 14,721
  • 2
  • 60
  • 84