R Plotting.pdf

Full Transcript

R Plotting Plot The plot() function is used to draw points (markers) in a diagram. The function takes parameters for specifying points in the diagram. Parameter 1 specifies points on the x-axis. Parameter 2 specifies points on the y-axis. At its simplest, you ca...

R Plotting Plot The plot() function is used to draw points (markers) in a diagram. The function takes parameters for specifying points in the diagram. Parameter 1 specifies points on the x-axis. Parameter 2 specifies points on the y-axis. At its simplest, you can use the plot() function to plot two numbers against each other: Example # Draw one point in the diagram, at position 1 and 3 plot(2, 7) #Draw two points in the diagram, one at position (3, 5) and one in position (5, 7): plot(c(3, 5), c(5, 7)) For better organization, when you have many values, it is better to use variables: If you want to draw dots in a sequence, on both the x-axis and the y-axis, use the : operator: Draw a Line The plot() function also takes a type parameter with the value l to draw a line to connect all the points in the diagram: Plot Labels The plot() function also accept other parameters, such as main, xlab and ylab if you want to customize the graph with a main title and different labels for the x and y-axis: Graph Appearance There are many other parameters you can use to change the appearance of the points. Colours Use col="color" to add a colour to the points: Size Use cex=number to change the size of the points (1 is default, while 0.5 means 50% smaller, and 2 means 100% larger): Point Shape Use pch with a value from 0 to 25 to change the point shape format: Line Width To change the width of the line, use the lwd parameter (1 is default, while 0.5 means 50% smaller, and 2 means 100% larger): Line Styles The line is solid by default. Use the lty parameter with a value from 0 to 6 to specify the line format. For example, lty=3 will display a dotted line instead of a solid line: R Pie Charts A pie chart is a circular graphical view of data. Use the pie() function to draw pie charts: Example Explained As you can see the pie chart draws one pie for each value in the vector (in this case 10, 20, 30, 40). By default, the plotting of the first pie starts from the x-axis and move counter clockwise. Start Angle You can change the start angle of the pie chart with the init.angle parameter. The value of init.angle is defined with angle in degrees, where default angle is 0. Labels and Header Use the label parameter to add a label to the pie chart, and use the main parameter to add a header: Colours You can add a colour to each pie with the col parameter: Legend To add a list of explanation for each pie, use the legend() function: Bar Charts A bar chart uses rectangular bars to visualize data. Bar charts can be displayed horizontally or vertically. The height or length of the bars are proportional to the values they represent. Use the barplot() function to draw a vertical bar chart: Bar Colour Use the col parameter to change the colour of the bars: Horizontal Bars If you want the bars to be displayed horizontally instead of vertically, use horiz=TRUE:

Use Quizgecko on...
Browser
Browser