0

I have data (as below), categorized into four different clusters. I want to have all curves of cluster 1 in black, cluster 2 in green, cluster 3 in blue, and 4 in red. How can I do that?

Appreciate!

My code:

matplot(t(Rank_Cum_Xp_Sorted[,-c(ncol(Rank_Cum_Xp_Sorted)-1,ncol(Rank_Cum_Xp_Sorted))]),lty=Rank_Cum_Xp_Sorted$Cluster,col=Rank_Cum_Xp_Sorted$Cluster,type="l",xlab = "",xaxt='n',ylab = "")
gplots::angleAxis(side= 1,labels = Dates, srt = 90,cex=1)
title(ylab="Rank", line=2.2, cex.lab=1.2)

My Data:

Rank_Cum_Xp_Sorted <- structure(list(V3 = c(52.6666666666667, 39.1666666666667, 52.6666666666667, 
39.1666666666667, 30.5, 52.6666666666667, 39.1666666666667, 39.1666666666667, 
39.1666666666667, 39.1666666666667, 30.5, 52.6666666666667, 52.6666666666667, 
32, 39.1666666666667, 39.1666666666667, 39.1666666666667, 39.1666666666667, 
39.1666666666667, 52.6666666666667, 39.1666666666667, 39.1666666666667, 
39.1666666666667, 52.6666666666667, 30.5, 52.6666666666667, 52.6666666666667, 
39.1666666666667, 52.6666666666667, 39.1666666666667, 39.1666666666667, 
52.6666666666667, 39.1666666666667, 52.6666666666667, 39.1666666666667, 
39.1666666666667, 67.6666666666667, 52.6666666666667, 39.1666666666667, 
52.6666666666667, 52.6666666666667, 67.6666666666667, 30.5, 52.6666666666667, 
52.6666666666667, 52.6666666666667, 52.6666666666667, 39.1666666666667, 
52.6666666666667, 39.1666666666667, 39.1666666666667, 39.1666666666667, 
52.6666666666667, 52.6666666666667, 39.1666666666667, 39.1666666666667, 
52.6666666666667, 52.6666666666667, 52.6666666666667, 67.6666666666667, 
52.6666666666667, 39.1666666666667, 39.1666666666667, 39.1666666666667, 
52.6666666666667, 52.6666666666667, 89.5, 39.1666666666667, 39.1666666666667, 
32, 30.5, 39.1666666666667, 52.6666666666667, 52.6666666666667, 
39.1666666666667, 52.6666666666667, 39.1666666666667, 39.1666666666667, 
39.1666666666667, 52.6666666666667, 39.1666666666667, 39.1666666666667, 
52.6666666666667, 52.6666666666667, 52.6666666666667, 31.5, 52.6666666666667, 
52.6666666666667, 39.1666666666667, 39.1666666666667), V4 = c(57.25, 
37.5, 57.25, 37.5, 23.875, 57.25, 37.5, 37.5, 37.5, 37.5, 23.875, 
57.25, 57.25, 26.5, 37.5, 37.5, 37.5, 37.5, 37.5, 57.25, 32.25, 
37.5, 37.5, 57.25, 23.875, 57.25, 41.5, 37.5, 57.25, 37.5, 37.5, 
57.25, 37.5, 57.25, 37.5, 37.5, 52.75, 57.25, 37.5, 57.25, 57.25, 
68.5, 23.875, 57.25, 57.25, 57.25, 57.25, 37.5, 57.25, 37.5, 
37.5, 37.5, 57.25, 57.25, 37.5, 37.5, 57.25, 57.25, 57.25, 68.5, 
57.25, 37.5, 37.5, 37.5, 57.25, 57.25, 89.625, 37.5, 37.5, 24.25, 
23.875, 37.5, 57.25, 57.25, 37.5, 57.25, 37.5, 37.5, 37.5, 57.25, 
37.5, 37.5, 57.25, 57.25, 57.25, 25.625, 57.25, 42.375, 37.5, 
37.5), V5 = c(60.4, 38.1, 60.4, 38.1, 20.8, 60.4, 38.1, 38.1, 
38.1, 38.1, 20.8, 60.4, 60.4, 21.4, 38.1, 30.5, 38.1, 33.7, 38.1, 
60.4, 30.3, 38.1, 38.1, 60.4, 20.8, 50.3, 36.1, 38.1, 46.6, 38.1, 
38.1, 60.4, 33.7, 60.4, 38.1, 38.1, 45.1, 50.3, 38.1, 60.4, 60.4, 
69.4, 20.8, 60.4, 60.4, 60.4, 60.4, 38.1, 60.4, 31.7, 38.1, 38.1, 
60.4, 60.4, 38.1, 38.1, 60.4, 60.4, 60.4, 69.4, 60.4, 33.7, 38.1, 
38.1, 60.4, 60.4, 89.7, 31.7, 38.1, 19.9, 20.8, 38.1, 60.4, 60.4, 
38.1, 60.4, 33.7, 38.1, 38.1, 60.4, 38.1, 38.1, 60.4, 60.4, 48.7, 
23.4, 60.4, 38.4, 38.1, 31.7), V6 = c(62.75, 39.1666666666667, 
62.75, 39.1666666666667, 19.0833333333333, 62.75, 39.1666666666667, 
39.1666666666667, 39.1666666666667, 39.1666666666667, 19.0833333333333, 
62.75, 62.75, 18, 39.1666666666667, 25.8333333333333, 39.1666666666667, 
31.8333333333333, 35.5, 51.1666666666667, 29.8333333333333, 39.1666666666667, 
39.1666666666667, 62.75, 19.0833333333333, 46.5, 30.9166666666667, 
39.1666666666667, 39.6666666666667, 39.1666666666667, 39.1666666666667, 
62.75, 31.8333333333333, 62.75, 35.5, 39.1666666666667, 40.4166666666667, 
46.5, 39.1666666666667, 62.75, 53.1666666666667, 70.25, 19.0833333333333, 
62.75, 62.75, 62.75, 62.75, 39.1666666666667, 62.75, 28.1666666666667, 
39.1666666666667, 39.1666666666667, 62.75, 62.75, 39.1666666666667, 
39.1666666666667, 62.75, 62.75, 62.75, 70.25, 62.75, 31.8333333333333, 
39.1666666666667, 39.1666666666667, 62.75, 62.75, 89.75, 28.1666666666667, 
39.1666666666667, 17, 19.0833333333333, 39.1666666666667, 62.75, 
53.1666666666667, 39.1666666666667, 62.75, 31.8333333333333, 
39.1666666666667, 39.1666666666667, 62.75, 39.1666666666667, 
39.1666666666667, 62.75, 62.75, 43.4166666666667, 22.3333333333333, 
62.75, 36.5833333333333, 39.1666666666667, 28.1666666666667), 
    V7 = c(63.8571428571429, 37.0714285714286, 65.9285714285714, 
    43.6428571428571, 19.8571428571429, 58.5714285714286, 43.6428571428571, 
    43.6428571428571, 43.6428571428571, 39.6428571428571, 18.2857142857143, 
    62.3571428571429, 65.9285714285714, 15.5714285714286, 43.6428571428571, 
    22.4285714285714, 43.6428571428571, 28.0714285714286, 36.5, 
    44.6428571428571, 32.1428571428571, 43.6428571428571, 37.0714285714286, 
    65.9285714285714, 19.8571428571429, 47.7142857142857, 28.9285714285714, 
    43.6428571428571, 34.7857142857143, 43.6428571428571, 35.0714285714286, 
    65.9285714285714, 32.7142857142857, 58.3571428571429, 35.8571428571429, 
    43.6428571428571, 38.1428571428571, 45.2857142857143, 37.0714285714286, 
    63.8571428571429, 47.7857142857143, 67.3571428571429, 19.8571428571429, 
    60.9285714285714, 65.9285714285714, 58.5714285714286, 63.8571428571429, 
    43.6428571428571, 62.0714285714286, 27.6428571428571, 43.6428571428571, 
    35.0714285714286, 65.9285714285714, 61.6428571428571, 43.6428571428571, 
    35.0714285714286, 60.9285714285714, 62.0714285714286, 60.9285714285714, 
    68.5, 56, 32.7142857142857, 40.7142857142857, 43.6428571428571, 
    60.9285714285714, 65.9285714285714, 89.7857142857143, 24.9285714285714, 
    37.0714285714286, 16.5, 19.8571428571429, 43.6428571428571, 
    65.9285714285714, 46.3571428571429, 37.0714285714286, 59.2142857142857, 
    32.7142857142857, 41.4285714285714, 39, 57.2857142857143, 
    43.6428571428571, 37.0714285714286, 60.1428571428571, 60.9285714285714, 
    38, 22.6428571428571, 65.9285714285714, 37.7142857142857, 
    43.6428571428571, 25.6428571428571), V8 = c(65, 36.9375, 
    68.375, 45.625, 21.875, 56.6875, 42.6875, 47.3125, 47.3125, 
    35.5625, 18.625, 62.5625, 61.125, 13.8125, 47.3125, 20.1875, 
    47.3125, 25.9375, 38.3125, 39.25, 34.75, 47.3125, 36.9375, 
    68.375, 21.875, 49.1875, 28.75, 47.3125, 31.8125, 47.3125, 
    32.75, 68.375, 30, 51.625, 32.75, 47.3125, 37.875, 40.1875, 
    36.9375, 65, 44.9375, 65.875, 21.875, 59.25, 68.375, 56.6875, 
    65, 45.625, 57.4375, 26.8125, 47.3125, 32.75, 68.375, 56.5625, 
    47.3125, 32.75, 60.25, 62.125, 60.25, 67.75, 52.125, 34.5625, 
    42.5625, 47.3125, 59.25, 68.375, 89.8125, 23.1875, 36.9375, 
    17.0625, 21.875, 47.3125, 68.375, 41.9375, 36.9375, 57.75, 
    29.1875, 43.6875, 40.0625, 54.625, 47.3125, 36.9375, 56.25, 
    59.25, 34.625, 22.4375, 68.375, 39.5, 47.3125, 24.5), V9 = c(61.0555555555556, 
    38.0555555555556, 70.2777777777778, 47.2777777777778, 24.6666666666667, 
    56.6666666666667, 43.1666666666667, 50.4444444444444, 45.3333333333333, 
    32.5555555555556, 17.5, 62.0555555555556, 58.5, 12.4444444444444, 
    50.4444444444444, 18.5, 50.4444444444444, 23.6111111111111, 
    40.7777777777778, 35.0555555555556, 37.7777777777778, 47.2777777777778, 
    38.0555555555556, 70.2777777777778, 24.6666666666667, 51.1111111111111, 
    29.7222222222222, 50.4444444444444, 29.9444444444444, 50.4444444444444, 
    31.7222222222222, 70.2777777777778, 28.3333333333333, 46.4444444444444, 
    30.7777777777778, 45.3333333333333, 38.8888888888889, 36.2777777777778, 
    38.0555555555556, 66.1666666666667, 43.8333333333333, 65.6666666666667, 
    24.6666666666667, 54.3333333333333, 70.2777777777778, 56.6666666666667, 
    66.1666666666667, 43.8333333333333, 54.9444444444444, 27.1111111111111, 
    50.4444444444444, 31.7222222222222, 70.2777777777778, 53.5555555555556, 
    50.4444444444444, 31.7222222222222, 60.6666666666667, 62.8333333333333, 
    60.6666666666667, 67.8333333333333, 50.2222222222222, 32.3888888888889, 
    40.4444444444444, 47.2777777777778, 54.3333333333333, 70.2777777777778, 
    89.8333333333333, 22.2777777777778, 38.0555555555556, 18.4444444444444, 
    24.6666666666667, 50.4444444444444, 70.2777777777778, 38.9444444444444, 
    38.0555555555556, 53, 26.5, 46.2222222222222, 42.1666666666667, 
    53.7777777777778, 50.4444444444444, 38.0555555555556, 54.3333333333333, 
    58.7777777777778, 32.4444444444444, 23.2222222222222, 70.2777777777778, 
    37.4444444444444, 50.4444444444444, 23.4444444444444), V10 = c(57.75, 
    41.3, 71.25, 42.95, 28.75, 58.05, 46.7, 49.6, 44.7, 31.5, 
    16.95, 58.3, 54.3, 11.3, 47.05, 16.85, 46.05, 21.55, 44.3, 
    32.05, 37.2, 47.2, 40.15, 66.05, 28.75, 48.8, 30.15, 51.7, 
    32.55, 54.3, 36.75, 69.25, 27.7, 45.55, 34.55, 42.3, 35.95, 
    34.65, 40.05, 62.75, 47.65, 64.3, 27.4, 53.8, 71.65, 58.6, 
    68.35, 44.1, 55.55, 32, 54, 33.75, 68.25, 56.05, 50.05, 31.35, 
    61.15, 62.75, 58.8, 65.25, 50.8, 34.75, 44.6, 47.95, 56.2, 
    71.95, 89.85, 21.4, 37.8, 21.25, 24.65, 46.05, 66.05, 35.85, 
    38.45, 54.55, 27.4, 48.15, 45.25, 52.15, 46.35, 36.45, 50.7, 
    54, 31.1, 25.1, 71.75, 36.9, 46.75, 28.4), Cluster = c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L)), class = "data.frame", row.names = c("AF", 
"AT", "BP", "CJ", "DC", 
"Fa", "FD", "Gonreira", "Gones", 
"Joãoeja", "Joãoeira", "Lueda", "reitas", 
"Luísnho", "Migeiro", "Natalideiro", "Nunoia", 
"Nunode", "Pedrro", "Ricar]ra", "Ricilva", 
"Rúbeines", "Tomaczewski", "Vasdro", "Adal", 
"Andra", "Antcheco", "Catarrêa", "Dioçalves", 
"Filres", "Fredeira", "Gonadeiro", "Jomador", 
"Joãorreto", "Joardal", "Jiro", "Josas", 
"Lonçalves", "Manurnandes", "Manurigues", "Marcilva", 
"Marreira", "Miguz", "Migcisco", "Migldes", 
"Migopes", "Paurreira", "Pedreiro", "Peoura", 
"Runcho", "Svonen", "Tontos", "Andguel", 
"Danodrigues", "Dominkova", "Francirata", "Guilhemes", 
"Joãsta", "Joues", "Jontos", "Jostos", 
"Márntos", "Migurino", "Rtos", "Stins", 
"Toues", "Yau", "Antino", "Ancisco", 
"Aniidian", "Anvares", "Birlar", "Caartins", 
"Diogimo", "Francilado", "Inêreira", "Irigues", 
"Joãado", "Joãres", "Joãlo", "Joãego", "Jooso", 
"Lulva", "Magnde", "Miguas", "Migegas", 
"Paloçalves", "Pbral", "Pemes", "Raquóvão"
))
Ester Silva
  • 670
  • 6
  • 24

