How to build two diagrams with the same x axis in R?

How do I plot 2 charts using the same x axis in R with ggplot2?

I'm looking for something like: http://i.stack.imgur.com/B9QT7.png

+3
source share
4 answers

The basic idea is to melt the data set so that you have the values ​​of the variables that you want to plot on the y axis in the same column, with a second column distinguishing the source code. For instance:

data("economics")
dat.m <- melt(economics, measure.vars=c("pop", "unemploy"))

Then use facet_grid to build each variable in a separate face:

ggplot(dat.m, aes(x=date, y=value)) + geom_line() + facet_grid(variable~., scales="free_y")
+2
source

Using the economic dataset others have mentioned about a good baseR solution can be.

layout(matrix(1:2, ncol = 1), widths = 1, heights = c(2,1.5), respect = FALSE)
par(mar = c(0, 4.1, 4.1, 2.1))
with(economics, plot(unemploy~date, type = 'l', xaxt = 'n', main = 'My Great Graph'))
par(mar = c(4.1, 4.1, 0, 2.1))
with(economics, plot(pop~date, type = 'l'))

my great graph

, , , , , . , ... , . , , , .

( , ggplot2... , ... , - - , )

+6

yahoo , . , googleVis. -, . :

install.packages("googleVis");library(googleVis);demo(googleVis)

8- - , -. ggplot.

+1

An outline of Ista's strategy using ggplot2can be achieved using the package lattice. Using the same data:

data("economics")
dat.m <- melt(economics, measure.vars=c("pop", "unemploy"))

Then we use xyplot, pointing two rows and one column through layoutand forcing the individual scales of the Y axis to use the argument scales:

xyplot(value~date|variable,data = dat.m, 
    panel = "panel.lines", layout = c(1,2),
    scales = list(y = list(relation = "free")))

enter image description here

0
source

Source: https://habr.com/ru/post/1793085/


All Articles