Coloured table according to %

Hi everyone!
I have a table with different numbers expressed in percentage which I would like to show in another map just with colours (like 100%=green and 0%=red), is it possible?
Thank you!
valentina

your title asks about colouring a a table?
your question body seems to ask about colouring a map ?
If you can clarify your question you will receive better responses.

perhaps you can provide some code for what you have so far, and point to what you have tried.
Please have a look at this guide, to see how to create a reprex:

Thanks.

This is what I did so far:

# example input
df1 <- read.table(text = "
Gene	R01_val	R02_val	R03_val	R04_val	R05_val	R06_val	R07_val	R08_val	R09_val	R10_val
1	92	83	69	65	80	79	79	72	89	83
2	0	61	45	79	61	63	76	59	59	84
3	84	69	74	60	75	91	80	79	69	89
4	82	72	48	67	81	89	76	85	86	74
5	33	51	0	65	65	69	69	62	57	74
6	75	0	83	82	85	85	73	84	81	94
7	58	82	67	72	68	75	71	76	64	86
8	37	63	57	57	54	74	66	78	71	79
9	52	62	63	72	47	70	82	80	43	77
10	62	55	0	69	71	76	84	67	82	80
11	45	63	58	55	73	0	86	66	52	78
12	67	72	65	54	55	62	63	57	71	59
13	74	84	59	70	80	92	81	89	66	69
14	75	82	66	73	72	90	83	91	78	89
15	50	66	53	68	69	55	59	74	66	76
16	61	77	69	75	91	89	84	82	66	83
17	0	86	59	76	74	79	82	70	77	0
18	49	55	77	39	31	76	83	27	74	65
19	75	81	84	82	94	92	86	79	82	0
20	79	73	75	83	89	90	91	71	70	92
21	78	80	77	64	76	83	65	82	85	90
22	80	78	70	78	89	91	76	52	86	76
23	70	62	79	66	76	84	85	74	68	72
24	52	47	59	43	63	61	66	66	86	88
25	81	82	87	77	92	92	91	82	82	96
26	38	66	57	70	76	90	81	60	71	0
27	39	68	45	70	67	58	46	50	59	0
28	72	67	65	50	64	64	60	74	64	89
29	50	52	59	43	83	62	58	60	77	82
30	59	75	68	76	69	77	83	82	84	94
31	91	81	74	79	83	80	79	65	85	0
32	68	69	64	74	63	69	85	81	81	88
33	33	56	45	64	61	0	30	39	40	67
34	71	80	87	76	81	85	71	84	69	86
35	70	64	75	50	53	71	65	0	81	76
36	73	88	71	62	87	85	83	84	83	86
37	67	83	60	71	62	77	72	60	69	84
38	57	51	38	50	71	35	41	59	67	93
39	62	0	69	63	83	49	86	77	76	94
40	77	46	56	56	87	83	69	74	81	85
41	67	45	60	61	78	55	66	54	70	91
42	81	62	68	81	77	82	75	74	70	83
43	72	85	84	53	80	87	88	62	76	79
44	85	55	47	73	90	92	84	73	84	92
45	68	75	68	62	86	84	82	75	80	78
46	52	70	33	48	83	65	83	62	77	61
47	77	65	54	72	72	76	70	69	68	56
48	68	78	79	70	75	77	79	75	66	0
49	66	42	60	87	64	75	80	77	77	89
50	67	60	58	57	76	75	75	64	51	88
51	62	87	76	84	88	88	81	90	39	65
52	77	79	90	67	0	65	0	78	0	80
53	80	67	69	72	92	85	91	90	92	95
54	58	87	74	71	80	76	62	84	75	92
55	88	87	79	72	88	87	86	74	74	58



                  
", header = TRUE)


library(ggplot2)
library(dplyr)
library(tidyr)

plotDat <- gather(df1, key = "Gene2", value = "value", -Gene)

ggplot(plotDat, aes(Gene2, Gene, col = value, fill = value, label = value)) +
  geom_tile() +
  geom_text(col = "black") +
  theme_minimal() +
  scale_fill_gradient2(low = "red", mid = "yellow", high = "green") +
  scale_color_gradient2(low = "red", mid = "yellow", high = "green")

But I want that the green colour fill the 100%, not the max value as it's so far.

Add limits to the scales like this:

ggplot(plotDat, aes(Gene2, Gene, col = value, fill = value, label = value)) +
  geom_tile() +
  geom_text(col = "black") +
  theme_minimal() +
  scale_fill_gradient2(low = "red", mid = "yellow", high = "green", limits=c(0,100), midpoint=50) +
  scale_color_gradient2(low = "red", mid = "yellow", high = "green", limits=c(0,100), midpoint=50)

Thank you!!! It works :smiley:

What if I would substitute the 0% with N/A, is there a way to colour differently (like blue) only the cases with "N/A"?

Yes, a little modification is needed to the data and you need to specify the na colour.

df1 <- read.table(text = "
Gene    R01_val R02_val R03_val R04_val R05_val R06_val R07_val R08_val R09_val R10_val
1   92  83  69  65  80  79  79  72  89  83
2   0   61  45  79  61  63  76  59  59  84
3   84  69  74  60  75  91  80  79  69  89
4   82  72  48  67  81  89  76  85  86  74
5   33  51  0   65  65  69  69  62  57  74
6   75  0   83  82  85  85  73  84  81  94
7   58  82  67  72  68  75  71  76  64  86
8   37  63  57  57  54  74  66  78  71  79
9   52  62  63  72  47  70  82  80  43  77
10  62  55  0   69  71  76  84  67  82  80
11  45  63  58  55  73  0   86  66  52  78
12  67  72  65  54  55  62  63  57  71  59
13  74  84  59  70  80  92  81  89  66  69
14  75  82  66  73  72  90  83  91  78  89
15  50  66  53  68  69  55  59  74  66  76
16  61  77  69  75  91  89  84  82  66  83
17  0   86  59  76  74  79  82  70  77  0
18  49  55  77  39  31  76  83  27  74  65
19  75  81  84  82  94  92  86  79  82  0
20  79  73  75  83  89  90  91  71  70  92
21  78  80  77  64  76  83  65  82  85  90
22  80  78  70  78  89  91  76  52  86  76
23  70  62  79  66  76  84  85  74  68  72
24  52  47  59  43  63  61  66  66  86  88
25  81  82  87  77  92  92  91  82  82  96
26  38  66  57  70  76  90  81  60  71  0
27  39  68  45  70  67  58  46  50  59  0
28  72  67  65  50  64  64  60  74  64  89
29  50  52  59  43  83  62  58  60  77  82
30  59  75  68  76  69  77  83  82  84  94
31  91  81  74  79  83  80  79  65  85  0
32  68  69  64  74  63  69  85  81  81  88
33  33  56  45  64  61  0   30  39  40  67
34  71  80  87  76  81  85  71  84  69  86
35  70  64  75  50  53  71  65  0   81  76
36  73  88  71  62  87  85  83  84  83  86
37  67  83  60  71  62  77  72  60  69  84
38  57  51  38  50  71  35  41  59  67  93
39  62  0   69  63  83  49  86  77  76  94
40  77  46  56  56  87  83  69  74  81  85
41  67  45  60  61  78  55  66  54  70  91
42  81  62  68  81  77  82  75  74  70  83
43  72  85  84  53  80  87  88  62  76  79
44  85  55  47  73  90  92  84  73  84  92
45  68  75  68  62  86  84  82  75  80  78
46  52  70  33  48  83  65  83  62  77  61
47  77  65  54  72  72  76  70  69  68  56
48  68  78  79  70  75  77  79  75  66  0
49  66  42  60  87  64  75  80  77  77  89
50  67  60  58  57  76  75  75  64  51  88
51  62  87  76  84  88  88  81  90  39  65
52  77  79  90  67  0   65  0   78  0   80
53  80  67  69  72  92  85  91  90  92  95
54  58  87  74  71  80  76  62  84  75  92
55  88  87  79  72  88  87  86  74  74  58
", header = TRUE)


library(ggplot2)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(tidyr)

plotDat <- gather(df1, key = "Gene2", value = "value", -Gene) %>%
   mutate(value2=na_if(value, 0))

summary(plotDat)
#>       Gene       Gene2               value           value2     
#>  Min.   : 1   Length:550         Min.   : 0.00   Min.   :27.00  
#>  1st Qu.:14   Class :character   1st Qu.:62.00   1st Qu.:64.00  
#>  Median :28   Mode  :character   Median :73.00   Median :74.00  
#>  Mean   :28                      Mean   :69.46   Mean   :71.81  
#>  3rd Qu.:42                      3rd Qu.:82.00   3rd Qu.:82.00  
#>  Max.   :55                      Max.   :96.00   Max.   :96.00  
#>                                                  NA's   :18

ggplot(plotDat, aes(Gene2, Gene, col = value2, fill = value2, label = value)) +
   geom_tile() +
   geom_text(col = "black") +
   theme_minimal() +
   scale_fill_gradient2(low = "red", mid = "yellow", high = "green", limits=c(0,100), midpoint=50, na.value="blue") +
   scale_color_gradient2(low = "red", mid = "yellow", high = "green", limits=c(0,100), midpoint=50, na.value="blue")

Created on 2021-07-01 by the reprex package (v2.0.0)

Ok, thank you so much!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.