2 Answers2

0

I think your solution will need to reshape the data before plotting them, into a "tall format", I will let you check in details with the library reshape that handles that.

Principle of the solution

The idea is to first reshape the data into a long format, append the cluster at the end and then plot it using ggplot. You can refer to this post that explains how to reshape data into a long format and plot it with ggplot.

After transposing your data, they are changed into this tall format.

Before:

              AF       AT       BP       CJ       DC       Fa       FD Gonreira
V3      52.66667 39.16667 52.66667 39.16667 30.50000 52.66667 39.16667 39.16667 ...
V4      57.25000 37.50000 57.25000 37.50000 23.87500 57.25000 37.50000 37.50000 ...
V5      60.40000 38.10000 60.40000 38.10000 20.80000 60.40000 38.10000 38.10000 ...
V6      62.75000 39.16667 62.75000 39.16667 19.08333 62.75000 39.16667 39.16667 ...
V7      63.85714 37.07143 65.92857 43.64286 19.85714 58.57143 43.64286 43.64286 ...
V8      65.00000 36.93750 68.37500 45.62500 21.87500 56.68750 42.68750 47.31250 ...
V9      61.05556 38.05556 70.27778 47.27778 24.66667 56.66667 43.16667 50.44444 ...
V10     57.75000 41.30000 71.25000 42.95000 28.75000 58.05000 46.70000 49.60000 ...

