gasilmovies.blogg.se

Scatter plot with two sets of data
Scatter plot with two sets of data













  • Using the par() function to set general graphical parameters:.
  • There are many more options that can be tweaked in order to customise your plot and have it look exactly how you want it. This is what is so interesting about the Anscombe Quartet of datasets that we used despite the fact that they look so different when plotted the statistics that describe them are almost identical! Notice that all four lines of best fit are exactly the same! We can only see one straight line because the other three are hidden underneath it.

    scatter plot with two sets of data scatter plot with two sets of data

    Points(anscombe$x4, anscombe$y4, pch = 3, col = 4)Ībline(lm(anscombe$y1 ~ anscombe$x1), col = 1)Ībline(lm(anscombe$y2 ~ anscombe$x2), col = 2)Ībline(lm(anscombe$圓 ~ anscombe$x3), col = 3)Ībline(lm(anscombe$y4 ~ anscombe$x4), col = 4)Ĭ("Dataset 1", "Dataset 2", "Dataset 3", "Dataset 4"), Points(anscombe$x3, anscombe$圓, pch = 2, col = 3) Points(anscombe$x2, anscombe$y2, pch = 1, col = 2) # The additional datasets are plotted using points() Main = "Anscombe's Quartet: All Datasets", # The first dataset is plotted using plot() This is the opposite of the plot(x, y) format which requires that the x-variable comes first. Note that the lm() function uses the tilde notation ( lm(y ~ x)) and so the y-variable must go on the left of the tilde and the x-variable on the right. To add a ‘line of best fit’, use the lm() ( linear model) function then the abline() function (which plots a straight line between a and b). There are many colour palettes available although they need to be ‘loaded’ before you can use them. If you are creating a plot that has multiple colours in it then using palettes is a good idea: instead of wasting time trying to find colours that go well together you can just use a palette and cycle through the colours in that. If you specify a number larger than 8 it will wrap around and start from the beginning again, so colour = 8 will make your symbols grey (the 8th and final colour in the default colour palette) while colour = 9 will make your symbols black (the 1st colour). This is because the default colour palette is: black, red, green3, blue, cyan, magenta, yellow and grey (in that order), and blue is the 4th element of that list. If you specify a number then ggplot will use the corresponding colour from the colour palette that is currently loaded for example, colour = 4 will make your symbols blue.For example, ‘‎#002147’ would make your plot Oxford Blue. Each pair RR, GG, BB is a hexadecimal number (from 00 to FF) that specifies how much red, green and blue is in the colour of your plot symbol, respectively.

    scatter plot with two sets of data

    Use the RGB value of the colour you want by using the format colour = "#RRGGBB".There are 657 colours that can be specified by name in this way, see them all here. Type out the colour’s name, eg colour = "blue" will make the symbols blue.There are three different ways of specifying which colour you want to use: Main = "Anscombe's Quartet: First Dataset", The Anscombe Quartet is an interesting dataset to use because it consists of four plots that have near-identical descriptive statistics, despite looking very different when graphed.Ĭhange the colour of the plot symbols using the col keyword argument: plot( Let’s plot each of them: plot(anscombe$x1, anscombe$y1) Remember that data frames are essentially tables where the columns are vectors, so two columns of a data frames can be plotted against each other very easily! Let’s use one of the pre-loaded data frames that comes packaged with R the Anscombe dataset: print(anscombe) # x1 x2 x3 x4 y1 y2 圓 y4Īs you can see there are four columns of x-values (x1, x2, x3, x4) and four columns of y-values (y1, y2, 圓, y4). If you are coding in R you will mostly be working with data frames.















    Scatter plot with two sets of data