![]() ![]() Scale_color_manual(values=c('red','blue','green')) +ĭata = d_long %>% slice_max(order_by = Time, n = 1), ![]() Ggplot(aes(x = Time, y = value, group = var, color = color_group)) + For more info on this check the documentation on grouping. To ensure you get one line per original variable you also need to set group = var. # inspect final table prior to plotting to confirm color mappingsįinally, generate line plot where color is mapped to the color_group variable. Pivot_longer(cols = -Time, names_to = "var", values_to = "value") %>% # set seed to make simulated data reproducible It's better to assign the colors separately and then join into the long format data by variable name prior to plotting.īelow is an example of how I'd do this with your data.įirst, prepare the data without the color mapping for each variable, we'll do that next: # load necessary packages To add: I would like to obtain the same plot above in the following general case, where I can't add the vector c to the data frame as length(c) is not equal to length(x1)=.=length(x4) Time <-c(1:5)Īs you point out in your comments, it is only possible to put the vector of colors as a column in the original ame because it happens to be square, but this is a dangerous way to store the information because the colors really belong to the columns rather than the rows. I want x1 and x2 in red, x3 in blue and x4 in green. Na.rm = TRUE,show.legend = F,color='black')īut I don't get the needed plot, the resulted curves are not colored according to the label. Scale_color_manual(values=c('red','blue','green'))+ Ggplot(aes(x=Time,y=value,color=factor(class),group=name))+ Label=ifelse(duplicated(Label),NA,Label)) %>% Mutate(Label=ifelse(Time=max(Time,na.rm = T),name,NA), Rename(class=value) %>% select(-name) %>% I have the following data frame which contains 4 columns of data in addition to the vector of labels c. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |