[R] Multiple Plots using ggplot

John Kane jrkrideau at inbox.com
Tue Mar 31 15:35:05 CEST 2015


The data you supplied is still in a useless format.

Please send it to us in dput format (and don't post in html)

Here is a complete example of creating a data.frame and converting it to a useable data set that readers on R-help can use

##=============Start Example===============##
# Simple example data set in a data.frame
data1  <-  data.frame(xx = 1:20, yy = sample(letters[1:26], 20, replace = TRUE), zz  <-  rnorm(20))

dput(data1)  # convert to dput() format for tranfering to other userss
 
# dput() result. Copy and paste back into your editor
structure(list(xx = 1:20, yy = structure(c(6L, 3L, 7L, 12L, 1L, 
1L, 2L, 7L, 9L, 6L, 8L, 7L, 9L, 5L, 4L, 10L, 11L, 4L, 8L, 11L
), .Label = c("a", "f", "g", "h", "i", "j", "k", "o", "p", "u", 
"w", "z"), class = "factor"), zz....rnorm.20. = c(0.379202224643519, 
-0.293649882956148, 2.27761155645142, 0.0378126031936277, 0.518138385757923, 
1.11655160886907, -1.64262245261915, 1.11341365979718, -0.184737977758355, 
0.439361470235051, 1.2597110753159, -0.795425331570368, 0.974654694801041, 
-0.309087884123705, -1.55929705211554, 0.147715827800676, -0.542626171203849, 
0.745294589678554, -0.254290052908619, 0.939894889209173)), .Names = c("xx", 
"yy", "zz....rnorm.20."), row.names = c(NA, -20L), class = "data.frame")

#  Read data back into standard R format, calling the data "dat1"

dat1  <-  structure(list(xx = 1:20, yy = structure(c(6L, 3L, 7L, 12L, 1L, 
1L, 2L, 7L, 9L, 6L, 8L, 7L, 9L, 5L, 4L, 10L, 11L, 4L, 8L, 11L
), .Label = c("a", "f", "g", "h", "i", "j", "k", "o", "p", "u", 
"w", "z"), class = "factor"), zz....rnorm.20. = c(0.379202224643519, 
-0.293649882956148, 2.27761155645142, 0.0378126031936277, 0.518138385757923, 
1.11655160886907, -1.64262245261915, 1.11341365979718, -0.184737977758355, 
0.439361470235051, 1.2597110753159, -0.795425331570368, 0.974654694801041, 
-0.309087884123705, -1.55929705211554, 0.147715827800676, -0.542626171203849, 
0.745294589678554, -0.254290052908619, 0.939894889209173)), .Names = c("xx", 
"yy", "zz....rnorm.20."), row.names = c(NA, -20L), class = "data.frame")

dat1
##=============End Example===============##

John Kane
Kingston ON Canada


> -----Original Message-----
> From: ntfredo at gmail.com
> Sent: Tue, 31 Mar 2015 10:55:11 +0300
> To: jdnewmil at dcn.davis.ca.us
> Subject: Re: [R] Multiple Plots using ggplot
> 
>  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.
>> 
>> 
> 
> 	[[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 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!



More information about the R-help mailing list