I believe the easiest way to get something like the provided graph is to do
a line plot based on cut/factor frequency, for example:

x = rnorm(500);
x.F = cut(x,c(-Inf,-2,-1,0,1,2,Inf))
plot(table(x.F),type="l")

There are of course many variations on how you can set this up, but if you
are familiar with regular plot parameters, it shouldn't be hard. I don't
know how to easily convert factors of type (a,b] to "a-b" without doing
string operations, but perhaps someone else can toss in a more elegant
solution (I don't for a moment doubt one exists, I just don't do plots like
this often so I've never looked at it): with that qualification, here's
mine:

x = rnorm(500,mean=1);
x.F = cut(x,c(-Inf,-1,0,1,2,3,Inf))

layout(t(1:2))
plot(table(x.F),type="l")

# To get x-axis labels like the graph

# convert to strings and get them in the form a,b
x.F.String = substr(names(table(x.F)),2,nchar(names(table(x.F)))-1)

# Convert to a-b and handle infinities
x.F.String = sub(","," - ",x.F.String)
x.F.String = sub("-Inf - "," < ",x.F.String)
x.F.String = sub(" -  Inf"," < ",x.F.String)

# Plot
plot(table(x.F),type="l",xaxt="n")
axis(1,at=1:length(levels(x.F)),label=names(table(x.F.String)))

Hope this helps,

Michael Weylandt

On Sun, Aug 14, 2011 at 9:45 AM, Mark D. <d.mar...@ymail.com> wrote:

> Thanks very much. The cut function is exactly what I was looking for. For
> the graph I forgot to include an example (picture attached). I think it is
> something different from what you have shown in the examples. I want to plot
> all the data in a line plot - exactly how it is shown in the attached graph.
>
>
> Mark
>
>
>
>
> ________________________________
> From: R. Michael Weylandt <michael.weyla...@gmail.com>
> To: Mark D. <d.mar...@ymail.com>
> Cc: "r-help@r-project.org" <r-help@r-project.org>
> Sent: Saturday, 13 August 2011, 21:39
> Subject: Re: [R] Plotting and quantiles
>
>
> I believe you received an informative answer to both these questions from
> Daniel Maiter one hour and twenty five minutes after sending your question:
> I repeat it here just in case you didn't get it.
>
> --------------------------
>
> Q1 is very opaque because you are not even saying what kind of plot you
> want.
> For a regular scatterplot, you have multiple options.
>
> a.) select only the data in the given intervals and plot the data
>
> b.) plot the entire data, but restrict the graph region to the intervals
> you
> are interested in, or
>
> c.) winsorize the data (i.e., set values below the lower cutoff and above
> the upper cutoff to the cutoff values
>
> Which one you want to do depends on which one makes the most sense given
> the
> purpose of your analysis
>
> Say:
>
> x<-rnorm(100)
> y<-x+rnorm(100)
>
> Then
>
> a.) plot(y~x,data=data.frame(x,y)[
> x<2&x>-2 , ])
> #plots y against x only for xs between -2 and 2
>
> b.) plot(y~x,xlim=c(-2,2))
>
> #plots all y agains x, but restricts the plotting region to -2 to 2 on the
> x-axis
>
> c.)
>
> x<-replace(x,x>2,2)
> x<-replace(x,x<(-2),-2)
> plot(y~x)
>
> #sets all x-values below -2 and above 2 to these cutoffs
>
>
>
> Q2: look at the cut() function.
>
> ?cut
>
> HTH,
> Daniel
>
> ---------------------
>
> If you need more information, a different solution, or further
> clarification, please ask new questions.
>
> Michael Weylandt
>
>
> On Sat, Aug 13, 2011 at 10:10 AM, Mark D. <d.mar...@ymail.com> wrote:
>
> Dear R users,
> >
> >
> >This is most likely very basic question but I am new to R and would really
> appreciate some tips on those two problems.
> >
> >1) I need to plot variables from a data frame. Because of some few high
> numbers my graph is really strange looking. How could I plot a fraction of
> the samples (like 0.1 (10%), 0.2 up to for example 0.6) on x axis and values
> 'boundaries' (like any value '< 100',  '101-200' and '> 201') on the y axis?
> This needs to be a simple line plot like the one I attached for an example.
> The values would come from one column.
> >
> >
> >2) I have a data frame with values and need to subset the rows based on
> the values. I wanted to order them (with increasing values) and divide into
> 3-4 groups. I though about using quantile but I want the group to be
> something like '1-25', '26-50', '51-75', '75-100' (ordered and for example
> 25th percentile, 26-50th etc). I could just look for a median divide into
> two and then again (or use quantiles 0.25, 0.5, 0.7 and 1 and then get rid
> of all rows in 0.25 that are in 0.5 etc) but surely there must by a faster
> and simpler way to do that (I need to do this a lot on different columns)?
> >
> >Thanks for your help,
> >Mark
> >______________________________________________
> >R-help@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.
> >
> >
>
> ______________________________________________
> R-help@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.
>
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to