R Basics-Fast discovery Data (R visualization)

Source: Internet
Author: User
Tags ggplot












---restore content starts---



Install the package and load



> install.packages (' Gcookbook ')



>install.packages (' Ggplot2 ')



> Library (GGPLOT2)



Or: Install.packages (' Ggplot2 ', ' Gcokbook ')



> Library (Gcookbook)



To set the working path:


SETWD ("D:/data")
GETWD ()


[1] "D:/data"



Read file



> Data <-read.csv (' dadtafile.csv ')



> Data
X1 X3.8.1941 X1.1.2007
1 2 1/24/1972 1/1/2007
2 3 6/1/1932 1/1/2007
3 4 5/17/1947 1/1/2007
4 5 3/10/1943 1/1/2007
5 6 1/8/1940 1/1/2007
6 7 8/5/1947 1/1/2007
7 8 4/14/2005 1/1/2007
8 9 6/23/1961 1/2/2007
9 10 1/10/1949 1/2/2007






> Data <-read.csv (' dadtafile.csv ', header=false)



> Data
V1 V2 V3
1 1 3/8/1941 1/1/2007
2 2 1/24/1972 1/1/2007
3 3 6/1/1932 1/1/2007
4 4 5/17/1947 1/1/2007
5 5 3/10/1943 1/1/2007
6 6 1/8/1940 1/1/2007
7 7 8/5/1947 1/1/2007
8 8 4/14/2005 1/1/2007
9 9 6/23/1961 1/2/2007
10 10 1/10/1949 1/2/2007



> Head (data)
V1 V2 V3
1 1 3/8/1941 1/1/2007
2 2 1/24/1972 1/1/2007
3 3 6/1/1932 1/1/2007
4 4 5/17/1947 1/1/2007
5 5 3/10/1943 1/1/2007
6 6 1/8/1940 1/1/2007






> Names (data) <-C (' Column1 ', ' Column1 ', ' Column1 ')
> Data
Column1 Column1 Column1
1 1 3/8/1941 1/1/2007
2 2 1/24/1972 1/1/2007
3 3 6/1/1932 1/1/2007
4 4 5/17/1947 1/1/2007
5 5 3/10/1943 1/1/2007
6 6 1/8/1940 1/1/2007
7 7 8/5/1947 1/1/2007
8 8 4/14/2005 1/1/2007
9 9 6/23/1961 1/2/2007
10 10 1/10/1949 1/2/2007
>



Sep parameter Setting separator symbol:



> Data <-read.csv (' datafile.csv ', sep= ' \ t ')



#制表符 use \ t






The characters that are read are as follows: China is recognized as a factor, so Stringsasfactors=false



i.e.:> data <-read.csv (' dadtafile.csv ')



> Data$sex <-factor (Data$sex)



#转换为因子



> str (DATA)
' Data.frame ': Ten Obs. of 3 variables:
$ column1:int 1 2 3 4 5 6 7 8 9 10
$ column1:factor W/10 Levels "1/10/1949", "1/24/1972",..: 5 2 8 7 4 3 10 6 9 1
$ column1:factor W/2 Levels "1/1/2007", "1/2/2007": 1 1 1 1 1 1 1 1 2 2


















---restore content ends---



---restore content starts---



Install the package and load



> install.packages (' Gcookbook ')



>install.packages (' Ggplot2 ')



> Library (GGPLOT2)



Or: Install.packages (' Ggplot2 ', ' Gcokbook ')



> Library (Gcookbook)



To set the working path:


SETWD ("D:/data")
GETWD ()


[1] "D:/data"



Read file



> Data <-read.csv (' dadtafile.csv ')



> Data
X1 X3.8.1941 X1.1.2007
1 2 1/24/1972 1/1/2007
2 3 6/1/1932 1/1/2007
3 4 5/17/1947 1/1/2007
4 5 3/10/1943 1/1/2007
5 6 1/8/1940 1/1/2007
6 7 8/5/1947 1/1/2007
7 8 4/14/2005 1/1/2007
8 9 6/23/1961 1/2/2007
9 10 1/10/1949 1/2/2007






> Data <-read.csv (' dadtafile.csv ', header=false)



> Data
V1 V2 V3
1 1 3/8/1941 1/1/2007
2 2 1/24/1972 1/1/2007
3 3 6/1/1932 1/1/2007
4 4 5/17/1947 1/1/2007
5 5 3/10/1943 1/1/2007
6 6 1/8/1940 1/1/2007
7 7 8/5/1947 1/1/2007
8 8 4/14/2005 1/1/2007
9 9 6/23/1961 1/2/2007
10 10 1/10/1949 1/2/2007



> Head (data)
V1 V2 V3
1 1 3/8/1941 1/1/2007
2 2 1/24/1972 1/1/2007
3 3 6/1/1932 1/1/2007
4 4 5/17/1947 1/1/2007
5 5 3/10/1943 1/1/2007
6 6 1/8/1940 1/1/2007






> Names (data) <-C (' Column1 ', ' Column1 ', ' Column1 ')
> Data
Column1 Column1 Column1
1 1 3/8/1941 1/1/2007
2 2 1/24/1972 1/1/2007
3 3 6/1/1932 1/1/2007
4 4 5/17/1947 1/1/2007
5 5 3/10/1943 1/1/2007
6 6 1/8/1940 1/1/2007
7 7 8/5/1947 1/1/2007
8 8 4/14/2005 1/1/2007
9 9 6/23/1961 1/2/2007
10 10 1/10/1949 1/2/2007
>



Sep parameter Setting separator symbol:



