arguments imply differing number of rows

925 views
Skip to first unread message

gireesh bogu

unread,
Sep 11, 2010, 10:50:46 PM9/11/10
to ggplot2
I have 2 datasets 1 (x1) with 11956 values in a single column and
another dataset (x2) with 9227.
I'm trying to plot them as kernel density plot using ggplot2 by using
the following command. I'm getting the below error because of
difference in number of values. Could you please assist me how to
resolve this issue. I may want to plot multiple files later on. Thanx

library(ggplot2)
x1 <- read.table("C:\\Documents and Settings\\gireesh\\Desktop\\new
1.txt")
x2 <- read.table("C:\\Documents and Settings\\gireesh\\Desktop\\new
2.txt")
df <- data.frame(x1, x2)

Error in data.frame(x1, x2) :
arguments imply differing number of rows: 11956, 9227

> df.m <- melt(df)
Using as id variables
> #ggplot(df.m) + geom_density(aes(x = value,
> # colour = variable)) + labs(x = NULL) +
> # opts(legend.position = "none") + opts(title = "Kernal Densities of histone marks")
> pdf("densities_plots.pdf")
> ggplot(df.m) + geom_density(aes(x=value, y=..density.. ,colour=variable)) + labs(x=NULL) + opts(legend.position="none") + opts(title = "Densities from a kernel density estimator")
> dev.off()

Dennis Murphy

unread,
Sep 12, 2010, 7:52:39 AM9/12/10
to gireesh bogu, ggplot2
Hi:

Here are a couple of options.

# Generate data of unequal length and 'rbind' them into a data frame:
d1 <- rnorm(10000)
d2 <- rnorm(8500, 2, 3)
df <- data.frame(ds = factor(rep(c('A', 'B'), c(10000, 8500))),
                   y = c(d1, d2))

More generally, you could use rep(c('A', 'B'), c(length(d1), length(d2)))
The idea is to stack the data vectors and create a factor whose levels
correspond to the individual data vectors. It is a good idea to process
this prior to initializing the ggplot - it helps to think in advance which
legends you want in the graph and how you want them to look, and
then pre-arrange the input data frame to have those features (including
choice of variable names and factor levels).

In a density plot, the fill refers to the color to be applied to the
area under the density curve while color (or colour) applies to the
color of the density curve itself.

# Option 1: Color the curves but no specified fill color
g <- ggplot(df, aes(x = y, groups = ds, colour = ds))
g + geom_density()

# Option 2: Use fill color for each density plot, but don't color the curve.
# In addition, we thicken the density curve just because we can :)
g <- ggplot(df, aes(x = y, groups = ds, fill = ds))
g + geom_density(size = 1)

# Option 2a: To lighten the fill color, use alpha transparency:
g + geom_density(size = 1, alpha = 0.2)

# A little better, but not ideal, so use the same color for fill and curve.
# Option 3:
g <- ggplot(df, aes(x = y, groups = ds, fill = ds, colour = ds))
g + geom_density(size = 1, alpha = 0.2)

The idea readily extends to multiple input data vectors.

HTH,
Dennis


--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

gireesh bogu

unread,
Sep 12, 2010, 9:30:40 AM9/12/10
to ggplot2
Thank you very much for your detailed and useful answer and your
valuble time.
Hope R should come up with some thing that can easily read different
lengths of data.

Gireesh
> > To unsubscribe: email ggplot2+u...@googlegroups.com<ggplot2%2Bunsu...@googlegroups.com >
> > More options:http://groups.google.com/group/ggplot2

gireesh bogu

unread,
Sep 12, 2010, 9:36:39 AM9/12/10
to ggplot2
And one more small thing.
Is it possible to adjust X-axis ranges that the way I want ?

On Sep 12, 7:52 pm, Dennis Murphy <djmu...@gmail.com> wrote:

Dennis Murphy

unread,
Sep 12, 2010, 11:54:14 AM9/12/10
to gireesh bogu, ggplot2
Hi:

On Sun, Sep 12, 2010 at 6:30 AM, gireesh bogu <girees...@gmail.com> wrote:
Thank you very much for your detailed and useful answer and your
valuble time.
Hope R should come up with some thing that can easily read different
lengths of data.

There is; it's called a list. Not only may lists contain different lengths of inputs,
they may contain different types. Lists are the most general of the data objects
available in R.

Having said that, ggplot() requires a data frame as input, and data
frames have the same number of rows in each column. Can't get around
that. Part of data analysis is knowing how to manipulate data into a form that
is amenable to the functions you want to apply. R has several functions in
the base package for manipulating and summarizing data, as well as several
packages devoted to this purpose, including Hadley's plyr, reshape and now
reshape2 packages, plus data.table, doBy, Hmisc and remix. There is much
computing power available in R for I/O and various tasks associated with
processing/reducing data, but you have to take the time to learn how. No
getting around that, either.

Borrowing from a Rolling Stones lyric,
"You can't always get what you want...but if you try sometime, you'll find,
  you get what you need." 

Re your other post, for x and y limits, look into xlim() and ylim(), respectively.

HTH,
Dennis


Dennis Murphy

unread,
Sep 12, 2010, 12:07:59 PM9/12/10
to gireesh bogu, ggplot2
I wanted to see how to do this with lists instead of data frames...
sorry to follow up to my own post :)

x1 <- rnorm(10000)
x2 <- rnorm(8500, 2, 3)
l <- list(A = x1, B = x2)
m <- melt(l)
dim(m)
# [1] 18500     2
head(m, 2)     # names are different, L1 is a factor
       value L1
1 -0.9777663  A
2 -0.9674459  A

g <- ggplot(m, aes(x = value, group = L1, fill = L1, colour = L1))
g + geom_density()
g + geom_density(alpha = 0.2) + xlim(-15, 15)

As one can see, melt() works on lists, too. The list components were named
to correspond to the levels I wanted in the plot legend. The variable names
are different in the melted object (a data frame :), but that's no big deal. Notice
the use of xlim() in the second plot.

HTH,
Dennis


James Howison

unread,
Sep 12, 2010, 12:10:19 PM9/12/10
to ggplot2

On Sep 12, 2010, at 11:54, Dennis Murphy wrote:

> Borrowing from a Rolling Stones lyric,
> "You can't always get what you want...but if you try sometime, you'll find,
> you get what you need."

+1 for new ggplot2 help motto :)

