Default colors in ggplot2 for extended legend labels
Default colors in ggplot2 for extended legend labels
I wanted to refer to the question: Force ggplot legend to show all categories when no values are present
I'm in a similar situation, but I would like the colors to be the default.
How should I do this?
ADDED:
I looked more closely and it turned out that, unfortunately, the labels were changed.
Raw data looks like this:
> str(mj)
'data.frame': 4393 obs. of 22 variables:
$ OS_Gatunek : Factor w/ 5 levels "Taraxacum ancistrolobum",..: 1 1 1 1 1 1 1 1 1 1 ...
$ PH_CreateDate : Factor w/ 15 levels "2016-04-06","2016-04-19",..: 2 2 2 2 2 2 2 2 2 2 ...
$ L_Ksztalt : Factor w/ 3 levels "lancetowaty",..: 3 2 3 3 2 2 3 3 2 3 ...
$ L_Symetria : Factor w/ 3 levels "duża","mała",..: 1 3 1 3 2 3 2 2 2 1 ...
$ L_Sfaldowanie : Factor w/ 2 levels "brak","obecne": 1 1 1 2 2 1 1 2 1 1 ...
$ KS_Ksz : Factor w/ 3 levels "hełmiasty","strzałkowaty",..: 2 3 1 1 3 1 1 1 1 1 ...
$ KS_KszWierz : Factor w/ 5 levels "spiczasty","tępo spiczasty",..: 3 1 5 2 2 1 1 2 3 4 ...
$ KS_KszKrGor : Factor w/ 10 levels "esowaty","odwrotnie esowaty",..: 7 7 10 1 7 10 10 10 10 10 ...
$ KS_KszKrDol : Factor w/ 10 levels "esowaty","odwrotnie esowaty",..: 9 7 10 7 7 9 9 10 9 9 ...
$ KS_Zab : Factor w/ 2 levels "brak","obecne": 1 1 1 1 1 1 1 1 1 1 ...
$ KS_TendTworzKlap : Factor w/ 2 levels "brak","obecna": 1 1 1 1 1 1 2 1 1 2 ...
$ KB_Ustawienie : Factor w/ 5 levels "odchylone","odgięte",..: 1 1 1 3 1 1 1 1 1 1 ...
$ KB_Zakonczenie : Factor w/ 5 levels "ostro spiczaste",..: 3 3 2 3 2 2 5 5 3 2 ...
$ KB_KsztKrawGornej: Factor w/ 10 levels "esowaty","odwrotnie esowaty",..: 10 1 10 7 7 10 10 10 10 1 ...
$ KB_KsztKrawDolnej: Factor w/ 10 levels "esowaty","odwrotnie esowaty",..: 9 7 10 7 2 10 9 2 9 1 ...
$ KB_ZabkKrGornej : Factor w/ 2 levels "brak","obecne": 2 1 1 1 1 1 1 2 1 1 ...
$ KB_ZabkKrDolnej : Factor w/ 2 levels "brak","obecne": 1 2 1 1 1 1 1 1 1 1 ...
$ KB_TendDoTwKlap : Factor w/ 2 levels "brak","obecna": 1 1 1 1 1 1 1 1 1 1 ...
$ I_Ksztalt : Factor w/ 3 levels "całe","postrzępione",..: 1 1 1 2 1 1 1 2 1 2 ...
$ I_Wyw : Factor w/ 2 levels "brak","obecne": 2 2 2 2 2 2 2 2 2 2 ...
$ I_SmolWyb : Factor w/ 2 levels "brak","obecne": 2 2 2 1 2 1 2 1 2 2 ...
$ N_Zabarwienie : Factor w/ 5 levels "cały czerwonawy lub różowy",..: 5 4 5 5 1 1 5 1 5 1 ...
And the code for the sample pie chart is as follows (changed from: How to create a pie chart with percentage labels using ggplot2?):
> data <- mj %>%
+ group_by(N_Zabarwienie) %>%
+ count() %>%
+ ungroup() %>%
+ mutate(per=`n`/sum(`n`)) %>%
+ arrange(desc(N_Zabarwienie))
> data$label <- scales::percent(data$per)
> ggplot(data=data)+
+ geom_bar(aes(x="", y=per, fill=N_Zabarwienie), stat="identity", width = 1)+
+ coord_polar("y", start=0)+
+ theme_void()+
+ geom_text(aes(x=1.3, y = cumsum(per) - per/2, label=label))
The chart looks like this:
Pie chart 1 - first code - all species
If I change the code as Benjamin Schlegel suggested:
> data <- mj %>%
+ group_by(N_Zabarwienie) %>%
+ count() %>%
+ ungroup() %>%
+ mutate(per=`n`/sum(`n`)) %>%
+ arrange(desc(N_Zabarwienie))
> data$label <- scales::percent(data$per)
> ggplot(data=data)+
+ geom_bar(aes(x="", y=per, fill=N_Zabarwienie), stat="identity", width = 1)+
+ coord_polar("y", start=0)+
+ theme_void()+
+ geom_text(aes(x=1.3, y = cumsum(per) - per/2, label=label)) +
+ scale_fill_discrete(labels = c("zielony do bladego bez czerwieni", "zewnątrz blady", "w środku różowy", "cały czerwonawy lub różowy", "błyszcząco purpurowy"), drop = FALSE)
This chart looks like:
Pie chart 2 - second code - all species
In the first chart, the most common is "cały czerwonawy lub różowy", which means all reddish or pink (it is the color of the petiole), and in the second graph - "zielony do bladego bez czerwieni" which means green to pale without red. The difference is diametrical.
The first version is correct.
> summary(mj$N_Zabarwienie)
cały czerwonawy lub różowy na zewnątrz blady
2943 1
purpurowy z połyskiem w środku różowy
1 716
zielony do bladego bez czerwieni
732
As I change the scope of data (only for one of the species), it shows only part of the legend (the one that is currently in use).
Below is an example chart (first code) for the selected species (Taraxacum ancistrolobum).
Pie chart 3 - first code - Taraxacum ancistrolobum
And this is the same set of data, but generated from the second code.
Pie chart 4 - second code - Taraxacum ancistrolobum
And here also the first version is correct.
> summary(jta$N_Zabarwienie)
cały czerwonawy lub różowy na zewnątrz blady
163 0
purpurowy z połyskiem w środku różowy
0 85
zielony do bladego bez czerwieni
163
I would like to put charts made for different species next to each other and then compare them. A uniform legend is essential for it.
So I repeat the question:
how to make the same legend on all charts, despite different data ranges, but with default colors?
scale_fill_manual
Thank you, but which command should I use?
– Olga Świder
Jun 28 at 9:23
1 Answer
1
scale_fill_manual
, which is used in the question you liked to, is used, when the colors are getting defined. If you want the default colors, you can use scale_fill_discrete
if your variable is discrete or scale_fill_continuous
if your variable is numeric.
scale_fill_manual
scale_fill_discrete
scale_fill_continuous
The example from the other question with specifying the colos would be:
ggplot(data = dat, aes(x = Row, y = Col)) +
geom_tile(aes(fill = Y1), color = "black") +
scale_fill_discrete(labels = c("cat1", "cat2", "cat3", "cat4", "cat5", "cat6", "cat7", "cat8"),
drop = FALSE)
It works great. Thank you very much :-).
– Olga Świder
Jun 28 at 9:45
You are welcome. Can you please upvote the question to show that it was useful for you?
– Benjamin Schlegel
Jun 28 at 9:49
I am new here and I do not know what is going on. But if you explain: for the hero - everything ;-).
– Olga Świder
Jun 28 at 9:52
Ah, I see, you need 15 reputation points to be able to upvote, but you only have 8.
– Benjamin Schlegel
Jun 28 at 9:54
Thanks for the clarification. And for the answer.
– Olga Świder
Jun 28 at 9:57
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Do not use
scale_fill_manual
.– Roman Luštrik
Jun 28 at 9:20