1

I am starting from this ggplot :

library(ggplot2) 
library(reshape2) 

data <- read.delim(textConnection("
Groups Time_1 Time_2 Time_3 Time_4
A 63.8 60.6 65.2 66.6
B 9.4 14.0 11.1 7.5
C 7.4 8.5 6.9 8.6
D 13.9 8.4 7.9 11.4
E 1.4 3.8 5.0 1.5
F 0.2 0.2 0.2 0.2
G 1.8 2.5 1.8 2.7
H 1.0 0.9 0.9 1.1
I 45.0 42.0 49.0 38.0
J 1.0 1.1 0.9 0.5
K 0.1 2.0 6.5 1.0
L 0.5 0.9 0.5 0.2
M 0.2 0.2 0.1 0.3"), sep = " ", header = T)


data_melt <- melt(data, id.var = "Groups")
data_melt$value <- as.numeric(data_melt$value)

ggplot <- ggplot(data=data_melt, aes(x=variable, y=value, group = Groups, color = Groups)) + geom_point(size = 1) + geom_line(size = 1)
ggplot

However, as you can see on the graph, the color code isn't optimal and it's hard to distinguish which color correponds to which groups, even when the curves are well separated:

enter image description here

Unfortunately, I always have between 10 and 13 groups, and I've spent some time looking at R color palettes but usually they're too small.

So I'm looking for ideas about how to have a better color code to improve my graph, any clue would be helpful !

EvenStar69
  • 243
  • 3
  • 15
  • 1
    you can also use [shapes](http://sape.inf.usi.ch/quick-reference/ggplot2/shape) in addition. – Andre Elrico May 28 '18 at 14:31
  • 2
    [This recent post](https://stackoverflow.com/questions/50321000/r-colors-many-distinctive-colors-that-are-still-pretty) doesn't have enough strong answers to flag as duplicate. But I would suggest you read the comments there as well for some cautions against encoding information by that many different colors – camille May 28 '18 at 14:35
  • 1
    You could also use a `geom_text` layer to put the names of the groups on the lines – Jack Brookes May 28 '18 at 14:38
  • 1
    You could try `directlabels` to add labels to the end of each line. Getting many colours that are distinct and colourblind friendly is challenging – Richard Telford May 28 '18 at 14:40
  • 1
    As the human eye and brain can't process more than 4-5 colors in a good and structured way I recomment to add grouping factors and facets or find another solution of plotting the data. – Roman May 28 '18 at 14:42
  • @AndreElrico people often reccomend use the addition of shapes. Is there a good and easy way to get ggplot to use both at the same time. Not as in 20 distinct colors and shapes, but maybe 5 distinct colors and 4 shapes at the same time, preferably used in one legend as not to make any diffrence between symbols or colors. – Rasmus Ø. Pedersen Sep 14 '18 at 13:43

3 Answers3

2

http://colorbrewer2.org has some nice palettes up to 12 colors (set the nature of the data to 'qualitative'), it's easy enough to transfer the hex values into R. You'll have to come up with your own tweak for that 13th value though.

steveLangsford
  • 646
  • 5
  • 9
2

If you have crowded data you can also think of faceting it like so:

ggplot(data=data_melt[!is.na(data_melt$value),], aes(x=variable, y=value, group = Groups, color = Groups)) + 
  geom_point(size = 1) +
  geom_line(size = 1) +
  facet_wrap( ~ Groups) +
  theme(axis.text.x = element_text(angle = 90))

enter image description here

moooh
  • 459
  • 3
  • 10
1

If you use the ggfocus extension you can highlight the most important levels with a few colors and make all the others gray, for example. If you care more about a subgroup of levels only, you can reduce your color scale range without losing the information of the complete data. See the example below:

enter image description here

VFreguglia
  • 2,129
  • 4
  • 14
  • 35