ggplot: Is it possible to plot different colors points for different time windows?

前端 未结 1 1584
余生分开走
余生分开走 2021-01-23 06:28

I have a question regarding ggplot.

I have a dataset that consists of different timepoints \"time\", x and y coordinates (named X_MA and Y_MA), see an excerpt of my data

相关标签:
1条回答
  • 2021-01-23 06:52

    You can do something like this:

    ggplot(data2,aes(x = X_MA, y = Y_MA, 
                     color =  cut(time, breaks = c(0,2,12,1000), labels = c("First two 
                     seconds","Middle two seconds","Last period")))) + 
           stat_density_2d(aes(fill = ..density..), geom = "raster", contour = FALSE) + 
           scale_fill_distiller(palette=8, direction=-1) + 
           scale_x_continuous(expand = c(0, 0)) + 
           scale_y_continuous(expand = c(0, 0)) + 
           theme(legend.position='n_one') + 
           geom_point(size=1) +
           guides(color=guide_legend(title="Time category"))
    

    Cut will split you data set up into intevals, which you can name here. Note that you will need to change the numbers for the intervals - I don't know what the first time reading is in your dataset, but I have left here for illustration.

    The guides bit at the bottom is there simply to name the legend.

    0 讨论(0)
提交回复
热议问题