After:

  Var1 Var2    value cluster
1   V3   AF 52.66667       1
2   V4   AF 57.25000       1
3   V5   AF 60.40000       1
4   V6   AF 62.75000       1
5   V7   AF 63.85714       1
6   V8   AF 65.00000       1
...

Afterwards it is just a matter of making the ggplot graph by grouping the variables:

  • using linetype for the names
  • cluster for the colours.

Be careful though, I didn't change your code, but the transpose removes the V10 row as well...

Code solution

library(ggplot2)
library(reshape2)
library(dplyr)

# Be careful the transpose bellow removes the V10 column/row
transposed_data <- t(
    Rank_Cum_Xp_Sorted[,-c(
     ncol(Rank_Cum_Xp_Sorted)-1,
     ncol(Rank_Cum_Xp_Sorted)
    )]
)

# Using an ordered rowname
rownames(transposed_data) <- seq(1, nrow(transposed_data))
# Conversion to tall format like in the post referenced:
final_table <- melt(transposed_data, id.vars=rownames(transposed_data))

# Saving the clusters with their attached names, and adding them to the long format
clusters <- data.frame(
    name = rownames(Rank_Cum_Xp_Sorted),
    cluster = Rank_Cum_Xp_Sorted$Cluster
)
final_table <- final_table %>%
    left_join(clusters, by = c("Var2" = "name"))

