I am new to ggplot2
. I would like to create a line plot that has points on them where the points are filled with different colors than the lines (see the plot below).
Suppose the dataset I am working with is the one below:
set.seed(100)
data<-data.frame(dv=c(rnorm(30), rnorm(30, mean=1), rnorm(30, mean=2)),
iv=rep(1:30, 3),
group=rep(letters[1:3], each=30))
I tried the following code:
p<-ggplot(data, aes(x=iv, y=dv, group=group, pch=group)) + geom_line() + geom_point()
p + scale_color_manual(values=rep("black",3))+ scale_shape(c(19,20,21)) +
scale_fill_manual(values=c("blue", "red","gray"))
p + scale_shape(c(19,20,21)) + scale_fill_manual(values=c("blue", "red","gray"))
But I do not get what I want.I hope someone can point me to the right direction. Thanks!
A custom color palettes can be specified using the functions : scale_fill_manual() for box plot, bar plot, violin plot, etc. scale_color_manual() for lines and points.
Integrating the pipe operator with ggplot2 The pipe operator can also be used to link data manipulation with consequent data visualization.
To change the color and the size of points, use the following arguments: col : color (hexadecimal color code or color name). For example, col = "blue" or col = "#4F6228" .
scale_fill_manual()
, scale_shape_manual()
and scale_colour_manual()
can be used only if you have set fill=
, shape=
or colour=
inside the aes()
.
To change colour just for the points you should add colour=group
inside geom_point()
call.
ggplot(data, aes(x=iv, y=dv, group=group,shape=group)) +
geom_line() + geom_point(aes(colour=group)) +
scale_shape_manual(values=c(19,20,21))+
scale_colour_manual(values=c("blue", "red","gray"))
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