1

I have a seemingly simple question that nonetheless I have not been able to solve. I would like to plot only a subset of a data.frame in ggplot and I keep getting an error. Here is my code that works (with the full data set):

ggplot(a2.25, aes(x=V1, y=V2)) + geom_point() +
  theme(plot.margin = unit(c(0,0,0,0), "lines"),
        plot.background = element_blank(),
        axis.title.y = element_blank(),
        axis.title.x = element_blank()) +
  ggtitle("a2_25")

But when I try to only plot a subset of the data via:

ggplot(a2.25, aes(x=V1[2:24], y=V2[2:24])) + geom_point() +
  theme(plot.margin = unit(c(0,0,0,0), "lines"),
        plot.background = element_blank(),
        axis.title.y = element_blank(),
        axis.title.x = element_blank()) +
  ggtitle("a2_25")

I get the following error message: "Error in data.frame(x = c(0.04, 0.08, 0.12, 0.16, 0.2, 0.24, 0.28, 0.32, : arguments imply differing number of rows: 23, 26" However, the file is composed of 26 obs. of 2 variables. When I examine the length of each column separately there are 26 observations in each.

Does anyone know what is causing this error/a simple way to overcome it? I am doing exploratory analysis on my data and have numerous files and will be converting back and forth between the full data set and subsets of it, so it would be very tedious to manually shorten the files.

Thank you!

Here is the samples data (dput):

structure(list(V1 = c(0, 0.04, 0.08, 0.12, 0.16, 0.2, 0.24, 0.28, 
0.32, 0.36, 0.4, 0.44, 0.48, 0.52, 0.56, 0.6, 0.64, 0.68, 0.72, 
0.76, 0.8, 0.84, 0.88, 0.92, 0.96, 1), V2 = c(0.9999396, 1.828642e-05, 
2.125182e-05, 1.369786e-05, 6.395666e-06, 7.471323e-07, 9.306843e-09, 
1.025577e-11, 1.225776e-15, 2.306844e-20, 1.021365e-25, 1.41806e-31, 
6.450008e-38, 7.751817e-45, 1.698149e-52, 4.40356e-61, 8.356799e-71, 
6.445585e-82, 9.108883e-95, 7.374944e-110, 5.603281e-128, 1.908444e-150, 
9.635286e-180, 1.938155e-221, 2.781784e-293, 0)), .Names = c("V1", 
"V2"), class = "data.frame", row.names = c(NA, -26L))
1
  • 1
    Subset your .data argument rather than the aes: ggplot(a2.25[2:24,], ... Commented Jul 17, 2013 at 17:41

2 Answers 2

3

If you need to subset data then it should be done with data frame a2.25 not the columns inside the aes().

ggplot(a2.25[2:24,], aes(x=V1, y=V2)) + geom_point()
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I'm new to ggplot and didn't realize I need to subset the data rather than the variables.
simple, efficient way of subsetting data, thank you! (been looking for this for a while!)
2

I am guessing a2.25 is your data set name?

Try subsetting the data instead of the individual variables.

For example, for the rows 2:24, try

ggplot(a2.25[2:24,], aes(x=V1, y=V2)) + geom_point() +
  theme(plot.margin = unit(c(0,0,0,0), "lines"),
        plot.background = element_blank(),
        axis.title.y = element_blank(),
        axis.title.x = element_blank()) +
  ggtitle("a2_25")

1 Comment

Thank you so much! I'm new to ggplot and didn't realize I need to subset the data rather than the variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.