ggplot- using geom_point and geom_line at the same time

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

ggplot- using geom_point and geom_line at the same time

Mary Kindall
Hi
I am plotting line chart using ggplot and want to use geom_line and
geom_point simultaneously.
I get the plot but now I have two legends.  None of the legend is
representing the true values. I need the legend with shape and color both.
Thanks



> con = textConnection("inputs  var1 var2 var3+ 100 10 5 2+ 1000 20 10 4+ 5000 30 15 8+ 10000 40 20 16+ 30000 50 25 32")> data = read.table(con, header=TRUE)> data  inputs var1 var2 var3
1    100   10    5    2
2   1000   20   10    4
3   5000   30   15    8
4  10000   40   20   16
5  30000   50   25   32> data = melt(data, id="inputs")> data   inputs
variable value
1     100     var1    10
2    1000     var1    20
3    5000     var1    30
4   10000     var1    40
5   30000     var1    50
6     100     var2     5
7    1000     var2    10
8    5000     var2    15
9   10000     var2    20
10  30000     var2    25
11    100     var3     2
12   1000     var3     4
13   5000     var3     8
14  10000     var3    16
15  30000     var3    32> g <- ggplot(data,aes(x=inputs, value,
colour=variable, fill = variable))> g <- g +
geom_point(aes(shape=variable), size=3) > g <- g +  geom_line(lwd=1) +
ylab("time") + xlab("inputs") +  labs(colour="MyLegend",  fill =
"MyLegend")> g

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

ggplot- using geom_point and geom_line at the same time

Mary Kindall
Hi
I am plotting line chart using ggplot and want to use geom_line and
geom_point simultaneously. I want to rename my legend but uptonow I remain
unsuccessful.
Someone please point what to add for renaming the legend.
I attached my example below.
Thanks



con = textConnection("inputs  var1  var2 var3
100 10 5 2
1000 20 10 4
5000 30 15 8
10000 40 20 16
30000 50 25 32")
 data = read.table(con, header=TRUE)
 data
 data = melt(data, id="inputs")
 g <- ggplot(data,aes(x=inputs, value, colour= variable, fill = variable,
shape=variable))
 g <- g + geom_line(lwd=0.8)
 g <- g + geom_point()
 g <- g + scale_x_continuous(name='Number of inputs')
 g <- g + scale_y_continuous('Conversion time (sec.)')
g



--
-------------
Mary Kindall
Yorktown Heights, NY
USA

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: ggplot- using geom_point and geom_line at the same time

Felipe Carrillo
Mary:
Here's one way.
## change the variable name to whatever title you want on your legend
data = melt(data, id="inputs",variable_name="customName")
data
g <- ggplot(data,aes(x=inputs, value, colour= customName, fill = customName,
shape=customName))
g <- g + geom_line(lwd=0.8)
g <- g + geom_point()
g <- g + scale_x_continuous(name='Number of inputs')
g <- g + scale_y_continuous('Conversion time (sec.)')

Felipe D. Carrillo
Supervisory Fishery Biologist
Department of the Interior
US Fish & Wildlife Service
California, USA
http://www.fws.gov/redbluff/rbdd_jsmp.aspx


From: Mary Kindall <[hidden email]>

>To: [hidden email]
>Sent: Monday, January 16, 2012 1:14 PM
>Subject: [R] ggplot- using geom_point and geom_line at the same time
>
>Hi
>I am plotting line chart using ggplot and want to use geom_line and
>geom_point simultaneously. I want to rename my legend but uptonow I remain
>unsuccessful.
>Someone please point what to add for renaming the legend.
>I attached my example below.
>Thanks
>
>
>
>con = textConnection("inputs  var1  var2 var3
>100 10 5 2
>1000 20 10 4
>5000 30 15 8
>10000 40 20 16
>30000 50 25 32")
>data = read.table(con, header=TRUE)
>data
>data = melt(data, id="inputs")
>g <- ggplot(data,aes(x=inputs, value, colour= variable, fill = variable,
>shape=variable))
>g <- g + geom_line(lwd=0.8)
>g <- g + geom_point()
>g <- g + scale_x_continuous(name='Number of inputs')
>g <- g + scale_y_continuous('Conversion time (sec.)')
>g
>
>
>
>--
>-------------
>Mary Kindall
>Yorktown Heights, NY
>USA
>
>    [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] mailing list
>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]]


______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: ggplot- using geom_point and geom_line at the same time

Mary Kindall
Thanks for reply
I wanted to have legend name with spaces. Right now I am using the
following code but it produce two legends. I have to use Gimp to cut the
redundant legend.

----------
con = textConnection("inputs  var1  var2 var3
100 10 5 2
1000 20 10 4
5000 30 15 8
10000 40 20 16
30000 50 25 32")
 data = read.table(con, header=TRUE)
 data
 data = melt(data, id="inputs")
 g <- ggplot(data,aes(x=inputs, value, colour= variable, fill = variable,
shape=variable))
 g <- g + geom_line(lwd=0.8)
 g <- g + geom_point()
g <- g + scale_colour_discrete('my Custom Legend')
g <- g + scale_shape_discrete("my Custom Legend")
g

 -----------------

On Mon, Jan 16, 2012 at 6:55 PM, Felipe Carrillo
<[hidden email]>wrote:

> Mary:
> Here's one way.
> ## change the variable name to whatever title you want on your legend
> data = melt(data, id="inputs",variable_name="customName")
> data
> g <- ggplot(data,aes(x=inputs, value, colour= customName, fill =
> customName,
> shape=customName))
> g <- g + geom_line(lwd=0.8)
> g <- g + geom_point()
> g <- g + scale_x_continuous(name='Number of inputs')
> g <- g + scale_y_continuous('Conversion time (sec.)')
>
> Felipe D. Carrillo
> Supervisory Fishery Biologist
> Department of the Interior
> US Fish & Wildlife Service
> California, USA
> http://www.fws.gov/redbluff/rbdd_jsmp.aspx
>
>   *From:* Mary Kindall <[hidden email]>
> *To:* [hidden email]
> *Sent:* Monday, January 16, 2012 1:14 PM
> *Subject:* [R] ggplot- using geom_point and geom_line at the same time
>
> Hi
> I am plotting line chart using ggplot and want to use geom_line and
> geom_point simultaneously. I want to rename my legend but uptonow I remain
> unsuccessful.
> Someone please point what to add for renaming the legend.
> I attached my example below.
> Thanks
>
>
>
> con = textConnection("inputs  var1  var2 var3
> 100 10 5 2
> 1000 20 10 4
> 5000 30 15 8
> 10000 40 20 16
> 30000 50 25 32")
> data = read.table(con, header=TRUE)
> data
> data = melt(data, id="inputs")
> g <- ggplot(data,aes(x=inputs, value, colour= variable, fill = variable,
> shape=variable))
> g <- g + geom_line(lwd=0.8)
> g <- g + geom_point()
> g <- g + scale_x_continuous(name='Number of inputs')
> g <- g + scale_y_continuous('Conversion time (sec.)')
> g
>
>
>
> --
> -------------
> Mary Kindall
> Yorktown Heights, NY
> USA
>
>     [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> 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.
>
>
>


--
-------------
Mary Kindall
Yorktown Heights, NY
USA

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: ggplot- using geom_point and geom_line at the same time

Michael Weylandt
Just jumping into the thread it looks like your penultimate and antepenultimate lines of code both add a legend - perhaps cut one?

Michael

On Jan 16, 2012, at 7:05 PM, Mary Kindall <[hidden email]> wrote:

> Thanks for reply
> I wanted to have legend name with spaces. Right now I am using the
> following code but it produce two legends. I have to use Gimp to cut the
> redundant legend.
>
> ----------
> con = textConnection("inputs  var1  var2 var3
> 100 10 5 2
> 1000 20 10 4
> 5000 30 15 8
> 10000 40 20 16
> 30000 50 25 32")
> data = read.table(con, header=TRUE)
> data
> data = melt(data, id="inputs")
> g <- ggplot(data,aes(x=inputs, value, colour= variable, fill = variable,
> shape=variable))
> g <- g + geom_line(lwd=0.8)
> g <- g + geom_point()
> g <- g + scale_colour_discrete('my Custom Legend')
> g <- g + scale_shape_discrete("my Custom Legend")
> g
>
> -----------------
>
> On Mon, Jan 16, 2012 at 6:55 PM, Felipe Carrillo
> <[hidden email]>wrote:
>
>> Mary:
>> Here's one way.
>> ## change the variable name to whatever title you want on your legend
>> data = melt(data, id="inputs",variable_name="customName")
>> data
>> g <- ggplot(data,aes(x=inputs, value, colour= customName, fill =
>> customName,
>> shape=customName))
>> g <- g + geom_line(lwd=0.8)
>> g <- g + geom_point()
>> g <- g + scale_x_continuous(name='Number of inputs')
>> g <- g + scale_y_continuous('Conversion time (sec.)')
>>
>> Felipe D. Carrillo
>> Supervisory Fishery Biologist
>> Department of the Interior
>> US Fish & Wildlife Service
>> California, USA
>> http://www.fws.gov/redbluff/rbdd_jsmp.aspx
>>
>>  *From:* Mary Kindall <[hidden email]>
>> *To:* [hidden email]
>> *Sent:* Monday, January 16, 2012 1:14 PM
>> *Subject:* [R] ggplot- using geom_point and geom_line at the same time
>>
>> Hi
>> I am plotting line chart using ggplot and want to use geom_line and
>> geom_point simultaneously. I want to rename my legend but uptonow I remain
>> unsuccessful.
>> Someone please point what to add for renaming the legend.
>> I attached my example below.
>> Thanks
>>
>>
>>
>> con = textConnection("inputs  var1  var2 var3
>> 100 10 5 2
>> 1000 20 10 4
>> 5000 30 15 8
>> 10000 40 20 16
>> 30000 50 25 32")
>> data = read.table(con, header=TRUE)
>> data
>> data = melt(data, id="inputs")
>> g <- ggplot(data,aes(x=inputs, value, colour= variable, fill = variable,
>> shape=variable))
>> g <- g + geom_line(lwd=0.8)
>> g <- g + geom_point()
>> g <- g + scale_x_continuous(name='Number of inputs')
>> g <- g + scale_y_continuous('Conversion time (sec.)')
>> g
>>
>>
>>
>> --
>> -------------
>> Mary Kindall
>> Yorktown Heights, NY
>> USA
>>
>>    [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list
>> 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.
>>
>>
>>
>
>
> --
> -------------
> Mary Kindall
> Yorktown Heights, NY
> USA
>
>    [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> 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.

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: ggplot- using geom_point and geom_line at the same time

Hadley Wickham-2
In reply to this post by Mary Kindall
On Mon, Jan 16, 2012 at 6:05 PM, Mary Kindall <[hidden email]> wrote:
> Thanks for reply
> I wanted to have legend name with spaces. Right now I am using the
> following code but it produce two legends. I have to use Gimp to cut the
> redundant legend.

Your basic problem is that you're using the fill and colour
aesthetics, but you only need colour.

Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: ggplot- using geom_point and geom_line at the same time

Mary Kindall
Thanks Hadley for your input.
The following code works fine now.
Thanks again


con = textConnection("inputs  var1  var2  var3
100 10 5 2
1000 20 10 4
5000 30 15 8
10000 40 20 16
30000 50 25 32")
 data = read.table(con, header=TRUE)
 data
 data = melt(data, id="inputs")
 g <- ggplot(data,aes(x=inputs, value, colour= variable, shape=variable))
 g <- g + geom_line(lwd=0.8)
 g <- g + geom_point()
g <- g + scale_colour_discrete('my Custom Legend')
g <- g + scale_shape_discrete("my Custom Legend")
g


-------------

On Tue, Jan 17, 2012 at 10:07 AM, Hadley Wickham <[hidden email]> wrote:

> On Mon, Jan 16, 2012 at 6:05 PM, Mary Kindall <[hidden email]>
> wrote:
> > Thanks for reply
> > I wanted to have legend name with spaces. Right now I am using the
> > following code but it produce two legends. I have to use Gimp to cut the
> > redundant legend.
>
> Your basic problem is that you're using the fill and colour
> aesthetics, but you only need colour.
>
> Hadley
>
> --
> Assistant Professor / Dobelman Family Junior Chair
> Department of Statistics / Rice University
> http://had.co.nz/
>



--
-------------
Mary Kindall
Yorktown Heights, NY
USA

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
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.