ggplot(d, aes(x = Var1, y = value, linetype=Var2, color = cluster)) + 
  geom_line() +
  xlab("Variable") +
  ylab("Value")

And the output of the function: enter image description here

  • Thank You @Pierre. I edited your code like the provided answer (I added it as answer since it could not be as comment). Anyhow, why your code always give 13 curves (like I have only 13 users) while I have a lot more. I even tested the code using different datasets but it exactly gives 13 curves. Also, how can I only show the legend for the clusters only? I used 'scale_color_manual(name="Clusters", values = c("Achievers" = "black",'Regular' = "limegreen","Disheartened"="blue","Underachievers"="red"))' but it says since 'scale_color_manual' is already used, re-using it will reset the colors. – Ester Silva Mar 16 '21 at 23:58
0

I edited the @Pierre Chevallier, like following to meet my goals.

final_table <- final_table %>%
  left_join(clusters, by = c("Var2" = "name"))

final_table$Cluster[final_table$Cluster==1]<-"A"
final_table$Cluster[final_table$Cluster==2]<-"B"
final_table$Cluster[final_table$Cluster==3]<-"C"
final_table$Cluster[final_table$Cluster==4]<-"D"

bp<-ggplot() +
geom_line(data=final_table, aes(x = Var1, y = value, color = Cluster,linetype=Var2),lwd=1) +
scale_color_manual(values=c('limegreen','blue','black','red')) +
xlab("") +
ylab("Ranks")+
  scale_x_continuous(breaks = c(seq(1,length(Dates),1)), labels = Dates)+ theme(legend.position="none")
Ester Silva
  • 670
  • 6
  • 24