colored Plot with R

Hello Everyone!!

I would Like to plot some data in several colors at the same time,

I meant, I have 300 observations and I would like that, in the plot, the first 100 will be green, the next 50 in Brown and the remaining part in blue (for example).
Is it POSSIBLE?
How can I do it?

I thank you in advance!

Yes, you can create an auxiliar column in your data and use it for color. For example, in ggplot2 you can do something like this:

library(tidyverse)

data = tibble(id = seq(1,100), var = rbinom(n = 100, size = 50, prob = 0.5)) # random data

data = data %>%
mutate(color = c(rep('A', 10), rep('B', 40), rep('C', 50))) # auxiliar column

ggplot(data) +
geom_bar(aes(x = id, y = var, fill = color), stat = 'identity')

1 Like

I'm not sure what kind of plot you would like to make, but here is an example of a bar plot using the data you posted. In this case, you can use scale_fill_identity() and it will use the value of the Color column to determine the fill color.

library(ggplot2)

df <- data.frame(
  Cat = letters[1:18],
  Data = c(0.026842105,0.15857143,
           0.044375,0.069333333,0.029,0.042580644,0.112708332,
           0.212647059,0.064864864,0.043181818,0.0244,0.074594594,
           0.182580646,0.071875,0.079230769,0.032666666,0.019117647,
           0.0064),
  Color = c("black","black","black",
           "black","black","black","red","red","red","red","red",
           "purple","purple","purple","purple","blue","blue",
           "blue")
  )

 ggplot(df, aes(Cat, Data, fill = Color)) +
  geom_col() +
  scale_fill_identity()

Created on 2020-07-28 by the reprex package (v0.3.0)

In base R.

