R language Ggplot2 package drawing line Chart _r language

Source: Internet
Author: User
Tags ggplot
Introduction

A line chart is generally used to describe a variable of one-dimensional variables as a continuous variable, which is usually time. In other words, a line chart is best suited to describe changes in time series data. Of course, with the change of discrete variables is also possible, but this discrete variable must be ordered. Draw a line chart

A basic line chart is relatively simple, as long as the AES in the Ggplot x,y data and Geom designated as line. If x is a continuous variable, it can be passed in directly. If x is a discrete variable, then x needs to be factorial and the group parameter in AES is set to 1.

Line can be solid line, can also be point-like, you can use a Linetpye to set, parameters can choose solid, dashed, dotted and so on.

Library (Gcookbook)
Library (ggplot2)
#连续变量的情况
ggplot (BOD, AES (X=time, Y=demand)) + Geom_line ()
# The case of discrete variables
BOD1 <-BOD # Assignment Data
bod1$time <-factor (bod1$time) #变量因子化
ggplot (BOD1, AES (X=time, Y=demand, Group=1) + geom_line (linetype= "dotted")

Of course, if you need to change the y-axis range, you can use the following to add Ylim (A, b)) to set.

Add dots to a line chart

Sometimes we need to show the x-axis point data on the line chart, so that we can distinguish the original data more clearly, which is especially suitable for the sparse data, such as the above 6 data. We'll just add geom_point () right behind the code. If you want to change the size and shape of the dots, you can control the two parameters of size and shape.

Ggplot (BOD, AES (X=time, Y=demand)) + geom_line () + geom_point (size=4, shape=20)

Draw more than one line chart

Sometimes we want to group a discrete variable and draw multiple line charts to compare.

Supp1=c ("OJ", "OJ", "OJ", "VC", "VC", "VC") Dose1=c (
0.5,1.0,2.0,0.5,1.0,2.0)
length1=c ( 13.23,22.70,26.06,7.98,16.77,26.14)
tgg=data.frame (supp1,dose1,length1)
Ggplot (TGG, AES (X=factor (DOSE1) , Y=length1, COLOUR=SUPP1,GROUP=SUPP1)) + geom_line (size=2) #x为药剂剂量, not a continuous type variable


Sometimes you want to add data points to the two lines of comparison, you can set the shape or fill parameters in Aes, the two methods get the same diagram, except the point shape difference. The former, the point shape of the two lines is different, the latter point shape is the same.

#设置shape参数
Ggplot (TGG, AES (X=DOSE1, Y=length1, COLOR=SUPP1,SHAPE=SUPP1)) + Geom_line () +geom_point (size=4)
# Set the Fill parameter
ggplot (TGG, AES (X=DOSE1, Y=length1, COLOR=SUPP1,FILL=SUPP1)) + Geom_line () +geom_point (size=4)



Careful you will find that the two lines of upstream data overlap, if you want to separate, using the Position_dodge function is a good way.

Ggplot (TGG, AES (X=DOSE1, Y=length1, SHAPE=SUPP1)) +
Geom_line (Position=position_dodge (0.2)) + # Dodge lines by 0.2< C8/>geom_point (Position=position_dodge (0.2), size=4) #控制两线各向左向右移0.2 clustering, the line also moved, the point also moved, never separate.

The basic drawing of


is this, and the individual thinks that the quality of the painting can be compared with Excel.

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.