[R] Multiple Plots using ggplot

John Kane jrkrideau at inbox.com
Tue Mar 31 16:32:00 CEST 2015


Hi Frederic,

Thanks for sending the data in dput() format. All it does in convert a data set into a standardized format (perfect copy) that anyone with R can read. People have different setups and defaults for reading data and so on and what you may read in to R as a character variable may be a factor when I read it it in and we can have some serious problems just trying to decide what the data looks like. 

I had a look at your code and it is confused. See my comments below

d <- subset(df1, select=c(Date,Start.of.Rain..i.,Start.of.Rain..ii.,Start.of.Rain..iii.)) 

d 

d2 <- melt(d , id = 'Date', variable_name = 'Start') 

# You do not have any variable in your data.frame called “Start”

# Reshape2 seems to have just ignored “variable_name = 'Start' and did the melt based on id = 'Date'. Strange, I would have expected an error but it worked !

d2 <- melt(d , id = 'Date') will give you exactly the same result.

ggplot(d2, aes(Date,value)) + geom_line(aes(colour = start),type = "h") 

Again you do not have a variable (column name) called 'start'. You have three column names (variables) in d2 These are "Date" "variable" and "value" .

ggplot(d2, aes(Date,value)) + geom_line(aes(colour = start),type = "h") 

Point one, you have no variable called start.  

Point two, what is type = “h” doing here? It is, as far as I can see not an option in geom_line for such an option. See ?geom_line for this point.

 I think you are confusing basic graphics commands ("type =")  with ggplot commands. Have a look at http://www.cookbook-r.com/Graphs/Shapes_and_line_types/ for some examples that show the differences.

Below is what I think you may be trying to do (note I use dat1 for the data.frame rather than your df1).

###==============================================
dat1  <-  structure(list(Date = structure(c(-6575, -6209, -5844, -5479,
-5114, -4748), class = "Date"), Number.of.Rain.Days = c(86L,
96L, 114L, 119L, 123L, 124L), Total.rain = c(1139.952, 977.646,
1382.014, 1323.086, 1266.444, 1235.964), Start.of.Rain..i. = c(92L,
98L, 92L, 100L, 92L, 92L), Start.of.Rain..ii. = c(239L, 98L,
92L, 100L, 92L, 92L), Start.of.Rain..iii. = c(112L, 112L, 120L,
125L, 119L, 112L), Start.Rain..iv. = c(112L, 112L, 120L, 174L,
119L, 112L), End.of.Rain.Season = c(228L, 229L, 240L, 228L, 228L,
228L)), .Names = c("Date", "Number.of.Rain.Days", "Total.rain",
"Start.of.Rain..i.", "Start.of.Rain..ii.", "Start.of.Rain..iii.",
"Start.Rain..iv.", "End.of.Rain.Season"), row.names = c(NA, 6L
), class = "data.frame")

dd <- subset(dat1, select=c(Date,Start.of.Rain..i.,Start.of.Rain..ii.,Start.of.Rain..iii.))

d2 <- melt(dd ,  id = 'Date')

ggplot(d2, aes(Date,value)) + geom_line(aes(colour = variable))

ggplot(d2, aes(Date, value)) + 
           geom_histogram(  position="dodge",  stat = "identity", aes(fill = variable))

##============================================
John Kane
Kingston ON Canada