> Data <-read.csv (' datafile.csv ', sep= ' \ t ')



#制表符 use \ t






The characters that are read are as follows: China is recognized as a factor, so Stringsasfactors=false



i.e.:> data <-read.csv (' dadtafile.csv ')



> Data$sex <-factor (Data$sex)



#转换为因子



> str (DATA)
' Data.frame ': Ten Obs. of 3 variables:
$ column1:int 1 2 3 4 5 6 7 8 9 10
$ column1:factor W/10 Levels "1/10/1949", "1/24/1972",..: 5 2 8 7 4 3 10 6 9 1
$ column1:factor W/2 Levels "1/1/2007", "1/2/2007": 1 1 1 1 1 1 1 1 2 2









Read.table ()









READ.XLSX ()






Install.packages (xlsx)



Library (XLSX)



If the library () is unsuccessful, refer to: https://wenku.baidu.com/view/1bc6610ece2f0066f433229f.html



Download java:https://www.java.com/en/download/manual.jsp












> Head (mtcars)
MPG cyl DISP hp Drat wt Qsec VS AM gear Carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
>



> Plot (wt,mpg)
Error in plot (WT, MPG): object ' wt ' not found
> Attach (Mtcars)
> Plot (wt,mpg)
> Detach (Mtcars)
> #par函数可实现一页多图, Color. thickness, etc...












In Ggplot2:



> Library (GGPLOT2)
> Attach (Mtcars)



> Qplot (WT,MPG)
> Detach (Mtcars)



Equivalent to:



> Qplot (wt,mpg,data=mtcars)



Equivalent to:
> Ggplot (Mtcars,aes (X=wt,y=mpg)) +geom_point ()












> Plot



View Help



Usage



Plot (x, Y, ...)
Arguments


X
The coordinates of points in the plot. Alternatively, a single plotting structure, the function or any R object with a plot method can be provided.
Y
The y coordinates of points in the plot, optional if x are an appropriate structure.
...
Arguments to is passed to methods, such as graphical parameters (see PAR). Many methods would accept the following arguments:
Type
What type of plot should is drawn. Possible types is
"P" for points,
"L" for lines,
"B" for both,
"C" for the lines part alone of "B",
"O" for both ' overplotted ',
"H" for ' histogram ' like (or ' high-density ') vertical lines,
"S" for Stair steps,
"S" for the other steps, see ' Details ' below,
"N" for no plotting.
All and types give a warning or an error; Using, e.g., type = "Punkte" being equivalent to type = ' P ' for S compatibility. Note that some methods, e.g. Plot.factor, don't accept this.
Main
A overall title for the Plot:see title.
Sub
A sub title for the Plot:see title.
Xlab
A title for the X Axis:see title.
Ylab
A title for the Y axis:see title.
Asp
The y/x aspect ratio, see Plot.window.








> Plot (pressure$temperature,pressure$pressure)






> Plot (pressure$temperature,pressure$pressure,type= "L")
>












> Points (pressure$temperature,pressure$pressure)
>






> Lines (pressure$temperature,pressure$pressure/2,col= "red")
> Points (pressure$temperature,pressure$pressure/2,col= "red")
>






> Qplot (pressure$temperature,pressure$pressure,geom= "line")
> Qplot (temperature,pressure,data=pressure,geom=c ("line", "point"))
Equivalent to:
> Ggplot (Pressure,aes (x=temperature,y=pressure)) +geom_line () +geom_point ()












> Data <-mtcars
> table (cyl)
Cyl
4 6 8
11 7 14






#计算频数



> Head (mtcars)
MPG cyl DISP hp Drat wt Qsec VS AM gear Carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1









> Barplot (bod$demand,names.arg=bod$time)






> Barplot (Table (cyl))






> Qplot (bod$time,bod$demand,geom= "bar", stat= "identity")

> Qplot (Factor (bod$time), bod$demand,geom= "bar", stat= "identity")



Can't draw!



Error:stat_count () must not is used with a Y aesthetic.
In addition:warning message:
' Stat ' is deprecated








> Qplot (mtcars$cyl)

> Qplot (Factor (mtcars$cyl))












> Ggplot (Bod,aes (Time,demand)) +geom_bar (stat = ' identity ')






> Ggplot (Bod,aes (X=factor (time), Y=demand) +geom_bar (stat= "Identity")
>












Https://www.cnblogs.com/lizhilei-123/p/6722116.html



Ggplot2 's fast-drawing qplot ()----color. Transparency, shape






Frequency Number Bar chart:



> Library (GGPLOT2)
> Ggplot (Mtcars,aes (X=factor (cyl)) +geom_bar ()



Equivalent:
> Qplot (Factor (cyl), Data=mtcars)






The bar chart looks similar to the histogram, but it is different, and the x-axis of the bar chart is a definite value, and the histogram is an interval.






> hist (mtcars$mpg,breaks=10)



#breaks = 10 Group pitch is 10









> qplot (mpg,data = mtcars,binwidth=4)



Or



> Ggplot (Mtcars,aes (X=mpg)) +geom_histogram (binwidth = 4)
The >binwidth parameter is the interval that adjusts the horizontal axis, and you can arbitrarily adjust the interval you think is appropriate.






Plot (toothgrowth$supp, toothgrowth$len)





BoxPlot (Len~supp,data = toothgrowth)




qplot(supp,len,data=ToothGrowth,geom = ‘boxplot‘)




ggplot(ToothGrowth,aes(supp,len))+geom_boxplot() 




 





























R code execution error???



Re-install it ....



System C disk space capacity is not enough.






R Basics-Fast discovery Data (R visualization)


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.