Plot with several variables

Good Morning,

I am tying to plot some data for my presentation.

Markov.Printemps<- c(0.3864, 0.57244352, 0.70207864, 0.7924084, 0.855350173, 0.899208, 0.929768135, 0.951062436, 0.965900306, 0.976239333, 0.983443567, 0.988463478, 0.991961351, 0.99439867, 0.996096993)

Markov.Ete<- c(0.3765, 0.53829825, 0.658109854, 0.746830347, 0.812527872, 0.861176889, 0.897201486, 0.923877701, 0.943631437, 0.958259079, 0.969090848, 0.977111773, 0.983051268, 0.987449464, 0.990706328)

Markov.Automne<-c(0.6853,0.82870879,0.906766194,0.94925284,0.972378321,0.98496552,0.991816732,0.995545847,0.997575605,0.998680402,0.999281743,0.999609053,0.999787207,0.999884177,0.999936958)

Markov.Hiver<-c(0.4012,0.57820528,0.702887799,0.790714166,0.852579058, 0.896156689,0.926852772,0.948475092,0.963705855,0.974434404,0.981991594,0.987314879,0.991064601,0.993705905, 0.995566439)

Topil.Hiver<-c(0.4012,0.64143856,0.78529341,0.871433694,0.923014496,0.95390108,0.972395967,0.983470705,0.990102258,0.994073232,0.996451051,0.99787489,0.998727484,0.999238017,0.999543725)
Topil.Ete<-c(0.3765,0.61124775, 0.757612972, 0.848871688, 0.905771498, 0.941248529, 0.963368458, 0.977160233, 0.985759405,0.991120989,0.994463937,0.996548265,0.997847843,0.99865813,0.999163344)
Topil.Automne<-c(0.4557,0.70373751,0.838744327,0.912228537,0.952225993,0.973996608,0.985846354,0.99229617,0.995806805,0.997717644,0.998757714,0.999323824,0.999631957,0.999799674,0.999890963)
Topil.Printemps<-c(0.3864,0.51446976,0.661682529,0.764260386,0.835736637,0.885541289,0.92024517,0.944426834,0.61276618,0.973017548,0.981198627,0.986899203,0.990871365,0.993639167,0.995567772)

the markov data will be the curves and Topil data will be points over/aound the curves

I wanna be clear, I give you a picture of how I exactly want it to be.

thank you in advance for your help

Hello,

Here is an example of your data (thanks for posting it in a copy-paste format!)

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

myData = data.frame(
  Markov.Printemps = c(0.3864, 0.57244352, 0.70207864, 0.7924084, 0.855350173, 0.899208, 0.929768135, 0.951062436, 0.965900306, 0.976239333, 0.983443567, 0.988463478, 0.991961351, 0.99439867, 0.996096993),
  Markov.Ete = c(0.3765, 0.53829825, 0.658109854, 0.746830347, 0.812527872, 0.861176889, 0.897201486, 0.923877701, 0.943631437, 0.958259079, 0.969090848, 0.977111773, 0.983051268, 0.987449464, 0.990706328),
  Markov.Automne = c(0.6853,0.82870879,0.906766194,0.94925284,0.972378321,0.98496552,0.991816732,0.995545847,0.997575605,0.998680402,0.999281743,0.999609053,0.999787207,0.999884177,0.999936958),
  Markov.Hiver = c(0.4012,0.57820528,0.702887799,0.790714166,0.852579058, 0.896156689,0.926852772,0.948475092,0.963705855,0.974434404,0.981991594,0.987314879,0.991064601,0.993705905, 0.995566439),
  Topil.Hiver = c(0.4012,0.64143856,0.78529341,0.871433694,0.923014496,0.95390108,0.972395967,0.983470705,0.990102258,0.994073232,0.996451051,0.99787489,0.998727484,0.999238017,0.999543725),
  Topil.Ete = c(0.3765,0.61124775, 0.757612972, 0.848871688, 0.905771498, 0.941248529, 0.963368458, 0.977160233, 0.985759405,0.991120989,0.994463937,0.996548265,0.997847843,0.99865813,0.999163344),
  Topil.Automne = c(0.4557,0.70373751,0.838744327,0.912228537,0.952225993,0.973996608,0.985846354,0.99229617,0.995806805,0.997717644,0.998757714,0.999323824,0.999631957,0.999799674,0.999890963),
  Topil.Printemps = c(0.3864,0.51446976,0.661682529,0.764260386,0.835736637,0.885541289,0.92024517,0.944426834,0.61276618,0.973017548,0.981198627,0.986899203,0.990871365,0.993639167,0.995567772)
)
head(myData)
  Markov.Printemps Markov.Ete Markov.Automne Markov.Hiver Topil.Hiver Topil.Ete Topil.Automne Topil.Printemps
