Plotting by Month (on x-axis) and trying to use "scale_x_date"

962 views
Skip to first unread message

AA

unread,
Feb 10, 2013, 7:33:05 PM2/10/13
to ggp...@googlegroups.com
I am trying to make a simple bar chart using a very simple dataset that is in this format:  (Note, first column is Project 1, Project 2, etc)
project month size
Project 1 Feb 10
Project 1 May 10
Project 2 Aug 15
Project 2 Jun 15
Project 3 Jun 5
Project 3 Oct 5
Project 4 Apr 4
Project 4 Sep 4
 
I've done a successful facetted plot where I replace the actual months with 1 for Jan, 2 for Feb, etc.   I do not know "R" but I'd like to try and learn how to do this correctly.
 
Here are my questions/problems:
1) If the data is already in a format that ggplot recognizes (Jan, Feb, etc for %b), then do you need to use "factor" to reorganize the data?  Can I put my data into a format "by hand" that ggplot will recognize?
 
2) When I try to use "+ scale_x_date(format = "%b")", I get the following error message:
Error in continuous_scale(aesthetics, "date", identity, breaks = breaks,  :
  unused argument(s) (format = "%b")
 
3) Also, when I try to plot the Jan, Feb, etc for the months, instead of 1,2, etc I also get a whole bunch of these error message:
 geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?
 

Ista Zahn

unread,
Feb 10, 2013, 8:24:50 PM2/10/13
to AA, ggp...@googlegroups.com
Hi,

On Sun, Feb 10, 2013 at 7:33 PM, AA <andrew....@gmail.com> wrote:
>
> I am trying to make a simple bar chart using a very simple dataset that is in this format: (Note, first column is Project 1, Project 2, etc)
> project month size
> Project 1 Feb 10
> Project 1 May 10
> Project 2 Aug 15
> Project 2 Jun 15
> Project 3 Jun 5
> Project 3 Oct 5
> Project 4 Apr 4
> Project 4 Sep 4
>
> I've done a successful facetted plot where I replace the actual months with 1 for Jan, 2 for Feb, etc. I do not know "R" but I'd like to try and learn how to do this correctly.
>
> Here are my questions/problems:
> 1) If the data is already in a format that ggplot recognizes (Jan, Feb, etc for %b), then do you need to use "factor" to reorganize the data? Can I put my data into a format "by hand" that ggplot will recognize?

No, I don't think so. You have to convert it, either to a factor or to a date.

>
> 2) When I try to use "+ scale_x_date(format = "%b")", I get the following error message:
> Error in continuous_scale(aesthetics, "date", identity, breaks = breaks, :
> unused argument(s) (format = "%b")
>

Maybe depends on your version of ggplot2, but the latest version has
no format argument to scale_x_date. See ?scale_x_date for examples
using the scales package to format the dates using the labels argument


> 3) Also, when I try to plot the Jan, Feb, etc for the months, instead of 1,2, etc I also get a whole bunch of these error message:
> geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?

From the help: "group is set to the interaction of all discrete
variables". See ?group for details, but in you case
geom_line(aes(group="project")) should do it.

In general I suggest converting your month string to a date, e.g.,

dat <- read.table(text="project month size
Project1 Feb 10
Project1 May 10
Project2 Aug 15
Project2 Jun 15
Project3 Jun 5
Project3 Oct 5
Project4 Apr 4
Project4 Sep 4", header=TRUE, stringsAsFactors=FALSE)

# turn month into a dat:
library(lubridate)
dat$month <- ymd(paste("2013", dat$month, "01"))

library(ggplot2)

ggplot(dat, aes(x=month, y=size, color=project)) +
geom_line() +
scale_x_datetime()


Best,
Ista
>
>
> --
> --
> 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/groups/opt_out.
>
>
Reply all
Reply to author
Forward
0 new messages