How to make a line graph with two variables and one x-axis in ggplot2?

73 views
Skip to first unread message

Jethro Emmanuel

unread,
Jul 29, 2015, 6:14:54 AM7/29/15
to ggplot2
How to make a line graph with two variables and one x-axis in ggplot2? I have done something, and the result is in the picture attached. I wanted to have the x-axis to have a label like Jan 2013, Mar 2013 ... etc. Also, I wanted to make it visually appealing, how?

Here's my data:

 Months Lengths Weights
1  Jan 2013   41.79    5.58
2  Mar 2013   36.77    3.89
3  Apr 2013   39.10    4.51
4  May 2013   39.66    4.21
5  Jun 2013   40.58    4.16
6  Jul 2013   37.61    3.68
7  Aug 2013   39.83    4.61
8  Sep 2013   41.39    4.62
9  Oct 2013   41.87    5.15
10 Nov 2013   40.64    4.58
11 Dec 2013   43.45    5.47
12 Jan 2014   38.17    4.00
13 Feb 2014   37.69    3.75
14 Mar 2014   39.54    4.43
15 Apr 2014   40.65    4.70
16 May 2014   43.45    5.76

LW Graph.png

Roman Luštrik

unread,
Jul 29, 2015, 6:51:16 AM7/29/15
to Jethro Emmanuel, ggplot2

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
In God we trust, all others bring data.

Dennis Murphy

unread,
Jul 30, 2015, 5:12:30 AM7/30/15
to Jethro Emmanuel, ggplot2
Hi:

Here's a way to do it with ggplot2 and the gridExtra package - I'll
let you handle the theming.

library(ggplot2)
library(gridExtra)
# library(scales)
# library(grid)

p <- ggplot(DF, aes(x = mon, y = Lengths)) +
geom_line() +
scale_x_date(labels = scales::date_format("%b\n%Y")) +
xlab(NULL)

q <- ggplot(DF, aes(x = mon, y = Weights)) +
geom_line() +
scale_x_date(labels = scales::date_format("%b\n%Y")) +
xlab(NULL)

gp <- ggplotGrob(p)
gq <- ggplotGrob(q)
maxWidth = grid::unit.pmax(gp$widths[2:5], gq$widths[2:5])
gp$widths[2:5] <- as.list(maxWidth)
gq$widths[2:5] <- as.list(maxWidth)
grid.arrange(gp, gq, ncol=1)

There's a somewhat more efficient way to do this with the gtable
package, but this works for your example. The code chunk vertically
aligns the two graphics regions, which is necessary because the y-axis
labels have different widths.

Aside: If you want the tick marks to be the same color as the grid
lines in your graph, add axis.ticks to the theme() call. It uses
element_line(); make the color of the ticks the same as those of the
grid lines.

Dennis

Jethro Emmanuel

unread,
Jul 30, 2015, 9:44:17 PM7/30/15
to ggplot2, jethroe...@gmail.com
Thank you very much for your responses. I will try it. :)

Brian

unread,
Aug 9, 2015, 11:10:51 AM8/9/15
to ggplot2
Hello list!

google led me to a snippet of code, to easily and almost generically at
equations to graphs.
I mean: https://gist.github.com/kdauria/524eade46135f6348140

Has there been any discussion on including similar functionality into
ggplot? It would be really cool! ggplot et al allow me to be lazy,
which I appreciate tremendously!

In case I was unclear: (Copied from)
http://stackoverflow.com/questions/7549694/ggplot2-adding-regression-line-equation-and-r2-on-graph

|library(devtools)source_gist("524eade46135f6348140")df =data.frame(x
=c(1:100))df$y =2+5*df$x +rnorm(100,sd =40)df$class
=rep(1:2,50)ggplot(data =df,aes(x =x,y
=y,label=y))+stat_smooth_func(geom="text",method="lm",hjust=0,parse=TRUE)+geom_smooth(method="lm",se=FALSE)+geom_point()+facet_wrap(~class)
Cheers, Brian |








Reply all
Reply to author
Forward
0 new messages