R语言-《Learning R》-Chapter14:Exploring and Visulizing

## 载入练习数据
> data(obama_vs_mccain, package = "learningr")
## 绘图
> with(obama_vs_mccain, plot(obama_vs_mccain$Income, obama_vs_mccain$Turnout))
## 绘图 增加颜色和pch(short for “plot character”)
> with(obama_vs_mccain, plot(obama_vs_mccain$Income,obama_vs_mccain$Turnout, col = "violet", pch = 20))
## R语言中,颜色的名字列表
> colors()
> ## 作图
> par(mar = c(3, 3, 0.5, 0.5), oma = rep.int(0, 4),mgp = c(2, 1, 0))
> regions <- levels(obama_vs_mccain$Region)
> plot.number <- seq_along(regions)
> layout(matrix(plot.number,ncol = 5, byrow = TRUE))
> for(region in regions){
+     regional_data <- subset(obama_vs_mccain, Region == region)
+ }
> for(region in regions){
+     regional_data <- subset(obama_vs_mccain, Region == region)
+     with(regional_data, plot(Income, Turnout))
+ }
+ > xyplot(
+     Turnout ~ Income, 
+     obama_vs_mccain,
+     scales = list(log = TRUE)
+     )
> xyplot(
+     Turnout ~ Income | Region, 
+     obama_vs_mccain,
+     scales = list(
+         log = TRUE,
+         relation = "same",
+         alternating = FALSE
+     ),
+     layout = c(5, 2)
+     )
## 作为参数
> lat1 <- xyplot(
+     Turnout ~ Income | Region, 
+     obama_vs_mccain,
+     )
> lat2 <- update(lat1,col = "violet")
## ggplot2 (the “2” is because it took a couple of attempts to get it right) takes many of the good ideas in lattice and builds on them.
> 2e4
[1] 20000
> rep(2e4, 2)
[1] 20000 20000
//## "base"作折线图
library(learningr)
> with(
+     crab_tag$daylog,
+     plot(Date, -Max.Depth, type = "l", ylim = c(-max(Max.Depth), 0)
+ ))
//## "base"增加一条蓝色的线
> with(
+     crab_tag$daylog,
+     lines(Date, -Min.Depth, col = "blue")
+ )
//## "lattice"作折线图
> xyplot(-Min.Depth + -Max.Depth ~ Date, crab_tag$daylog, type = "l")
//## "ggplot2"作折线图
> ggplot(crab_tag$daylog, aes(Date, Min.Depth)) +
+        geom_line()
//## "ggplot2"作折线图
> ggplot(crab_tag$daylog, aes(Date, ymin = -Min.Depth, ymax = -Max.Depth )) +
+        geom_ribbon(color = "black", fill = "white")
//## If you want to explore the distribution of a continuous variable, histograms are the obvious choice.
//## "base"作柱状图
> library(learningr)
> with(obama_vs_mccain, 
>      hist(Obama)
>      )
//## "base"作柱状图 4个柱子+命名
> with(obama_vs_mccain, 
>      hist(Obama, 4, 
>      main = "Title of the picture")
>      )
> with(obama_vs_mccain, 
       hist(Obama, seq.int(0, 100, 10), main = "Title of the picture")
+      )
//## "base"作柱状图 使用FD的算法
with(obama_vs_mccain,
     hist(Obama, "FD", main = "Title of the picture")
     )
//## "base"作柱状图 定义function  Figure14-31.
> binner <- function(x){
+     seq(min(x, na.rm = TRUE), max(x, na.rm = TRUE), length.out = 50)
+ }
> with(obama_vs_mccain,
+      hist(Obama, binner, main = "Title of the picture")
+      ) 
//## "base"作柱状图 定义function  Figure14-32.
> with(obama_vs_mccain, 
+      hist(Obama, freq = FALSE))
> with(obama_vs_mccain, 
+      hist(Obama, freq = TRUE))
//## "lattice"作柱状图   Figure14-33.
> library(lattice)
> histogram(~ Obama, obama_vs_mccain)
> histogram(~ Obama, obama_vs_mccain, 
+           breaks = 10)
//## "ggplot2"作柱状图   Figure14-33.
> library(ggplot2)
> ggplot(obama_vs_mccain, aes(Obama)) +
+     geom_histogram(binwidth = 5)
> ggplot(obama_vs_mccain, aes(Obama, ..density..)) +
+     geom_histogram(binwidth = 5)
> ggplot(obama_vs_mccain, aes(Obama, ..count..)) +
+     geom_histogram(binwidth = 5)
//## "base"作box plot图   Figure14-38.
> boxplot(Obama ~ Region, data = obama_vs_mccain)
//## "base"作box plot图   Figure14-39.  按照median排名从左到右
> ovm <- within(
+     obama_vs_mccain,
+     Region <- reorder(Region, Obama, median)
+ )
> boxplot(Obama ~ Region, data = ovm)
//## "base"作bar 图   
//## Bar charts (a.k.a. bar plots) are the natural way of displaying numeric variables8 split by a categorical variable.
//## 删除2个地区
> ovm <- ovm[!(ovm$State %in% c("Alaska", "Hawaii")), ]
> par(las = 1, mar = c(3, 9, 1, 1))
//## 比较多个参数
> with(ovm, barplot(Catholic, names.arg = State, horiz = TRUE))
> religions <- with(ovm, rbind(Catholic, Protestant, Non.religious, Other))
> colnames(religions) <- ovm$State
> par(las = 1, mar = c(3, 9, 1, 1))
> barplot(religions, horiz = TRUE, beside = FALSE)
//## 如果看不懂,就改一个参数吧
> barplot(religions, horiz = TRUE, beside = TRUE)
//##  "lattice"作bar 图   
> barchart(State ~ Catholic, ovm)
> barchart(State ~ Catholic + Protestant,
+          ovm,
+          stack = TRUE
+          )
//##  "ggplot2"作bar 图  如果作图太多,会出现无法显示的情况,需要清空绘图区
> library(reshape2)
> religions_long <- melt(
+     ovm,
+     id.vars = "State",
+     measure.vars = c("Catholic", "Protestant")
+ )
> ggplot(religions_long, 
+        aes(State, value, fill = variable)) +
+     geom_bar(stat = "identity") +
+     coord_flip()
> ggplot(religions_long, 
+        aes(State, value, fill = variable)) +
+     geom_bar(stat = "identity", position = "dodge") +
+     coord_flip()
> ggplot(religions_long, 
+        aes(State, value, fill = variable)) +
+     geom_bar(stat = "identity", position = "fill") +
+     coord_flip()

猜你喜欢

转载自blog.csdn.net/weixin_43452592/article/details/83904841