1        0.3864000  0.3765000      0.6853000    0.4012000   0.4012000 0.3765000     0.4557000       0.3864000
2        0.5724435  0.5382982      0.8287088    0.5782053   0.6414386 0.6112477     0.7037375       0.5144698
3        0.7020786  0.6581099      0.9067662    0.7028878   0.7852934 0.7576130     0.8387443       0.6616825
4        0.7924084  0.7468303      0.9492528    0.7907142   0.8714337 0.8488717     0.9122285       0.7642604
5        0.8553502  0.8125279      0.9723783    0.8525791   0.9230145 0.9057715     0.9522260       0.8357366
6        0.8992080  0.8611769      0.9849655    0.8961567   0.9539011 0.9412485     0.9739966       0.8855413

#Transform
myData = myData %>% mutate(x = 1:n()) %>% pivot_longer(-x, names_to = "model", values_to = "y")
head(myData)
# A tibble: 6 x 3
      x model                y
  <int> <chr>            <dbl>
1     1 Markov.Printemps 0.386
2     1 Markov.Ete       0.376
3     1 Markov.Automne   0.685
4     1 Markov.Hiver     0.401
5     1 Topil.Hiver      0.401
6     1 Topil.Ete        0.376

#Plot
ggplot(myData, aes(x = x, y = y, color = model)) + geom_line()

Created on 2020-08-11 by the reprex package (v0.3.0)

EXPLANATION
First I created a data frame from all your data by just pasting all vectors together (swapping the <- for =)

Next we add an index for each value (will be come the x-axis) assuming the data is in correct order. This is done my the mutate(x = 1:n()).

The big trick here is to use the pivot_longer function to transform the data from wide to long. We specify that the x-column will be kept, but the other ones are transformed into long format. I renamed the new name and value to model and y respectively, but you can change that if you like.

All that rests now is to use ggplot to plot the new data. By setting the color to model, it will split the data based on the variable given (in our case model). If you don't want different colours, replace it with groups like so: ggplot(myData, aes(x = x, y = y, group = model)) + geom_line()

Hope this helps,
PJ