> -----Original Message-----
> From: ntfredo at gmail.com
> Sent: Tue, 31 Mar 2015 16:55:56 +0300
> To: ssefick at gmail.com
> Subject: Re: [R] Multiple Plots using ggplot
> 
> Hi John,
> 
> Sorry for the mistake I made for providing useless data.
> Here I am interest only on Tmin and Tmax columns. I want to use the same
> approach with the previous data. I want to plot on the same graph not
> separate graph. Thanks
> 
>> dput(head(BUTemp))structure(list(Year = c(1971L, 1971L, 1971L, 1971L,
>> 1971L, 1971L
> ), Month = c(2L, 2L, 2L, 2L, 2L, 2L), Day = 1:6, Rain = c(0,
> 0, 0, 0, 0, 0), Tmax = c(24.3, 25, 25.6, 26.5, 27.8, 27.5), Tmin =
> c(13.5,
> 13.2, 12.7, 12.7, 12.2, 14)), .Names = c("Year", "Month", "Day",
> "Rain", "Tmax", "Tmin"), row.names = c(NA, 6L), class = "data.frame")
> 
> Regards,
> 
> Frederic.
> 
> 
> 
> Frederic Ntirenganya
> Maseno University,
> African Maths Initiative,
> Kenya.
> Mobile:(+254)718492836
> Email: fredo at aims.ac.za
> https://sites.google.com/a/aims.ac.za/fredo/
> 
> On Tue, Mar 31, 2015 at 4:46 PM, Frederic Ntirenganya <ntfredo at gmail.com>
> wrote:
> 
>> Hi All,
>> 
>> Thanks for the help. I want to plot some of the columns on the same
>> graph
>> not all of them. Sorry, I failed to follow the instructions. Here is the
>> output of *dput()* but I don't know how it works.
>> 
>>> dput(head(data))structure(list(Date = structure(c(-6575, -6209, -5844,
>>> -5479,
>> -5114, -4748), class = "Date"), Number.of.Rain.Days = c(86L,
>> 96L, 114L, 119L, 123L, 124L), Total.rain = c(1139.952, 977.646,
>> 1382.014, 1323.086, 1266.444, 1235.964), Start.of.Rain..i. = c(92L,
>> 98L, 92L, 100L, 92L, 92L), Start.of.Rain..ii. = c(239L, 98L,
>> 92L, 100L, 92L, 92L), Start.of.Rain..iii. = c(112L, 112L, 120L,
>> 125L, 119L, 112L), Start.Rain..iv. = c(112L, 112L, 120L, 174L,
>> 119L, 112L), End.of.Rain.Season = c(228L, 229L, 240L, 228L, 228L,
>> 228L)), .Names = c("Date", "Number.of.Rain.Days", "Total.rain",
>> "Start.of.Rain..i.", "Start.of.Rain..ii.", "Start.of.Rain..iii.",
>> "Start.Rain..iv.", "End.of.Rain.Season"), row.names = c(NA, 6L
>> ), class = "data.frame")
>> 
>>  I think I need subset function then melt. Here is the approach I used:
>> 
>> d <- subset(df1,
>> select=c(Date,Start.of.Rain..i.,Start.of.Rain..ii.,Start.of.Rain..iii.))
>> d
>> d2 <- melt(d ,  id = 'Date', variable_name = 'Start')
>> 
>> ggplot(d2, aes(Date,value)) + geom_line(aes(colour = start),type = "h")
>> 
>>  but the error is:
>> 
>> Don't know how to automatically pick scale for object of type function.
>> Defaulting to continuousError in data.frame(colour = function (x, ...)
>> :
>>   arguments imply differing number of rows: 0, 183
>> 
>> 
>> Thanks,
>> 
>> Frederic.
>> 
>> 
>> 
>> Frederic Ntirenganya
>> Maseno University,
>> African Maths Initiative,
>> Kenya.
>> Mobile:(+254)718492836
>> Email: fredo at aims.ac.za
>> https://sites.google.com/a/aims.ac.za/fredo/
>> 
>> On Tue, Mar 31, 2015 at 4:20 PM, stephen sefick <ssefick at gmail.com>
>> wrote:
>> 
>>> Your data and post is still not provided in one of the formats provided
>>> here:
>>> http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example.
>>> I am unsure of what you want to do, but I have made a reproducible
>>> example
>>> that might help.
>>> 
>>> zz <- "Date Number.of.Rain.Days Total.rain Start.of.Rain..i.
>>> Start.of.Rain..ii.   Start.of.Rain..iii.
>>>  1952-01-01                  86   1139.952                92
>>>    239                 11
>>>  1953-01-01                  96    977.646                98
>>>     98                 11
>>>  1954-01-01                 114   1382.014                92
>>>     92                 12
>>>  1955-01-01                 119   1323.086               100
>>>    100                 12
>>>  1956-01-01                 123   1266.444                92
>>>     92                 11
>>>  1957-01-01                 124   1235.964                92
>>>     92                 11"
>>> 
>>> library(reshape)
>>> library(ggplot2)
>>> 
>>> Data <- read.table(text=zz, header = TRUE)
>>> 
>>> df1 <-data.frame(Data)
>>> 
>>> df2 <- melt(df1 ,  id = c('Date', 'Number.of.Rain.Days'))
>>> 
>>> df3 <- df2[-grep("Total.rain", df2$variable),]
>>> 
>>> qplot(Date,value, data=df3) +facet_wrap(~variable)
>>> 
>>> On Tue, Mar 31, 2015 at 2:55 AM, Frederic Ntirenganya
>>> <ntfredo at gmail.com>
>>> wrote:
>>> 
>>>>  Hi All,
>>>> 
>>>> Sorry for the shape of data which was not good enough.This is how my
>>>> data look like.
>>>> 
>>>> I want to plot multiple using ggplot function from a data frame of
>>>> many columns. I want to plot only Start.of.Rain..i.,
>>>> Start.of.Rain..ii. and  Start.of.Rain..iii. and I failed to make it.
>>>> What I want is to compare Start.of.Rain..i., Start.of.Rain..ii. and
>>>> Start.of.Rain..iii. by plotting vertical line. I also need to add
>>>> points to the plot to be able to separate them. The x-axis must be
>>>> date column. Thanks!
>>>> 
>>>> Here is how the data look like and how I tried to make it.
>>>> 
>>>> 
>>>> 
>>>> Date Number.of.Rain.Days Total.rain Start.of.Rain..i.
>>>> Start.of.Rain..ii.
>>>> Start.of.Rain..iii. 1952-01-01 86 1139.952 92 239 11 1953-01-01 96
>>>> 977.646
>>>> 98 98 11 1954-01-01 114 1382.014 92 92 12 1955-01-01 119 1323.086 100
>>>> 100
>>>> 12 1956-01-01 123 1266.444 92 92 11 1957-01-01 124 1235.964 92 92 11
>>>> 
>>>> 
>>>> Here is how I tried to solve the problem.
>>>> 
>>>> df1 <-data.frame(data)
>>>> df1
>>>> df2 <- melt(df1 ,  id = 'Date', variable_name = 'start of Rains')
>>>> df2
>>>> 
>>>> ggplot(df2, aes(Date,value)) + geom_line(aes(colour ="red"),type =
>>>> "h")
>>>> 
>>>> Kindly any help is welcome. Thanks
>>>> 
>>>> Regards,
>>>> Frederic.
>>>> 
>>>> Frederic Ntirenganya
>>>> Maseno University,
>>>> African Maths Initiative,
>>>> Kenya.
>>>> Mobile:(+254)718492836
>>>> Email: fredo at aims.ac.za
>>>> https://sites.google.com/a/aims.ac.za/fredo/
>>>> 
>>>> On Tue, Mar 31, 2015 at 9:24 AM, Jeff Newmiller <
>>>> jdnewmil at dcn.davis.ca.us> wrote:
>>>> 
>>>>> This is no better because (a) you are still posting using HTML
>>>>> format,
>>>>> and (b) using printed output loses the internal representation of the
>>>>> data.
>>>>> The dput function is very helpful for solving this. [1]
>>>>> 
>>>>> [1]
>>>>> http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
>>>>> 
>>>>> ---------------------------------------------------------------------------
>>>>> Jeff Newmiller                        The     .....       .....  Go
>>>>> Live...
>>>>> DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live
>>>>> Go...
>>>>>                                       Live:   OO#.. Dead: OO#..
>>>>> Playing
>>>>> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
>>>>> /Software/Embedded Controllers)               .OO#.       .OO#.
>>>>> rocks...1k
>>>>> 
>>>>> ---------------------------------------------------------------------------
>>>>> Sent from my phone. Please excuse my brevity.
>>>>> 
>>>>> On March 30, 2015 10:56:48 PM PDT, Frederic Ntirenganya <
>>>>> ntfredo at gmail.com> wrote:
>>>>> >Hi Stephen,
>>>>>> 
>>>>> >Sorry, the data came in bad way.
>>>>> >Here is the head of the data.
>>>>>> 
>>>>>>> head(data)        Date Number.of.Rain.Days Total.rain
>>>>> >Start.of.Rain..i. Start.of.Rain..ii. Start.of.Rain..iii.
>>>>> >Start.Rain..iv.
>>>>> >1 1952-01-01                  86   1139.952                92
>>>>>>      239                 112             112
>>>>> >2 1953-01-01                  96    977.646                98
>>>>>>       98                 112             112
>>>>> >3 1954-01-01                 114   1382.014                92
>>>>>>       92                 120             120
>>>>> >4 1955-01-01                 119   1323.086               100
>>>>>>      100                 125             174
>>>>> >5 1956-01-01                 123   1266.444                92
>>>>>>       92                 119             119
>>>>> >6 1957-01-01                 124   1235.964                92
>>>>>>       92                 112             112
>>>>>> 
>>>>>> 
>>>>>> 
>>>>> >Frederic Ntirenganya
>>>>> >Maseno University,
>>>>> >African Maths Initiative,
>>>>> >Kenya.
>>>>> >Mobile:(+254)718492836
>>>>> >Email: fredo at aims.ac.za
>>>>> >https://sites.google.com/a/aims.ac.za/fredo/
>>>>>> 
>>>>> >On Mon, Mar 30, 2015 at 5:34 PM, stephen sefick <ssefick at gmail.com>
>>>>> >wrote:
>>>>>> 
>>>>>>> Hi Frederic,
>>>>>>> 
>>>>>>> Can you provide a minimal reproducible example including either
>>>>>>> real
>>>>> >data
>>>>>>> (dput), or simulated data that mimics your situation? This will
>>>>>>> allow
>>>>> >more
>>>>>>> people to help.
>>>>>>> 
>>>>>>> Stephen
>>>>>>> 
>>>>>>> On Mon, Mar 30, 2015 at 8:39 AM, Frederic Ntirenganya
>>>>> ><ntfredo at gmail.com>
>>>>>>> wrote:
>>>>>>> 
>>>>>>>> Dear All,
>>>>>>>> 
>>>>>>>> I want to plot multiple using ggplot function from a data frame of
>>>>>>>> many columns. I want to plot only str1, str2 and str3 and I failed
>>>>> >to
>>>>>>>> make it. What I want is to compare str1, str2 and str3 by plotting
>>>>>>>> vertical line. I also need to add points to the plot to be able to
>>>>>>>> separate them.
>>>>>>>> 
>>>>>>>> 
>>>>>>>> Here is how the data look like and how I tried to make it.
>>>>>>>> 
>>>>>>>> Date NumberofRaindays TotalRains str1 str2 str3 1/1/1952 86 1360.5
>>>>> >92 120
>>>>>>>> 112 1/1/1953 96 1100 98 100 110
>>>>>>>> ...                                           ....
>>>>>>>> ....             ...              ....            ....
>>>>>>>> 
>>>>>>>> df1 <-data.frame(data)
>>>>>>>> df1
>>>>>>>> df2 <- melt(df1 ,  id = 'Date', variable_name = 'start of Rains')
>>>>>>>> df2
>>>>>>>> 
>>>>>>>> ggplot(df2, aes(Date,value)) + geom_line(aes(colour ="red"),type =
>>>>> >"h")
>>>>>>>> 
>>>>>>>> Kindly any help is welcome. Thanks
>>>>>>>> 
>>>>>>>> Regards,
>>>>>>>> Frederic.
>>>>>>>> 
>>>>>>>> Frederic Ntirenganya
>>>>>>>> Maseno University,
>>>>>>>> African Maths Initiative,
>>>>>>>> Kenya.
>>>>>>>> Mobile:(+254)718492836
>>>>>>>> Email: fredo at aims.ac.za
>>>>>>>> https://sites.google.com/a/aims.ac.za/fredo/
>>>>>>>> 
>>>>>>>>         [[alternative HTML version deleted]]
>>>>>>>> 
>>>>>>>> ______________________________________________
>>>>>>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>>>>> PLEASE do read the posting guide
>>>>>>>> http://www.R-project.org/posting-guide.html
>>>>>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>>>>> 
>>>>>>> 
>>>>>>> 
>>>>>>> 
>>>>>>> --
>>>>>>> Stephen Sefick
>>>>>>> **************************************************
>>>>>>> Auburn University
>>>>>>> Biological Sciences
>>>>>>> 331 Funchess Hall
>>>>>>> Auburn, Alabama
>>>>>>> 36849
>>>>>>> **************************************************
>>>>>>> sas0025 at auburn.edu
>>>>>>> http://www.auburn.edu/~sas0025
>>>>>>> **************************************************
>>>>>>> 
>>>>>>> Let's not spend our time and resources thinking about things that
>>>>>>> are
>>>>> >so
>>>>>>> little or so large that all they really do for us is puff us up and
>>>>> >make us
>>>>>>> feel like gods.  We are mammals, and have not exhausted the
>>>>>>> annoying
>>>>> >little
>>>>>>> problems of being mammals.
>>>>>>> 
>>>>>>>                                 -K. Mullis
>>>>>>> 
>>>>>>> "A big computer, a complex algorithm and a long time does not equal
>>>>>>> science."
>>>>>>> 
>>>>>>>                               -Robert Gentleman
>>>>>>> 
>>>>>>> 
>>>>>> 
>>>>>>       [[alternative HTML version deleted]]
>>>>>> 
>>>>> >______________________________________________
>>>>> >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>>> >https://stat.ethz.ch/mailman/listinfo/r-help
>>>>> >PLEASE do read the posting guide
>>>>> >http://www.R-project.org/posting-guide.html
>>>>> >and provide commented, minimal, self-contained, reproducible code.
>>>>> 
>>>>> 
>>>> 
>>> 
>>> 
>>> --
>>> Stephen Sefick
>>> **************************************************
>>> Auburn University
>>> Biological Sciences
>>> 331 Funchess Hall
>>> Auburn, Alabama
>>> 36849
>>> **************************************************
>>> sas0025 at auburn.edu
>>> http://www.auburn.edu/~sas0025
>>> **************************************************
>>> 
>>> Let's not spend our time and resources thinking about things that are
>>> so
>>> little or so large that all they really do for us is puff us up and
>>> make us
>>> feel like gods.  We are mammals, and have not exhausted the annoying
>>> little
>>> problems of being mammals.
>>> 
>>>                                 -K. Mullis
>>> 
>>> "A big computer, a complex algorithm and a long time does not equal
>>> science."
>>> 
>>>                               -Robert Gentleman
>>> 
>>> 
>> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

____________________________________________________________
FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family!
Visit http://www.inbox.com/photosharing to find out more!



More information about the R-help mailing list