felix93
felix93

Reputation: 39

Is it possible to add one ggplot to above another, when I use scatterplot?

I have one dataset with points for the day, and I have one dataset with points for the night. I want to add the plot for the night above the one for the day, so I can compare the two in the same plot. Is this possible?

I already have ggplot for day and night, called plot_day and plot_night. I want to add the points for night in the plot for the day, and have different shapes and color of the points, so I can easily see that square is, for example, day and circle is for example night:

ggplot(plot_day, aes(x=time, c(plot_day, plot_night))) +
ggtitle("") + theme_update(plot.title=element_text(hjust=0.5))+
geom_points(aes(y=plot_day, colour="plot_day"))+
geom_point(aes(y=plot_night, colour="plot_night"))+
labs(title="", x="", y="") + ylim(c(0,5))+
scale_color_discrete(name="", labels=c("Day", "Night")), theme_light() +
scale_x_date(date_labels=%b%, date_breaks="1 month", minor_breaks=NULL)+
theme_update(plot.title=element_text(hjust=0.5)) + theme_light()

Upvotes: 0

Views: 207

Answers (1)

teunbrand
teunbrand

Reputation: 37933

My approach would be something like the following, assuming data_day and data_night have the same columns (but substitute your own data):

ggplot(data_day, aes(x = time, y = some_y_value_column)) +
  geom_point(data = data_day, aes(shape = "day", colour = "day")) +
  geom_point(data = data_night, aes(shape = "night", colour = "night")) +
  ...theme/scales/labs etc...

You can then control the shapes and colours by adding the appropriate scales:

  scale_colour_manual(values = c("red", "blue"), breaks = c("day", "night")) +
  scale_shape_manual(values = c(15, 19), breaks = c("day", "night"))

EDIT: would even be easier if you would combine your data first and do something like the following:

new_data <- rbind(cbind(data_day, id = "day"),
                  cbind(data_night, id = "night")

ggplot(new_data, aes(x = time, y = some_y_value_column)) +
  geom_point(aes(shape = id, colour = id))

Upvotes: 1

Related Questions