Home > Blockchain >  How to make a gradated rainbow colour palette figure?
How to make a gradated rainbow colour palette figure?

Time:07-17

I am trying to make a diagram similar to the photos I have attached below in R. I will be using categorical data instead of numerical as show in the photo. Firstly, what is this type of diagram called? Secondly, is there a function that can help me to recreate a diagram similar to this one in R? Would you recommend any package, or is this doable with base R?

enter image description here

CodePudding user response:

You can certainly reproduce the plot. It's not clear how this could be achieved with your own data, since you haven't shared it, but I would probably reproduce it like this:

df <- data.frame(x = seq(-0.5, 14.5, length.out = 1000))

library(ggplot2)

ggplot(df, aes(x = x, color = x))  
  geom_segment(aes(xend = x, y = 1, yend = 2))  
  scale_color_gradientn(colours = c("#f794a7", "#f6a79b", "#f4b896",
                                    "#f6c392", "#f4cc95", "#ecd798",
                                    "#d5e299", "#b9ef96", "#a7f0af",
                                    "#9de9d5", "#8fe2f8", "#93ccf6",
                                    "#90b9f6", "#a1abf3", "#b39ef7",
                                    "#bf8ff3"))  
  geom_vline(xintercept = 0:13   0.5, colour = "white")  
  geom_text(data = data.frame(x = 0:14), aes(y = 1.5, label = x), 
            color = "white", size = 6)  
  geom_text(data = data.frame(x = c(1, 7, 13), 
                              lab = c("acid", "neutral", "alkali")),
            aes(y = 0.8, label = lab), size = 8, color = "black")  
  ylim(c(-1, 4))  
  annotate(geom = "text", x = 7, y = 2.5, label = "The pH Scale", size = 10)  
  theme_void()  
  theme(legend.position = "none")

enter image description here

CodePudding user response:

Maybe you want something like this:

library(dplyr)
library(ggplot2)
df <- data.frame(pH = c(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14),
                 x = c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1))

df %>%
  ggplot(aes(x = "", y = x, fill = as.factor(pH)))  
  geom_col()  
  geom_text(aes(label = pH),
            position = position_stack(vjust = 0.5))  
  scale_fill_manual(values=rainbow(15))  
  labs(x = "", y = "")  
  theme_minimal()  
  ggtitle("The pH Scale")  
  annotate("text", y = c(1, 7, 14), x = 0.5, label = c("acid", "neutral", "alkali"), size = 3)  
  coord_flip()  
  theme(axis.ticks = element_blank(), 
        axis.text.x = element_blank(),
        line = element_blank(),
        plot.title = element_text(hjust = 0.5),
        legend.position = "none")

Created on 2022-07-16 by the reprex package (v2.0.1)

  •  Tags:  
  • r
  • Related