dashed and solid lines in a plot

1,980 views
Skip to first unread message

phil.c...@oracle.com

unread,
Oct 30, 2013, 11:34:27 AM10/30/13
to ggp...@googlegroups.com
I'm making the plot below using the following qplot specification:

plot <- qplot(x, y, data = df, colour = Experiments, , geom = "line", main = Title)
plot + scale_colour_manual(values = c("red", "purple", "blue", "yellow", "black", "orange", "green", "pink")) + labs(y = YLabel, x = "Log2 Message Size")

However, I'd like the purple, yellow, orange, and pink lines to be dashed and the others solid.
How would I do this in ggplot (or qplot)?
Thanks


Doug Mitarotonda

unread,
Oct 30, 2013, 1:18:17 PM10/30/13
to phil.c...@oracle.com, ggplot2
You are going to want to use scale_linetype_manual, something like:
scale_linetype_manual(values=c("solid", "dotted", "solid", [etc.]))

where [etc.] is where you fill in what you want. 

This website also notes the different linetypes if you want to use something other than dotted:


Best wishes,
Doug


--
--
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.
For more options, visit https://groups.google.com/groups/opt_out.

Philip Cannata

unread,
Oct 30, 2013, 2:49:50 PM10/30/13
to Doug Mitarotonda, ggplot2, phil.c...@oracle.com
Doug, thanks for the quick response. Here's what I tried

        plot <- qplot(x, y, data = df, colour = Experiments, , geom = "line", main = Title)
        plot + scale_colour_manual(values = c("red", "red", "blue", "blue", "green", "green", "black", "black")) +
        scale_linetype_manual(values=c("solid", "dashed", "solid", "dashed", "solid", "dashed", "solid", "dashed")) +
        labs(y = YLabel, x = "Log2 Message Size")
but I still get all solid lines.
phil

Doug Mitarotonda

unread,
Oct 30, 2013, 3:05:49 PM10/30/13
to Philip Cannata, ggplot2
Looks like you need to specify the linetype aesthetic as well. For example:
df <- data.frame(A = rep(1:3, times = 2), B = c(4:9), L = rep(c("S", "D"), each = 3))
ggplot(df, aes(x = A, y = B)) + geom_line(aes(linetype = L)) + scale_linetype_manual(values = c(S = "solid", D = "dashed"))

In this example, you map the linetypes using L and then override them. In your case I would do something like aes(linetype = Experiments) and the override them with what I provided before.

Philip Cannata

unread,
Oct 30, 2013, 4:02:34 PM10/30/13
to Doug Mitarotonda, ggplot2, phil.c...@oracle.com
Sorry, I'm a newbie and your response doesn't make sense yet.
I have 3 R variables x, y, and Experiments and I do
df <- data.frame(x, y, Experiments)

        plot <- qplot(x, y, data = df, colour = Experiments, , geom = "line", main = Title)
        plot + scale_colour_manual(values = c("red", "red", "blue", "blue", "green", "green", "black", "black")) +
        scale_linetype_manual(values=c("solid", "dashed", "solid", "dashed", "solid", "dashed", "solid", "dashed")) +
        labs(y = YLabel, x = "Log2 Message Size")

I don't know how to factor your df and ggplot statements below into this.
Sorry to be a pain.
phil

Doug Mitarotonda

unread,
Oct 30, 2013, 4:14:15 PM10/30/13
to Philip Cannata, ggplot2
Hey Phil,
Sorry I am not being clear for you. I have edited my example in a style similar to what you are presenting:

df <- data.frame(x = rep(1:3, times = 2), y = c(4:9), Experiments = rep(c("Exp7833", "Exp7836"), each = 3))

ggplot(df, aes(x = x, y = y)) + geom_line(aes(linetype = Experiments, color = Experiments)) + scale_linetype_manual(values = c(Exp7833 = "solid", Exp7836 = "dashed"))

In general, for future reference, check this out on how to provide a minimum working example (MWE). https://github.com/hadley/devtools/wiki/Reproducibility

If you provide an MWE, then we can be sure to be "speaking the same language" on the same data structures, so this kind of confusion can be avoided.

Best wishes,
Doug

Philip Cannata

unread,
Oct 30, 2013, 4:35:09 PM10/30/13
to Doug Mitarotonda, ggplot2, phil.c...@oracle.com
The following actually worked beautifully, thanks for the help. Is there a place where I can find all of the scale_?_manual possibilities?
Thanks again for the quick responses and help this should get me going.
phil


df <- data.frame(x, y, Experiments)
        ggplot(df, aes(x = x, y = y)) + geom_line(aes(linetype = Experiments, color = Experiments)) +
        scale_colour_manual(values = c("red", "red", "blue", "blue", "green", "green", "black", "black")) +
        scale_linetype_manual(values=c("solid", "dashed", "solid", "dashed", "solid", "dashed", "solid", "dashed")) +
        labs(y = YLabel, x = "Log2 Message Size", title=Title)

Doug Mitarotonda

unread,
Oct 30, 2013, 5:00:05 PM10/30/13
to Philip Cannata, ggplot2
Other than flipping through the package's documentation in the R help window, this is the resource I use: http://docs.ggplot2.org/current/

Philip Cannata

unread,
Oct 31, 2013, 10:34:57 AM10/31/13
to Doug Mitarotonda, ggplot2, phil.c...@oracle.com
I found the following at  http://docs.ggplot2.org/0.9.3.1/scale_manual.html
    scale_colour_manual(..., values)
    scale_fill_manual(..., values)
    scale_size_manual(..., values)
    scale_shape_manual(..., values)
    scale_linetype_manual(..., values)
    scale_alpha_manual(..., values)
    scale_color_manual(..., values)
Thanks again for your help.
phil
Reply all
Reply to author
Forward
0 new messages