Plot two graphs in same plot in R – Stack Overflow bitcoin graph

When constructing multilayer plots one should consider ggplot package. The idea is to create a graphical object with basic aesthetics and enhance it incrementally.

Ggplot style requires data to be packed in data.Frame. # data generation

X <- seq(-2, 2, 0.05)

Y1 <- pnorm(x)

Y2 <- pnorm(x,1,1)

Df <- data.Frame(x,y1,y2)

Basic solution: require(ggplot2)

Ggplot(df, aes(x)) + # basic graphical object

Geom line(aes(y=y1), colour=red) + # first layer

Geom line(aes(y=y2), colour=green) # second layer

Here + operator is used to add extra layers to basic object.

With ggplot you have access to graphical object on every stage of plotting. Say, usual step-by-step setup can look like this: g <- ggplot(df, aes(x))

G <- g + geom line(aes(y=y1), colour=red)

G <- g + geom line(aes(y=y2), colour=green)

bitcoin graph


G produces the plot, and you can see it at every stage (well, after creation of at least one layer). Further enchantments of the plot are also made with created object. For example, we can add labels for axises: g <- g + ylab(Y) + xlab(X)


Final g looks like:

UPDATE (2013-11-08):

As pointed out in comments, ggplot’s philosophy suggests using data in long format.

You can refer to this answer https://stackoverflow.Com/a/19039094/1796914 in order to see corresponding code.

Tl;dr: you want to use curve (with add=TRUE) or lines.

I disagree with par(new=TRUE) because that will double-print tick-marks and axis labels. Eg

The output of plot(sin); par(new=T); plot( function(x) x**2 ).

Look how messed up the vertical axis labels are! Since the ranges are different you would need to set ylim=c(lowest point between the two functions, highest point between the two functions), which is less easy than what I’m about to show you—and way less easy if you want to add not just two curves, but many.Bitcoin graph

What always confused me about plotting is the difference between curve and lines. (if you can’t remember that these are the names of the two important plotting commands, just sing it.) here’s the big difference between curve and lines.

Curve will plot a function, like curve(sin). Lines plots points with x and y values, like: lines( x=0:10, y=sin(0:10) ).

And here’s a minor difference: curve needs to be called with add=TRUE for what you’re trying to do, while lines already assumes you’re adding to an existing plot.

Here’s the result of calling plot(0:2); curve(sin).

Behind the scenes, check out methods(plot). And check body( plot.Function )[[5]]. When you call plot(sin) R figures out that sin is a function (not y values) and uses the plot.Function method, which ends up calling curve. So curve is the tool meant to handle functions.Bitcoin graph

As described by @redmode, you may plot the two lines in the same graphical device using ggplot. However, the data in that answer was in a ‘wide’ format, whereas in ggplot it is generally most convenient to keep the data in a data frame in a ‘long’ format. Then, by using different ‘grouping variables’ in the aesthetics arguments, properties of the line, such as linetype or colour, will vary according to the grouping variable, and corresponding legends will appear. In this case we can use the colour aessthetics, which matches colour of the lines to different levels of a variable in the data set (here: y1 vs y2). But first we need to melt the data from wide to long format, using the function ‘melt’ from reshape2 package. Library(ggplot2)


# original data in a ‘wide’ format

X <- seq(-2, 2, 0.05)

bitcoin graph

Y1 <- pnorm(x)

Y2 <- pnorm(x, 1, 1)

Df <- data.Frame(x, y1, y2)

# melt the data to a long format

Df2 <- melt(data = df, id.Vars = x)

# plot, using the aesthetics argument ‘colour’

Ggplot(data = df2, aes(x = x, y = value, colour = variable)) + geom line()

Stack exchange


• technology

• life / arts

• culture / recreation

• science

• other

• stack overflow

• server fault

• super user

• web applications

• ask ubuntu

• webmasters

• game development

• tex – latex

• software engineering

• unix & linux

• ask different (apple)

• wordpress development

• geographic information systems

• electrical engineering

• android enthusiasts

• information security

• database administrators

• drupal answers

• sharepoint

• user experience

• mathematica

• salesforce

• expressionengine® answers

• stack overflow em português

• blender

bitcoin graph

• network engineering

• cryptography

• code review

• magento

• software recommendations

• signal processing

• emacs

• raspberry pi

• stack overflow на русском

• programming puzzles & code golf

• stack overflow en español

• ethereum

• data science

• arduino

• bitcoin

More (28)

• photography

• science fiction & fantasy

• graphic design

• movies & TV

• music: practice & theory

• worldbuilding

• seasoned advice (cooking)

• home improvement

• personal finance & money

• academia

• law

More (16)

• english language & usage

• skeptics

• mi yodeya (judaism)

• travel

• christianity

• english language learners

• japanese language

• arqade (gaming)

• bicycles

• role-playing games

• anime & manga

• puzzling

• motor vehicle maintenance & repair

More (32)

• mathoverflow

• mathematics

• cross validated (stats)

• theoretical computer science

bitcoin graph

• physics

• chemistry

• biology

• computer science

• philosophy

More (10)

• meta stack exchange

• stack apps


• data

• area 51