EvenStar69
EvenStar69

Reputation: 343

beautiful ggplot with more than 10 colors

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 !

Upvotes: 1

Views: 2823

Answers (3)

VFreguglia
VFreguglia

Reputation: 2311

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

Upvotes: 1

moooh
moooh

Reputation: 469

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

Upvotes: 2

steveLangsford
steveLangsford

Reputation: 646

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.

Upvotes: 2

Related Questions