how to plot temperatures with ggplot2

221 views
Skip to first unread message

Jean-Michel Richer

unread,
Jun 29, 2017, 11:01:31 AM6/29/17
to ggplot2
Hi everybody,
I am trying to plot minimum, maximum and average temperatures for a very simple data file.
However I get different errors like:
- geom_path: Each group consists of only one observation. Do you need to adjust
the group aesthetic?
- Don't know how to automatically pick scale for object of type uneval. Defaulting to continuous
Error: Aesthetics must be either length 1 or the same as the data (12): x

So nothing I do is working, I am looking for some help !

Here is my input file (table)
# read file
temperatures <- read.table("temperatures.rt", header=T)
> temperatures
   month minT maxT
1    jan    6   22
2    feb    8   24
3    mar    9   26
4    apr   12   27
5    mai   13   27
6    jun   14   25
7    jul   13   24
8    aug   13   24
9    sep   13   23
10   oct   11   23
11   nov    8   22
12   dec    6   22

First I get some problem concerning the months, so I transformed as:

temperatures$month = factor(temperatures$month, levels=temperatures$month)

but then everything I do leads me to errors that I am unable to solve.

Regards,
JM

Roman Luštrik

unread,
Jun 29, 2017, 12:02:30 PM6/29/17
to Jean-Michel Richer, ggplot2
Can you provide your dataset in an easy-to-paste form? What is your code for plotting?
Where in your dataset is average temperature specified?

Cheers,
Roman

--
--
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+unsubscribe@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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



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

Roman Luštrik

unread,
Jun 29, 2017, 12:38:53 PM6/29/17
to Jean-Michel Richer, ggplot2
Please keep the list in the loop. Looking at your data, it would seem you first need to make it into the right format - long format. You can do that using tidyr::gather. Plotting from then on is trivial. Also notice that I'm not calling any column name explicitly from the object, but rather just referring to the name.

xy <- read.table("../Downloads/temperatures.rt", header = TRUE)
xy$meanT <- with(xy, (minT + maxT)/2) # with regular warnings that this implies that the distribution is symmetrical...

# this ensures that the months are plotted in the correct, specified order
xy$month <- factor(xy$month, levels = xy$month)

library(tidyr)
library(ggplot2)

# put your data into long format suitable to be used in ggplot()
xy <- gather(xy, key = temp, value = value, -month)

ggplot(xy, aes(x = month, y = value, color = temp, group = temp)) +
  theme_bw() +
  scale_color_brewer(palette = "Set1") +
  geom_line()


Cheers,
Roman
Reply all
Reply to author
Forward
0 new messages