--J

Rob James

unread,
Sep 12, 2010, 1:18:18 PM9/12/10
to James Howison, ggplot2
u

On 2010-09-12, at 9:10, James Howison <ja...@freelancepropaganda.com>
wrote:

gireesh bogu

unread,
Sep 12, 2010, 8:16:18 PM9/12/10
to Dennis Murphy, ggplot2
library(ggplot2)
#pdf(“densities_plots.pdf”)
d1 <- rnorm(10000)
d2 <- rnorm(8500, 2, 3)
l <- list(A = d1, B = d2)
m <- melt(l)
dim(m)
g <- ggplot(m, aes(x = value, group = L1, fill = L1, colour = L1))
g + geom_density()
g + geom_density(alpha = 0.2) + xlim(-15, 15)
#dev.off()

gireesh bogu

unread,
Sep 12, 2010, 11:01:31 PM9/12/10
to ggplot2
Hey thank you for the posts but I got errors because if I adjust X-
axis to some value it is ignoring other values. I want the frewuncy of
all the numbers with in X-axis value ranges.
Please help me

> g + geom_density(fill=NA)+ xlim (-2000,2000)
Warning messages:
1: Removed 11796 rows containing missing values (stat_density).
2: Removed 20277 rows containing missing values (stat_density).
3: Removed 9298 rows containing missing values (stat_density).
4: Removed 10014 rows containing missing values (stat_density).
> >> <ggplot2%2Bunsu...@googlegroups.com<ggplot2%252Bunsubscribe@googlegroup s.com>>

Dennis Murphy

unread,
Sep 12, 2010, 11:57:48 PM9/12/10
to gireesh bogu, ggplot2
Hi:

It would help if you posted a small, reproducible example that illustrates the problem clearly.

On Sun, Sep 12, 2010 at 8:01 PM, gireesh bogu <girees...@gmail.com> wrote:
Hey thank you for the posts but I got errors because if I adjust X-
axis to some value it is ignoring other values. I want the frewuncy of
all the numbers with in X-axis value ranges.
Please help me

> g + geom_density(fill=NA)+ xlim (-2000,2000)
Warning messages:
1: Removed 11796 rows containing missing values (stat_density).
2: Removed 20277 rows containing missing values (stat_density).
3: Removed 9298 rows containing missing values (stat_density).
4: Removed 10014 rows containing missing values (stat_density).

ggplot removes(ignores, actually) values that are outside the requested range - evidently, you have a lot of values outside of the interval (-2K, 2K), K = 10^3. This feature is documented and behaves as advertised.

What you seem to be saying is that you want the frequency/density of all responses whose corresponding x value is within the interval (-2000, 2000), but then you complain that geom_density is ignoring the other values. ggplot2 responded in accordance with your code and observe that it provided you with warnings rather than errors. To borrow from Jim Holtman on R-help, what is the problem you *really* want to solve?

HTH,
Dennis

Reply all
Reply to author
Forward
0 new messages