2
$\begingroup$

I'm very new to R Programming. So please excuse for such a simple doubt. How to plots this graph in R?

I want to plot the above graph. The x & y values are sequence from 0 to 2560. I want plot a a curve on the points where x*y=10^6.

What are the line required in R Programming Language.

$\endgroup$
3
  • $\begingroup$ the legend in your graph does not correspond to the graph of function $y=10^6/x$, is this intentional? $\endgroup$
    – mpiktas
    Jan 25, 2011 at 14:15
  • $\begingroup$ Thanks @mpiktas. My mistake. And my intention is not y=10^6/x. I want to make a graph with x & y values and the mark the points(curve) where the values of x & y satisfies (x*y=10^6). Is it possible? Not sure if this is mathematical correct or not. $\endgroup$
    – Saneef
    Jan 25, 2011 at 14:54
  • $\begingroup$ For reference: en.wikipedia.org/wiki/File:Vector_Video_Standards2.svg I want to plot the line which acts as the boundary for yellow on the lower right (>1M pixels) $\endgroup$
    – Saneef
    Jan 25, 2011 at 15:02

2 Answers 2

7
$\begingroup$

I think all you need is:

 curve(1e6/x,0,2560)

EDIT in light of comments:
Or perhaps:

plot(...<your data>...)
curve(1e6/x, 1e6/2560,2560, add=TRUE)
$\endgroup$
7
  • $\begingroup$ This solution is a lot nicer than my "hackish" try! $\endgroup$
    – daroczig
    Jan 25, 2011 at 14:13
  • $\begingroup$ +1, did not know about curve :) Always used plot directly. $\endgroup$
    – mpiktas
    Jan 25, 2011 at 14:13
  • $\begingroup$ For completeness, perhaps add text(2000, 30000, expression(paste("where xy=", 10^6)))? $\endgroup$ Jan 25, 2011 at 14:22
  • $\begingroup$ @mpiktas: Good point ;) $\endgroup$ Jan 25, 2011 at 14:38
  • $\begingroup$ But I want to make graph with x & y values and the mark the points(curve) where the values of x & y satisfies (x*y=10^6) $\endgroup$
    – Saneef
    Jan 25, 2011 at 14:56
0
$\begingroup$

The easiest way is to compute all y values at every given x values, like:

df <- data.frame(x=1:2560)
df$y <- 10^6/df$x
# the latter equivalent to:
# df <- within(df,y<-10^6/x)

And after plot the dataframe:

plot(df, type="l", main=expression(f(x) == frac(10^{6},x)))

enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ why use loop? df <- within(df,y<-10^6/x) is much nicer and more efficient. Also OP asks for line, so you need to supply type="l" in your plot function. $\endgroup$
    – mpiktas
    Jan 25, 2011 at 14:17
  • $\begingroup$ @mpiktas: true! It is terrible, that I fall into a habbit always using loops :( I edit my answer based on your suggestions. $\endgroup$
    – daroczig
    Jan 25, 2011 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.