[R] barplot and cumulative curve using ggplot2 layers

hadley wickham h.wickham at gmail.com
Thu Dec 10 23:00:27 CET 2009


Hi Sunita,

To get the bars, you want:

ggplot(mydata, aes(x = factor(jobno), y = recruits)) + geom_bar()

and to add the cumulative sum, first add it to the data:

mydata$cum_ recruits <- cumsum(mydata$recruits)

and then add another layer:

+ geom_line(aes(y = cum_recruits, group = 1))

Hadley

On Thu, Dec 10, 2009 at 9:35 AM, Sunitap22 <sunitap22 at gmail.com> wrote:
>
> Hello
>
> My dataset is as follows:
> jobno       recruits
> 1100         18
> 1200         1
> 1850         5
> 2100         190
> 2789          25
> 3000          1
> .                .
> .                .
> the dataset has 130 rows
>
> I want to plot barplot from left side and cumulative curve from right side
> in one graph itself using layers in ggplot2.
>
> I sorted the recruits 1st in decreasing order and then used
> mydata <- data.frame(jobno, recruits)
> ggplot(mydata, aes(x = jobno, y = recruits)) + geom_bar()
>
> but I am getting an error
> "stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust
> this.
> Error in pmin(y, 0) : object "y" not found"
>
> Can someone guide me regarding this? How to get barplot from left side and
> cumulative curve from right side in one graph itself.
>
> Thanks in advance
> Regards
> Sunita
> --
> View this message in context: http://n4.nabble.com/barplot-and-cumulative-curve-using-ggplot2-layers-tp957075p957075.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
http://had.co.nz/




More information about the R-help mailing list