x <- seq(1:300)
y <- rnorm(300)
z <- as.factor(rep(1:3, c(100,50,150))
plot(x,y, col=z) # Note this uses 1=black, 2=red, 3=green

Still I don't get it

any Help??
Please!

library(tidyverse)
y.ma <- data.frame(x=1:800,
                   y=500+cumsum(sample.int(5,size=800,replace = TRUE)-3))

y.ma2 <- mutate(y.ma,
                col = case_when(between(x,1,100) ~ "1-100",
                                between(x,100,150) ~ "100-150",
                                between(x,150,300) ~ "150-300",
                                between(x,300,500) ~ "300-500",
                                TRUE ~ "500+"))

ggplot(data=y.ma2,
       mapping=aes(x=x,y=y,colour=col)) + 
       geom_line() +
       ggtitle("New Scotia") + 
       ylab("Precipitation") +
       xlab("Day") +
       labs(colour = "Key") +
       scale_colour_manual(values=c("1-100"="black",
                        "100-150"="pink",
                        "150-300"="green",
                        "300-500"="yellow",
                        "500+"="red"))
2 Likes

it seems that I cannot replace your base, weird.
like you created a dataframe, but when i replace it with mine, it doesn't work

please say in what way it didnt work.
If there was an error, you should share that.

sorry,
i just sent another one

I'm sorry but this way of transferring example data is inefficient.
If you have the data in object y.ma then please provide the output of

dput(y.ma)

Hopefully,...

June= c(0.534800002276897, 0.0445999999344349, 0.0839999996125698, 
3.38600003242493, 3.00200000882149, 1.43220002770424, 5.53179992198944, 
0, 0.000199999995529652, 0.396999993771315, 13.5630000782013, 
3.76300001323223, 0.21180000051856, 0.0185999995842576, 0, 0.654399992823601, 
1.02599999576807, 9.21259998321533, 0.124599999412894, 0.554599997289479, 
21.6407998847961, 5.32059999465942, 9.76139987945557, 3.70819996774197, 
0.0466000003367662, 2.74400001168251, 0.612200002074242, 10.5995999622345, 
1.31879999756813, 1.33699999220669, 8.6180001115799, 1.22139998555183, 
1.96120001494884, 8.52640001296997, 1.94499997615814, 0, 0.323399998880923, 
1.42739999890327, 0.0519999991357327, 0.155199998691678, 3.81140001296997, 
1.68300001978874, 0.000799999982118607, 0.0678000010177493, 0.119800000451505, 
0.329800004586577, 8.14459998130798, 11.856399936676, 0.551599996685982, 
0.151400001198053, 0.979399993568659, 0.138599999547005, 2.69920001506805, 
4.10520001411438, 0.00379999991506338, 8.10159998893738, 10.99140001297, 
0.564799995422363, 6.21380009651184, 5.86039993286133, 7.12359997749329, 
0.188999999761581, 0.0539999993890524, 0.302800002098083, 0.00379999991506338, 
0.415399999581277, 0.788200015164912, 0.329399998560548, 0.204800003767014, 
1.6697999958694, 6.50440006256103, 35.3081996154785, 0.333799997419119, 
0.782399991378188, 2.66880000591278, 0.0768000003322959, 0.0309999995678663, 
0, 0.0121999998763204, 9.40380001068115, 7.0490000629425, 6.04460003852844, 
11.1856001091003, 1.56539998948574, 1.95879998087883, 0.454200005680323, 
0.465400001816452, 1.00699999213219, 13.8228001785278, 2.20619999170303, 
24.0966002655029, 0.125399998612702, 19.011399936676, 7.71199995040894, 
23.29, 28.6640000534058, 6.64380008220673, 2.80620000481606, 
0.447600000239909, 0.104399999082088, 0, 0.00719999983906746, 
0.694199997708201, 1.94280001282692, 0.355199999064207, 0, 2.79139998912811, 
0.0275999996811152, 0, 7.75300005555153, 1.57639999985695, 0.130999999903142, 
1.22299999594688, 4.29780006408691, 0.00939999986439943, 0.478000002913177, 
0.370800006091595, 32.8191997146606, 1.94779999256134, 1.11079999506474, 
0.653400002121925, 2.0563999825716, 2.52280000686646, 0.22840000256896, 
0.280799997523427, 3.37260001182556, 2.25099994972348, 0.235800001695752, 
2.72059998750687, 3.67780000925064, 0, 0.112999998517334, 24.2859997177124, 
4.08100000619888, 25.8859997653961, 0.733999993950129, 0.000199999995529652, 
5.01280003547668, 4.44040009498596, 0.408399995714426, 0.083399999551475, 
0.230199996158481, 0.000199999995529652, 0.213600005060434, 4.73000000476837, 
2.68820004969835, 1.88260001182556, 0.0225999996438622, 0.946199995242059, 
2.55040001153946, 1.14700001597404, 0, 0.525399999544024, 2.08319998502731, 
5.85079998970032, 3.31540001392364, 1.10799998879433, 0.139400001205504, 
8.77359993934631, 0.580800002217293, 0.00979999978095293, 0, 
0.000799999982118607, 0.753600007146597, 0.0433999997377396, 
0.0467999996244907, 0, 0.00219999995082617, 0.0153999996557832, 
0.506399998664856, 0.00459999989718199, 3.23839994192123, 28.7470001411438, 
15.6725997257233, 0.291399997323751, 29.7171996688843, 1.26040001749992, 
1.59639997005463, 0.00419999990612268, 0.149400001689792, 0.921800008863211, 
0.0940000002458692, 7.36979994773865, 0.684399996101856, 0.0191999998316169, 
0, 0.0231999996677041, 54.2080004882813, 0.531800004541874, 0.160799999497831, 
6.90679999351501, 26.3239999771118, 0.813800013363361, 1.41599999785423, 
0.306400006189942, 0.236400003880262, 0.853400005400181, 0.863599988222122, 
1.45360000133514, 0, 0.723599996939301, 0.113199998773634, 2.88300000429153, 
1.05539999842644, 0.783800005465746, 9.14620004653931, 2.81679998517036, 
23.7500000572205, 18.2665997695923, 3.24600003004074, 0, 0, 0.00339999996125698, 
1.18279999375343, 1.91819999217987, 27.1018000411987, 3.19359995961189, 
0, 0.00519999988377094, 2.69720000982285, 0, 0.0474000011757016, 
2.94060003519058, 11.4839999198914, 14.9016000175476, 2.00179999351501, 
0, 26.117799987793, 7.49699999094009, 4.98720004558563, 1.90779999732971, 
0.407200002670288, 0.173000000827014, 0.00119999997317791, 11.7430000495911, 
43.9789999389648, 0.0622000005841255, 0, 0.00119999997317791, 
0, 3.13979997038841, 16.1606001281738, 2.3613999915123, 0.00499999988824129, 
0.000999999977648258, 6.10580008506775, 0, 0.0586000000685453, 
0.273999993503094, 5.35479998111725, 0.148400001488626, 0.244800002090633, 
19.5712000274658, 0.0193999997153878, 0, 8.45459989547729, 1.04020001292229, 
0.00820000000298023, 8.0383998966217, 0.0456000004336238, 15.2854000282288, 
38.123800239563, 6.18639998912811, 3.07340001225471, 0.301399999111891, 
0.427399995289743, 0.0204000004380941, 10.2884000682831, 2.48080000758171, 
0.196399998888373, 0.534800002276897, 0.0445999999344349, 0.0839999996125698, 
3.38600003242493, 3.00200000882149, 1.43220002770424, 5.53179992198944, 
0, 0.000199999995529652, 0.396999993771315, 13.5630000782013, 
3.76300001323223, 0.21180000051856, 0.0185999995842576, 0, 0.654399992823601, 
1.02599999576807, 9.21259998321533, 0.124599999412894, 0.554599997289479, 
21.6407998847961, 5.32059999465942, 9.76139987945557, 3.70819996774197, 
0.0466000003367662, 2.74400001168251, 0.612200002074242, 10.5995999622345, 
1.31879999756813, 1.33699999220669)

you gave me a vector called June, here is an example of integrating it with the code I previously shared.


library(tidyverse)
y.ma <- data.frame(x=1:300,
                   y=June)

y.ma2 <- mutate(y.ma,
                col = case_when(between(x,1,100) ~ "1-100",
                                between(x,100,150) ~ "100-150",
                                between(x,150,300) ~ "150-300",
                                between(x,300,500) ~ "300-500",
                                TRUE ~ "500+"))

ggplot(data=y.ma2,
       mapping=aes(x=x,y=y,colour=col)) + 
  geom_line() +
  # geom_smooth()+
  ggtitle("New Scotia") + 
  ylab("Precipitation") +
  xlab("Day") +
  labs(colour = "Key") +
  scale_colour_manual(values=c("1-100"="black",
                               "100-150"="pink",
                               "150-300"="green",
                               "300-500"="yellow",
                               "500+"="red"))

Thank you so much!
very helpful

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

thank you for your quick answer.
do you mean something like that?

Data Color
0.026842105 black
0.15857143 black
0.044375 black
0.069333333 black
0.029 black
0.042580644 black
0.112708332 red
0.212647059 red
0.064864864 red
0.043181818 red
0.0244 red
0.074594594 purple
0.182580646 purple
0.071875 purple
0.079230769 purple
0.032666666 blue
0.019117647 blue
0.0064 blue
1 Like

simple plot like this:
plot(y.ma, main="New Scotia", ylab="Precipitation", type="l")
but the database extend to 9 years. (the years don't necessarily have the same size of data )

i want to make that difference by coloring the plot.
i have like 800 observations.
I don't mind to set manually the limit
1 to 100 color A
101 to 140 Color B
141 to 300 Color C
and so on....

here a the current presentation: