Skip to content
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

Closed
hichew22 opened this issue Apr 30, 2024 · 12 comments
Closed

Setting color of individual trajectories #152

hichew22 opened this issue Apr 30, 2024 · 12 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@hichew22
Copy link

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:
image

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!

@niekdt niekdt self-assigned this May 1, 2024
@niekdt niekdt added the enhancement New feature or request label May 1, 2024
@niekdt niekdt added this to the 1.6.1 milestone May 1, 2024
@niekdt
Copy link
Collaborator

niekdt commented May 1, 2024

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 plotTrajectories without faceting (this colors the trajectories), then faceting manually, and adding black cluster trajectories on top.

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

image

Note that for drawing the cluster trajectories, you need to specify the correct time and value variables (Time and Y in the example).

@hichew22
Copy link
Author

hichew22 commented May 1, 2024

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?

@niekdt
Copy link
Collaborator

niekdt commented May 1, 2024

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 trajectories(kmlModel4) and clusterTrajectories(kmlModel4)

@hichew22
Copy link
Author

hichew22 commented May 1, 2024

I created a data frame of the IDs, Y, Time, and cluster assignments (df). I also have a dataframe of the cluster trajectories (df_clust_traj).

I tried creating a ggplot as so:
p_kml <- ggplot() + geom_line( data = df, aes( x = Time, y = Y, fill = ID, color = Cluster ), alpha = 0.3) + facet_wrap(~ Cluster) + geom_line(data = df_clust_traj, aes(x = Time, y = Y, color = Cluster))

Now, there are 4 facets (1 for each Cluster), and the individual trajectories are plotted within each facet. However, each facet has all 4 cluster trajectories. For example, this is 1 of the facets:

image

Can you help me edit my code so that each cluster trajectory is assigned to its respective facet?

@niekdt
Copy link
Collaborator

niekdt commented May 2, 2024

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 changed fill to group, because line geoms do not have a fill property, but we do want to draw each trajectory separately.
  • I set the cluster lines to black so they are visible

I don't see each cluster trajectory per facet. Did you use:

df = trajectories(kmlModel4)
df_clust_traj = clusterTrajectories(kmlModel4)

@hichew22
Copy link
Author

hichew22 commented May 2, 2024

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:
`id <- ids(kml_model_4)
cluster_kml <- trajectoryAssignments(kml_model_4)
df_cluster_kml <- cbind(id, cluster_kml) %>%
as.data.frame()

df <- df %>%
left_join(., df_cluster_kml, by = "Id")`

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.

@niekdt
Copy link
Collaborator

niekdt commented May 3, 2024

Oops, I forgot to mention in my previous post that I made a change, reported in #153, where trajectories() now also outputs a Cluster column for lcModel objects. Then you'll find that the code above works.

To get the change, install the latest commit from master using remotes::install_github('philips-software/latrend')

@hichew22
Copy link
Author

hichew22 commented May 3, 2024

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?

@niekdt
Copy link
Collaborator

niekdt commented May 3, 2024

Definitely, but then the cluster trajectories will be very hard to see against the trajectories.

Just set color = Cluster as in your original code.

@hichew22
Copy link
Author

hichew22 commented May 3, 2024

When I do that, all 4 cluster trajectories show up in each facet. Does that occur for you?

@niekdt
Copy link
Collaborator

niekdt commented May 6, 2024

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')

image

@hichew22
Copy link
Author

hichew22 commented May 6, 2024

Yes, it works for me now too!

I am using:

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 = Cluster)

To have the cluster trajectories match the color of the individual trajectories. Thank you!

@hichew22 hichew22 closed this as completed May 6, 2024
@hichew22 hichew22 reopened this May 6, 2024
@hichew22 hichew22 closed this as completed May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants