add diffrent geom_line to set of data by variable

19 views
Skip to first unread message

John Dieji

unread,
Oct 7, 2015, 10:52:49 AM10/7/15
to ggplot2
Hi everyone,

Im new to ggplot and have a small problem for which I cant find a solution.

My plot works fine and everything is ok, I have only this problem:

My dataframe looks like this:

      date                              V1               V2                V3                V4           Color
1 2015-09-01 12:32:45    593.6667    1378.667     25.69695    104.50518      1
2 2015-09-02 13:10:10    627.0000    1300.667     34.39477     27.06166      1
3 2015-09-04 12:26:22    697.6667    1406.667     66.10850     50.56020      1
4 2015-09-01 12:32:45    557.6667    1359.333     20.79263     63.81484      2
5 2015-09-02 13:10:10    657.3333    1314.333     44.28694     91.11714      2
6 2015-09-04 12:26:22    653.0000    1313.333     31.09662     43.46646      2

And Im plotting with this code:

plot <- ggplot(df, aes(x=v1, y=v2), colour=color) +
           geom_line() +
           geom_point() 

So than I got my plot. The Problem is I want to have a plot where only data with the same color (or color value 1,2..) have a line between them. If im plotting with my code every point is bound together. But I only want to have a connection between data points which have the same colot value.

Im glad for every help,

best j

Tim Richter-Heitmann

unread,
Oct 7, 2015, 10:58:00 AM10/7/15
to ggp...@googlegroups.com
I am sure you need to set the group-aesthetics in geom_line() to Color,
but without having an example dataset, i cant really tell. But
forgetting to set the group-argument in geom_line usually leads to
undesired results.
Maybe you can put forward your input data (as per dput(df)), so i can
have a look.
> --
> --
> 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
> <mailto:ggplot2+u...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.


--
Tim Richter-Heitmann (M.Sc.)
PhD Candidate



International Max-Planck Research School for Marine Microbiology
University of Bremen
Microbial Ecophysiology Group (AG Friedrich)
FB02 - Biologie/Chemie
Leobener Straße (NW2 A2130)
D-28359 Bremen
Tel.: 0049(0)421 218-63062
Fax: 0049(0)421 218-63069

Doug Mitarotonda

unread,
Oct 7, 2015, 11:03:24 AM10/7/15
to John Dieji, ggplot2
A couple of things:
1) to get what you want, you should see the `group` argument to `aes`
2) R is case sensitive, so your code would not work as written
3) by looking at your code you probably want to map Color in `aes`, not set it outside the argument

So, you probably want something like:

plot <- ggplot(df, aes(x=V1, y=V2, group = Color, color = Color)) +
           geom_line() +
           geom_point() 

But, you did not provide a reproducible example, so I can’t be sure this works. Please see https://github.com/hadley/devtools/wiki/Reproducibility on how to write a good question.

(just saw Tim responded while I typed this…)


--
--
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.
Reply all
Reply to author
Forward
0 new messages