R language Learning-line drawing

Source: Internet
Author: User
Tags vars ggplot

A line chart is a way to reflect a change in the trend, and its input data is generally a matrix.Single line DiagramSuppose there is such a matrix, the first column of the transcription starting site and its upstream and downstream 5 KB area, the second column h3k27ac decorated in these areas of abundance, want to draw a line chart display.
Profile= "Pos; h3k27ac-5000;8.7-4000;8.4-3000;8.3-2000;7.2-1000;3.60;3.61000;7.12000;8.23000;8.44000;8.55000;8.5 "
Read in Data
Profile_text <-read.table (Text=profile, header=t, Row.names=1, quote= "", sep= ";") # Keep location information in Melt # Melt format is ggplot2 paint favorite format # Good experience under this format, although more occupy a lot of space, but it is very convenient # here can be used ' xvariable ', can be other strings, but need to ensure that the following with the same # Because this column is to be displayed on the x-axis, it is named ' Xvariable '. profile_text$xvariable = Rownames (profile_text) library (GGPLOT2) library (reshape2) data_m <-Melt (Profile_text, Id.vars=c ("xvariable")) data_m   xvariable variable value1      -5000  h3k27ac   8.72      -4000  h3k27ac   8.43      -3000  h3k27ac   8.34      -2000  h3k27ac   7.25      -1000  h3k27ac   3.66          0  H3k27ac   3.67  h3k27ac   7.18  h3k27ac 8.29   8.410      4000  h3k27ac   8.511  h3k27ac   8.5
And then start drawing, just like the painting heatmap.
# variable and value are two columns after the matrix melt the name, internal variable, variable represents the point line attribute, and value represents the corresponding value. P <-Ggplot (Data_m, AES (X=xvariable, Y=value), color=variable) + geom_line () # The diagram is stored in the current directory's Rplots.pdf file, and if you use Rstudio, you can not run Dev.off () Dev.off ()
Expecting a bell-shaped curve, the result went wrong. Look carefully, come out a hint
Geom_path:each Group consists of only one observation. Need to adjust the group aesthetic?
The original default Ggplot2 each point as a grouping, nothing is drawn out. And data_m in the data are derived from a group of H3K27AC, the name of the group variable, modify the script to see the effect.
P <-Ggplot (Data_m, AES (X=xvariable, Y=value, color=variable, group=variable)) + geom_line () + Theme (legend.position= C (0.1,0.9)) p
Figure out, a line, look no problem; look carefully, no, how is not the bell-shaped, the original horizontal dislocation. Check the data format
Summary (data_m)  xvariable      variable         length:11 h3k27ac:11 class:character              Mode  : Character
The problem is that xvariable, though it looks like a number, is actually stored as a string (because it is read as a row name) and needs to be converted to a number.
Data_m$xvariable <-as.numeric (data_m$xvariable) #再检验下is. Numeric (data_m$xvariable) [1] TRUE
All right, keep drawing.
# Note When the line is broken, the plus sign is at the end of the line and cannot be placed at the beginning P <-Ggplot (Data_m, AES (X=xvariable, y=value,color=variable,group=variable)) +     Geom_line ( ) + Theme (Legend.position=c (0.1,0.8)) p
Figure finally out, adjusted the position of the next legend, looks a little bit ugly, if smooth, will not be better, stat_smooth can be drawn to the line for local fitting. You can use (but use caution) without affecting the trend of change.
P <-Ggplot (Data_m, AES (X=xvariable, y=value,color=variable,group=variable)) +      geom_line () + Stat_smooth ( method= "Auto", Se=false) +      Theme (Legend.position=c (0.1,0.8)) p
Look, the trend is still the same, the lines are more graceful. Another way is to increase the number of intervals, the line will be better, and more real. Stat_smooth and Geom_line each draw a line, keep only one bar is good.
P <-Ggplot (Data_m, AES (X=xvariable, y=value,color=variable,group=variable)) +      Stat_smooth (method= "Auto", se= FALSE) + Theme (Legend.position=c (0.1,0.8)) p
All right, finally, we've finished drawing a single line chart.multi-line diagramLet's take another example of a multi-line graph, just add a few more columns to the previous data matrix.
Profile = "pos;h3k27ac;ctcf;enhancer;h3k4me3;polii-5000;8.7;10.7;11.7;10;8.3-4000;8.4;10.8;11.8;9.8;7.8-3000;8.3 ; 10.5;12.2;9.4;7-2000;7.2;10.9;12.7;8.4;4.8-1000;3.6;8.5;12.8;4.8;1.30;3.6;8.5;13.4;5.2;1.51000;7.1;10.9;12.4; 8.1;4.92000;8.2;10.7;12.4;9.5;7.73000;8.4;10.4;12;9.8;7.94000;8.5;10.6;11.7;9.7;8.25000;8.5;10.6;11.7;10;8.2 " Profile_text <-read.table (Text=profile, header=t, Row.names=1, quote= "", sep= ";") profile_text$xvariable = Rownames (profile_text) data_m <-Melt (Profile_text, Id.vars=c ("xvariable")) data_m$ Xvariable <-As.numeric (data_m$xvariable) # here group=variable, not group=1 (if you use 1 above) # Variable and value are the names of two columns after the matrix melt, internal variables, variable represent the attributes of the point line, and value represents the corresponding value. P <-Ggplot (Data_m, AES (X=xvariable, y=value,color=variable,group=variable)) + Stat_smooth (method= "Auto", Se=FALSE ) + Theme (Legend.position=c (0.85,0.2)) p
Horizontal Text line graphIf the horizontal axis is text, how do you adjust the order? Remember the order of the rows or columns next to the heat map? Reset the variable's factor level to control its order.
Profile = "pos;h3k27ac;ctcf;enhancer;h3k4me3;polii-5000;8.7;10.7;11.7;10;8.3-4000;8.4;10.8;11.8;9.8;7.8-3000;8.3 ; 10.5;12.2;9.4;7-2000;7.2;10.9;12.7;8.4;4.8-1000;3.6;8.5;12.8;4.8;1.30;3.6;8.5;13.4;5.2;1.51000;7.1;10.9;12.4; 8.1;4.92000;8.2;10.7;12.4;9.5;7.73000;8.4;10.4;12;9.8;7.94000;8.5;10.6;11.7;9.7;8.25000;8.5;10.6;11.7;10;8.2 " Profile_text <-read.table (Text=profile, header=t, Row.names=1, quote= "", sep= ";") Profile_text_rownames <-row.names (profile_text) profile_text$xvariable = Rownames (profile_text) data_m <-Melt ( Profile_text, Id.vars=c ("Xvariable") # is this sentence that will often be used data_m$xvariable <-factor (data_m$xvariable, Levels=profile_ Text_rownames, ordered=t) # Geom_line Set the thickness and transparency of the line P <-ggplot (Data_m, AES (X=xvariable, y=value,color=variable,group= Variable) + geom_line (size=1, alpha=0.9) + Theme (Legend.position=c (0.85,0.2)) + Theme (Axis.text.x=element_text (angle) =45,hjust=1, Vjust=1) # stat_smooth#p <-Ggplot (Data_m, AES (X=xvariable, y=value,color=variable,group=variable)) + Stat_Smooth (method= "Auto", Se=false) + Theme (Legend.position=c (0.85,0.2)) + Theme (Axis.text.x=element_text (angle=45, Hjust=1, vjust=1)) p
Compare the position information as the number (front line chart) and position information on the horizontal axis of the difference. When the value is numeric, Ggplot2 selects the appropriate number of ticks to mark, and when it is text, it is all marked. In addition, the smooth effect is not obvious in the text axis

This completes the basic drawing of the line chart, although it is still possible, but there are many areas that need to be improved, such as adding one or more vertical lines on the online map, adding a horizontal line, modifying the x-axis markers (e.g. 0 for TSS), setting the color of each line, and so on.

R language Learning-line drawing

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.