Changing the default properties of geom_line, geom_point, etc.

65 views
Skip to first unread message

John Harrold

unread,
Apr 21, 2015, 1:06:38 PM4/21/15
to ggp...@googlegroups.com
Hello,

This may be a very naive question as I'm very new to R and ggplot. I know that I can specify properties each time I add a layer using geom_line for example below:

x = seq(0,10, by=.1)
y1 = sin(x)
y2 = cos(x)
myd = data.frame(x =x, y1=y1, y2=y2)
p = ggplot()
p = p + geom_line(data=myd, aes(x=x, y=y1), color='blue', size=2) 
p = p + geom_line(data=myd, aes(x=x, y=y2), color='red', size=2) 
print(p)

What is the appropriate way to change (for example) the default size of lines so I do not have to specify it for each layer?

Thanks
John

Doug Mitarotonda

unread,
Apr 21, 2015, 1:17:20 PM4/21/15
to John Harrold, ggp...@googlegroups.com
Hi John,
ggplot usually thinks of the input as “long” data frames, not “wide.” See packages `tidyr` and `reshape2` for more information on how to use tools to convert. But just using your example the way I would do it is:

# same
x = seq(0,10, by=.1)
y1 = sin(x)
y2 = cos(x)
# I am manually creating the long version, but could be done using `tidyr` or `reshape2`
myd2 <- data.frame(x = c(x, x), y = c(y1, y2), type = c(rep("y1", length(y1), rep("y2", length(y2)))), stringsAsFactors = FALSE)
# Note how you “map” the type on to the color aesthetic
p <- ggplot(myd2, aes(x = x, y = y)) + geom_line(aes(color = type), size = 2)
# if you want to manually set the colors of the lines
p <- p + scale_color_manual(values = c("blue", "red"))
print(p)

Cheers,
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/d/optout.

Roman Luštrik

unread,
Apr 21, 2015, 1:20:35 PM4/21/15
to Doug Mitarotonda, John Harrold, ggplot2
In addition to Doug's comments, here is how I would melt your data.frame and use a RColorBrewer palette.

library(reshape2)
head(myd)
molten.xyd <- melt(myd, id.vars = "x")

ggplot(molten.xyd, aes(x = x, y = value, color = variable)) +
    theme_bw() +
    scale_color_brewer(palette = "Set1") +
    geom_line()


--
In God we trust, all others bring data.

John Harrold

unread,
Apr 21, 2015, 2:15:19 PM4/21/15
to ggp...@googlegroups.com, john.m....@gmail.com, dougmit...@gmail.com
I apologize, I don't think I was clear. What I wanted was the appropriate way to change the default line thickness for all lines instead of specifying it each time I called geom_line(). Similarly how would I change the default size of points using geom_point(). I would imagine these are done using theme(), but I'm not sure how to go about changing the defaults.

Sorry again if I wasn't clear. 
Reply all
Reply to author
Forward
0 new messages