Quickly explore data

method

Direct call plot()function, passing vectors x and y vector run command.

plot(mtcars$wt, mtcars$mpg)

plot of chunk unnamed-chunk-1

For ggplot2 system can be used qplot()to obtain the same results of drawing functions:

library(ggplot2)
qplot(mtcars$wt, mtcars$mpg)

plot of chunk unnamed-chunk-2

If the vector contains two parameters in a data frame, the following commands:

qplot(wt, mpg, data=mtcars)

plot of chunk unnamed-chunk-3

# 这与下面等价
# ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()

Draw a line chart

method

Use plot()need to pass in a function to draw the coordinates of parameters, and the use of parameters type="l":

plot(pressure$temperature, pressure$pressure, type="l")

plot of chunk unnamed-chunk-4

If you want to add a point or polyline On this basis, we need to point()function and lines()implement functions.

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")

plot of chunk unnamed-chunk-5

You can use ggplotthe package to achieve a similar effect.

library(ggplot2)
qplot(pressure$temperature, pressure$pressure, geom=c("line", "point"))

plot of chunk unnamed-chunk-6

# 或者
 
ggplot(pressure, aes(x=temperature, y=pressure)) + geom_line() + geom_point()

plot of chunk unnamed-chunk-6

Draw a bar graph

method

The barplot()two arguments, a first set heights of the bars, a second set corresponding to the label (optional).

barplot(BOD$demand, names.arg = BOD$Time)

plot of chunk unnamed-chunk-7

Sometimes, the bar represents the frequency of each element in the packet, which is similar with the histogram. However, the x-axis is no longer seen in FIG continuous values, but discrete. Use table()frequency computing function category.

barplot(table(mtcars$cyl))

plot of chunk unnamed-chunk-8

You can use ggplot2the system functions, attention needs to be converted as the abscissa variable is a type and a parameter setting factor.

library(ggplot2)
 
ggplot 大专栏  快速探索数据(BOD, aes(x=factor(Time), y=demand)) + geom_bar(stat="identity")

plot of chunk unnamed-chunk-9

ggplot(mtcars, aes(x=factor(cyl))) + geom_bar()

plot of chunk unnamed-chunk-9

Draw a histogram

Similarly, we use both methods to draw

hist(mtcars$mpg)

plot of chunk unnamed-chunk-10

# 通过breaks参数指定大致组距
hist(mtcars$mpg, breaks = 10)

plot of chunk unnamed-chunk-10

library(ggplot2)
ggplot(mtcars, aes(x=mpg)) + geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

plot of chunk unnamed-chunk-10

ggplot(mtcars, aes(x=mpg)) + geom_histogram(binwidth = 5)

plot of chunk unnamed-chunk-10

Drawing boxplot

Use plot()passing it two vectors when plotted as a function boxplot: x, y. When the variable x is a factor, it will default to draw boxplot.

plot(ToothGrowth$supp, ToothGrowth$len)

plot of chunk unnamed-chunk-11

When these two variable parameters contained in the same data block, use the formula syntax.

# 公式语法
boxplot(len ~ supp, data=ToothGrowth)

plot of chunk unnamed-chunk-12

# 在x轴引入两变量交互
boxplot(len ~ supp + dose, data = ToothGrowth)

plot of chunk unnamed-chunk-12

Use the following ggplot2draw

library(ggplot2)
qplot(ToothGrowth$supp, ToothGrowth$len, geom="boxplot")

plot of chunk unnamed-chunk-13

ggplot(ToothGrowth, aes(x=supp, y=len)) + geom_boxplot()

plot of chunk unnamed-chunk-13

Using the interaction()function to grouping variables can be plotted together boxplot multi-packet-based variables.

ggplot(ToothGrowth, aes(x=interaction(supp, dose), y=len)) + geom_boxplot()

plot of chunk unnamed-chunk-14

Guess you like

Origin www.cnblogs.com/liuzhongrong/p/11873945.html