Thank you for you help.
Did you see the picture in my message? (maybe it wasn't uploaded)

  1. but only the markov's data will be curves
  2. the name of each curve should be on the plot
  3. the topil's data will be points/cross/triangle/circle, not curves

the plot in the picture is what i want, with your colors and your background indeed

thank you in advance

Hello,

Ok here you go, this is all the code you should need. You can edit different parts to tweak. Know that ggplot can edit a lot more stuff, so read about it online if you like to alter certain aspects of the graph.

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

Markov = data.frame(
  Printemps = c(0.3864, 0.57244352, 0.70207864, 0.7924084, 0.855350173, 0.899208, 0.929768135, 0.951062436, 0.965900306, 0.976239333, 0.983443567, 0.988463478, 0.991961351, 0.99439867, 0.996096993),
  Ete = c(0.3765, 0.53829825, 0.658109854, 0.746830347, 0.812527872, 0.861176889, 0.897201486, 0.923877701, 0.943631437, 0.958259079, 0.969090848, 0.977111773, 0.983051268, 0.987449464, 0.990706328),
  Automne = c(0.6853,0.82870879,0.906766194,0.94925284,0.972378321,0.98496552,0.991816732,0.995545847,0.997575605,0.998680402,0.999281743,0.999609053,0.999787207,0.999884177,0.999936958),
  Hiver = c(0.4012,0.57820528,0.702887799,0.790714166,0.852579058, 0.896156689,0.926852772,0.948475092,0.963705855,0.974434404,0.981991594,0.987314879,0.991064601,0.993705905, 0.995566439)
)
Topil = data.frame(
  Hiver = c(0.4012,0.64143856,0.78529341,0.871433694,0.923014496,0.95390108,0.972395967,0.983470705,0.990102258,0.994073232,0.996451051,0.99787489,0.998727484,0.999238017,0.999543725),
  Ete = c(0.3765,0.61124775, 0.757612972, 0.848871688, 0.905771498, 0.941248529, 0.963368458, 0.977160233, 0.985759405,0.991120989,0.994463937,0.996548265,0.997847843,0.99865813,0.999163344),
  Automne = c(0.4557,0.70373751,0.838744327,0.912228537,0.952225993,0.973996608,0.985846354,0.99229617,0.995806805,0.997717644,0.998757714,0.999323824,0.999631957,0.999799674,0.999890963),
  Printemps = c(0.3864,0.51446976,0.661682529,0.764260386,0.835736637,0.885541289,0.92024517,0.944426834,0.61276618,0.973017548,0.981198627,0.986899203,0.990871365,0.993639167,0.995567772)
)

#Edit the values to best place the labels
labels = data.frame(
  label = c("Printemps", "Ete", "Automne", "Hiver"),
  x = c(4, 7, 7, 5),
  y = c(0.7924084, 0.88, 1.01, 0.852579058)
)

#Transform
Markov = Markov %>% mutate(x = 1:n()) %>% pivot_longer(-x, names_to = "Markov", values_to = "y")
Topil = Topil %>% mutate(x = 1:n()) %>% pivot_longer(-x, names_to = "Topil", values_to = "y")


#Plot
ggplot() + 
  #Markov lines
  geom_line(data = Markov, aes(x = x, y = y, color = Markov), inherit.aes = F) +
  geom_text(data = labels, aes(x = x, y = y, label = label, color = label), inherit.aes = F) +
  #Topil points
  geom_point(data = Topil, aes(x = x, y = y, shape = Topil, color = Topil), inherit.aes = F) +
  guides(color = FALSE) + #hide color legend
  labs(x = "X -axis", y = "Y-axis", title = "Tile") + 
  theme( #Edit the background and lines
    panel.background = element_rect(fill = "white"),
    panel.grid.major = element_line(size = 0.5, linetype = 'solid', colour = "gray"),
    panel.grid.minor = element_line(size = 0.5, linetype = 'dotted', colour = "gray"))

Created on 2020-08-11 by the reprex package (v0.3.0)

PJ

Thank you so much.

What if i wanted the curves to be bold? and a little wider?

Hi @MoLo,
ggplot2 is pretty powerful, and the types of formatting tweaks you're asking for are definitely possible, it will just require a little reading.

To change what the curves look like, e.g. their width, you'll want to take a look at some of the parameters to geom_line(). There are some examples here: http://ggplot.yhathq.com/docs/geom_line.html, or you can check out ?geom_line() in RStudio.

As you can see from that example, there are lots of possible customizations!

1 Like

it's very helpful. thank you

library(ggplot2)
library(data.table)
Markov = data.table(
  Printemps = c(0.3864, 0.57244352, 0.70207864, 0.7924084, 0.855350173, 0.899208, 0.929768135, 0.951062436, 0.965900306, 0.976239333, 0.983443567, 0.988463478, 0.991961351, 0.99439867, 0.996096993),
  Ete = c(0.3765, 0.53829825, 0.658109854, 0.746830347, 0.812527872, 0.861176889, 0.897201486, 0.923877701, 0.943631437, 0.958259079, 0.969090848, 0.977111773, 0.983051268, 0.987449464, 0.990706328),
  Automne = c(0.6853,0.82870879,0.906766194,0.94925284,0.972378321,0.98496552,0.991816732,0.995545847,0.997575605,0.998680402,0.999281743,0.999609053,0.999787207,0.999884177,0.999936958),
  Hiver = c(0.4012,0.57820528,0.702887799,0.790714166,0.852579058, 0.896156689,0.926852772,0.948475092,0.963705855,0.974434404,0.981991594,0.987314879,0.991064601,0.993705905, 0.995566439)
)
Topil = data.table(
  Hiver = c(0.4012,0.64143856,0.78529341,0.871433694,0.923014496,0.95390108,0.972395967,0.983470705,0.990102258,0.994073232,0.996451051,0.99787489,0.998727484,0.999238017,0.999543725),
  Ete = c(0.3765,0.61124775, 0.757612972, 0.848871688, 0.905771498, 0.941248529, 0.963368458, 0.977160233, 0.985759405,0.991120989,0.994463937,0.996548265,0.997847843,0.99865813,0.999163344),
  Automne = c(0.4557,0.70373751,0.838744327,0.912228537,0.952225993,0.973996608,0.985846354,0.99229617,0.995806805,0.997717644,0.998757714,0.999323824,0.999631957,0.999799674,0.999890963),
  Printemps = c(0.3864,0.51446976,0.661682529,0.764260386,0.835736637,0.885541289,0.92024517,0.944426834,0.61276618,0.973017548,0.981198627,0.986899203,0.990871365,0.993639167,0.995567772)
)

#Edit the values to best place the labels
labels = data.table(
  label = c("Printemps", "Ete", "Automne", "Hiver"),
  x = c(4, 7, 7, 5),
  y = c(0.7924084, 0.88, 1.01, 0.852579058)
)

#Transform  Markov
Markov[,x:=.I]
Markov=melt(Markov,id.var="x",variable.name="Markov",value.name="y")
print(Markov,topn = 6)
#Transform Topil 
Topil[,x:=.I]
Topil=melt(Topil,id.var="x",variable.name="Topil",value.name="y")
print(Topil,topn = 6)

#Plot
ggplot() + 
  #Markov lines
  Markov[,geom_line(data = .SD, aes(x = x, y = y, color = Markov), inherit.aes = F),] +
  labels[,geom_text(data = .SD, aes(x = x, y = y, label = label, color = label), inherit.aes = F),] +
  #Topil points
  Topil[,geom_point(data = .SD, aes(x = x, y = y, shape = Topil, color = Topil), inherit.aes = F),] +
  guides(color = FALSE) + #hide color legend
  labs(x = "X -axis", y = "Y-axis", title = "Tile") + 
  theme( #Edit the background and lines
    panel.background = element_rect(fill = "white"),
    panel.grid.major = element_line(size = 0.5, linetype = 'solid', colour = "gray"),
    panel.grid.minor = element_line(size = 0.5, linetype = 'dotted', colour = "gray"))

Thank you.

but I don't understad this part:

x = c(4, 7, 7, 5),
y = c(0.7924084, 0.88, 1.01, 0.852579058)
)
What does it stand for?

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