R language-Graphics First order

Source: Internet
Author: User
Tags dashed line

In this section, the main purpose is to use the R language to make simple graphics

Case 1: Making a relationship between WT and MPG

1 Attach (Mtcars) 2 plot (Wt,mpg) 3 abline (LM (mpg~wt))4 title ('Regression of mpg on Weight'  )5 Detach (Mtcars)

1. Data sets are bound
2. Open the graphics window and create a scatter plot
3. Added a fit curve
4. Add a title to the graphic
5. Unbind

You can also put the resulting picture in a PDF

1 PDF ('mygraph.pdf')2Attach (mtcars)3 plot (Wt,mpg) 4 abline (LM (mpg~wt))5 title ('Regression of mpg on Weight'  )6Detach (mtcars)7 Dev.off ()

Case 2 analysis of the patient's response to the two types of drugs

1. Preparing the data

1 dose <-C (20,30,40,45,60)2 druga <-C (16,20,27,40,60)3 DRUGB < -C (15,18,25,31,40)4 #描述药物A的剂量和病人的响应 5 plot (dose,druga,type='b' )

2. You can use the graphic parameters to modify the style of the above picture

1 opar <-par (no.readonly = T)2 par (lty=2,pch=17)3 plot (Dose,druga, Type='b')4 par (OPAR)

1. Copy the parameter settings of the current drawing
2. Change the line type to dashed, and the default breaks to triangles
3. Draw the graphic and restore the original setting

1 plot (dose,druga,type='b', lty=3,lwd=3,pch=15,cex=2)

1. Linear bit line, width is 3 times times the default, split point is square, size is twice times the default size

3. Use graphical parameters to control the appearance

1Par (Pin=c (2,3))2Par (lwd=2,cex=1.5)3Par (cex.axis=.75,font.axis=3)4Plot (dose,druga,type='b', pch=19,lty=2,col='Red')5Plot (dose,drugb,type='b', pch=23,lty=6,col='Blue', bg='Green')6Par (OPAR)

1. Modify the default parameters of the graphic (2 inches wide, 3 inches high)
2. The line width is twice times the default, and the spacer is 1.5 times times the default
3. Axis scale text is set to italic, 0.75 times times the default size
4. Create the first shape with red dashed and solid dots
5. Create a second graphic with a blue dashed line, a blue border, and a green solid diamond
6. Restore the initial graphics settings

4. Adding text coordinates and legends

1Plot (dose,druga,type='b', col='Red', lty=2,pch=2,lwd=2,2Main ='Clinical trials for Drug A',3Sub ='This is hypothetical data',4Xlab ='dosage', Ylab ='Drug Response',5Xlim = C (0,60), Ylim = C (0,70))

2. Insert the main title
3. Inserting subtitles
4. x-Axis Description
5. Y-Axis description
6. Axis

1Plot (dose,druga,type='b', col='Red', lty=2,pch=2,lwd=2,2Main ='Clinical trials for Drug A',3Sub ='This is hypothetical data',4Xlab ='dosage', Ylab ='Drug Response',5Xlim = C (0,60), Ylim = C (0,70))6Lines (dose,drugb,type='b', pch=17,lty=2,col='Blue')7Abline (H=c (), lwd=1.5,lty=2,col='Grey')8 Library (HMISC)9Minor.tick (nx=3,ny=3,tick.ratio=0.5)TenLegend'TopLeft', inset =. 05,title ='drug Type'C'A','B'), lty = C (ON), PCH = C (15,17), Col=c ('Red','Blue'))

1~7. Drawing
8~9. Add tick marks
10. Add a Legend

5. Text Callout

1 Attach (Mtcars)2Plot (Wt,mpg,main ='mileage vs. Car Weight', Xlab ='Weight', Ylab ='Mileage', pch=18,col='Blue')3Text (Wt,mpg,row.names (mtcars), CeX = 0.6,pos=4,col='Red')4Detach (Mtcars)

3. The text function is used to label points in the drawing

6. Graphic combination

1 Attach (Mtcars)2Opar <-par (no.readonly =T)3Par (Mfrow=c (2,2))4Plot (Wt,mpg,main ='scatterplot of WT vs. mpg')5Plot (Wt,disp,main ='scatterplot of WT vs. DISP')6hist (Wt,main ='histogram of WT')7BoxPlot (WT, main='boxplot of WT')8 par (Opar)9Detach (Mtcars)

3. Specify the distribution of the graph
4. Scatter plot of WT and MPG
5. Scatter plots of WT and DISP
6. The histogram of WT
7. WT's Box

1 Attach (Mtcars) 2 layout (Matrix (c (1,1,2,3), 2,2,byrow = T))3hist (WT)4hist (MPG)  5hist (disp)6 Detach (Mtcars)

2. Use the layout function to change the layout of the graphic

7. Fine-grained control of graphical layouts

1Opar <-par (no.readonly =T)2Par (Fig=c (0,0.8,0,0.8))3Plot (Mtcars$wt,mtcars$mpg,xlab ='Miles Per Gallon', Ylab ='Car Weight')4Par (Fig=c (0,0.8,0.55,1), new=T)5BoxPlot (mtcars$wt,horizontal = t,axes=F)6Par (Fig=c (0.65,1,0,0.8), new=T)7BoxPlot (mtcars$mpg,axes=F)8Mtext'Enhanced Scatterplot', side = 3,outer = T,line = 3)9Par (OPAR)

A. Set up scatter plots
. Add wt on top of the box line diagram
6~7. Add MPG to the right of the box line diagram
8. Add a title

Summary:

The main purpose of this section is to modify the default graphics in R, modify the axes of a graphic, fonts, spacers, lines, colors and how to add headings, subheadings, labels, text guides

How to define the bounds and size of a graph, and how to integrate multiple graphs into one

R language-Graphics First order

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.