我有一个带有白天点的数据集,而我有一个夜晚有点的数据集。我想将夜间的图添加到白天的图上,因此可以在同一图中比较两者。这可能吗?
我已经有白天和黑夜的ggplot,分别称为plot_day和plot_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()
答案 0 :(得分:0)
假设data_day
和data_night
具有相同的列(但替换您自己的数据),我的方法将类似于以下内容:
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...
然后您可以通过添加适当的比例来控制形状和颜色:
scale_colour_manual(values = c("red", "blue"), breaks = c("day", "night")) +
scale_shape_manual(values = c(15, 19), breaks = c("day", "night"))
编辑:如果您首先合并数据并执行以下操作,甚至会更加容易:
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))