I have this five (5) geom_vline() in my plot and I want them to have a different colors. Is there a way to do that?
Here's my codes,
library(ggplot2)
x <- seq(-7, 8, length = 90)
tvalues <- dt(x,15)
qplot(x, tvalues) + geom_polygon(fill = "purple", colour = "purple", alpha = 0.5) +
geom_point(fill = "purple", colour = "purple", alpha = 0.2, pch = 21) +
geom_vline(xintercept = c(a <- c(-2.27685371, 0.01661155,
0.33598194, 1.92426022), mean(a)), linetype = "dashed", colour = "red") + theme_bw() + xlab(bquote(bold('Average Tensile Strength (lb/in'^'2'*')'))) +
ylab(expression(bold(P(x)))) +
opts(title = expression(bold("Student t Distribution")), plot.title = theme_text(size = 20, colour = "darkblue"),
panel.border = theme_rect(size = 2, colour = "red"))
And here is the output,
Notice the five vertical lines in the plot, I want each of those line to have a different color,
I tried this
library(colorRamps)
geom_vline(xintercept = c(a <- c(-2.27685371, 0.01661155,
0.33598194, 1.92426022), mean(a)), linetype = "dashed", colour = matlab.like(5))
but didn't work, Another attempt
geom_vline(xintercept = c(a <- c(-2.27685371, 0.01661155,
0.33598194, 1.92426022), mean(a)), linetype = "dashed", colour = c("red","blue","green","yellow","orange"))
and still unsuccessful.
Thanks in advance!
So you're sort of missing the fundamental idea behind ggplot2, which is that you always put all your data into a data.frame
and every aesthetic that you map corresponds to a variable in your data frame.
You could get 5 vertical lines, each of a different color with five separate calls to geom_vline
but that misses the point of the entire package. Instead, you create a data frame:
a <- c(-2.27685371,0.01661155,0.33598194,1.92426022)
vlines <- data.frame(xint = c(a,mean(a)),grp = letters[1:5])
I've explicitly created a grouping variable grp
to map to colour
. Then we add the layer and map the aesthetics to these variables using aes
:
qplot(x, tvalues) +
geom_polygon(fill = "purple", colour = "purple", alpha = 0.5) +
geom_point(fill = "purple", colour = "purple", alpha = 0.2, pch = 21) +
geom_vline(data = vlines,aes(xintercept = xint,colour = grp), linetype = "dashed") +
theme_bw() +
xlab(bquote(bold('Average Tensile Strength (lb/in'^'2'*')'))) +
ylab(expression(bold(P(x)))) +
opts(title = expression(bold("Student t Distribution")),
plot.title = theme_text(size = 20, colour = "darkblue"),
panel.border = theme_rect(size = 2, colour = "red"))
(The colors will be hard to distinguish because they're dashed lines, and two of them are nearly on top of each other.)
You will get much more out of ggplot2 if you transition away from qplot
towards ggplot()
and start putting your data into data frames rather than vectors.
If you just want to put one line, adding the following geom
would add a vertical line at x=1
in red colour and dashed.
+ geom_vline(aes(xintercept=1), colour="#BB0000", linetype="dashed")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With