-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Setting color of individual trajectories #152
Comments
Hi, There's currently no option to do this directly, but I see some advantages to making your proposal the default. I'll look into this. For now, you can achieve it relatively easily by using To illustrate: data(latrendData)
method <- lcMethodLMKM(Y ~ Time, id = "Id", time = "Time")
model <- latrend(method, latrendData, nClusters = 3) Now to create the plot: plotTrajectories(trajs, cluster = trajectoryAssignments(model), facet = FALSE) +
facet_wrap(~ Cluster) + # enable facet by cluster
geom_line(data = clusterTrajectories(model), aes(x = Time, y = Y), color = 'black') # add cluster lines Note that for drawing the cluster trajectories, you need to specify the correct time and value variables (Time and Y in the example). |
Thank you, this is working for me! Is there a way to change the alpha of the individual trajectories so that they are less transparent? |
I'm not aware of an easy way to change the alpha of already-added geom lines of a ggplot object. It's probably easier to create your own plot, using the data output from |
You were very close. This works for me: ggplot() +
geom_line( data = df, aes( x = Time, y = Y, group = Id, color = Cluster ), alpha = 0.3) +
facet_wrap(~ Cluster) +
geom_line(data = df_clust_traj, aes(x = Time, y = Y), color = 'black')
I don't see each cluster trajectory per facet. Did you use: df = trajectories(kmlModel4)
df_clust_traj = clusterTrajectories(kmlModel4) |
When I use df = trajectories(kmlModel4), the dataframe only has ID, Time, and Y. So I had to extract the cluster assignments for each ID and join the dataframes so that df has ID, Time, Y, and Cluster: df <- df %>% I did use df_clust_traj = clusterTrajectories(kmlModel4). I'm hoping to generate a version of the plot with the cluster lines set to the same color as the individual trajectories. When I set color to 'Cluster' with the 2nd geom-line, then each facet shows all 4 cluster trajectories. |
Oops, I forgot to mention in my previous post that I made a change, reported in #153, where To get the change, install the latest commit from master using |
Thank you! Is there a way to change the cluster trajectory within each facet to match the color of the individual trajectories? For example? Instead of black lines it would be red for cluster A, green for B, etc? |
Definitely, but then the cluster trajectories will be very hard to see against the trajectories. Just set |
When I do that, all 4 cluster trajectories show up in each facet. Does that occur for you? |
If you're using the original df code you posted, check if the cluster names are the same for both data.frames This works for me: df = trajectories(kmlModel4)
df_clust_traj = clusterTrajectories(kmlModel4)
ggplot() +
geom_line( data = df, aes( x = Time, y = Y, group = Id, color = Cluster ), alpha = 0.3) +
facet_wrap(~ Cluster) +
geom_line(data = df_clust_traj, aes(x = Time, y = Y), color = 'black') |
Yes, it works for me now too! I am using:
To have the cluster trajectories match the color of the individual trajectories. Thank you! |
Hello,
I was wondering if there is a way to set the color of the individual trajectories within the plot() function. I would like to set the color of each individual trajectory according to the cluster it has been assigned to.
For example, for this example in the vignette:
All of the lines in A would be red, all the lines in B would be green, and so forth (rather than black).
Thank you!
The text was updated successfully, but these errors were encountered: