par(ask=TRUE)

# Basic scatterplot
library(ggplot2)
ggplot(data=mtcars, aes(x=wt, y=mpg)) +
geom_point() +
labs(title="Automobile Data", x="Weight", y="Miles Per Gallon")

# Scatter plot with additional options
library(ggplot2)
ggplot(data=mtcars, aes(x=wt, y=mpg)) +
geom_point(pch=17, color="blue", size=2) +
geom_smooth(method="lm", color="red", linetype=2) +
labs(title="Automobile Data", x="Weight", y="Miles Per Gallon")

# Scatter plot with faceting and grouping
data(mtcars)
mtcars$am <- factor(mtcars$am, levels=c(0,1),
labels=c("Automatic", "Manual"))
mtcars$vs <- factor(mtcars$vs, levels=c(0,1),
labels=c("V-Engine", "Straight Engine"))
mtcars$cyl <- factor(mtcars$cyl) library(ggplot2)
ggplot(data=mtcars, aes(x=hp, y=mpg,
shape=cyl, color=cyl)) +
geom_point(size=3) +
facet_grid(am~vs) +
labs(title="Automobile Data by Engine Type",
x="Horsepower", y="Miles Per Gallon")

# Using geoms
data(singer, package="lattice")
ggplot(singer, aes(x=height)) + geom_histogram()

ggplot(singer, aes(x=voice.part, y=height)) + geom_boxplot()

data(Salaries, package="car")
library(ggplot2)
ggplot(Salaries, aes(x=rank, y=salary)) +
geom_boxplot(fill="cornflowerblue",
color="black", notch=TRUE)+
geom_point(position="jitter", color="blue", alpha=.5)+
geom_rug(side="l", color="black")

# Grouping
library(ggplot2)
data(singer, package="lattice")
ggplot(singer, aes(x=voice.part, y=height)) +
geom_violin(fill="lightblue") +
geom_boxplot(fill="lightgreen", width=.2)

data(Salaries, package="car")
library(ggplot2)
ggplot(data=Salaries, aes(x=salary, fill=rank)) +
geom_density(alpha=.3)

ggplot(Salaries, aes(x=yrs.since.phd, y=salary, color=rank,
shape=sex)) + geom_point()

ggplot(Salaries, aes(x=rank, fill=sex)) +
geom_bar(position="stack") + labs(title='position="stack"')

ggplot(Salaries, aes(x=rank, fill=sex)) +
geom_bar(position="dodge") + labs(title='position="dodge"')

ggplot(Salaries, aes(x=rank, fill=sex)) +
geom_bar(position="fill") + labs(title='position="fill"')

# Placing options
ggplot(Salaries, aes(x=rank, fill=sex))+ geom_bar()

ggplot(Salaries, aes(x=rank)) + geom_bar(fill="red")

ggplot(Salaries, aes(x=rank, fill="red")) + geom_bar()

# Faceting
data(singer, package="lattice")
library(ggplot2)
ggplot(data=singer, aes(x=height)) +
geom_histogram() +
facet_wrap(~voice.part, nrow=4)

library(ggplot2)
ggplot(Salaries, aes(x=yrs.since.phd, y=salary, color=rank,
shape=rank)) + geom_point() + facet_grid(.~sex)

data(singer, package="lattice")
library(ggplot2)
ggplot(data=singer, aes(x=height, fill=voice.part)) +
geom_density() +
facet_grid(voice.part~.)

# Adding smoothed lines
data(Salaries, package="car")
library(ggplot2)
ggplot(data=Salaries, aes(x=yrs.since.phd, y=salary)) +
geom_smooth() + geom_point()

ggplot(data=Salaries, aes(x=yrs.since.phd, y=salary,
linetype=sex, shape=sex, color=sex)) +
geom_smooth(method=lm, formula=y~poly(x,2),
se=FALSE, size=1) +
geom_point(size=2)

# Modifying axes
data(Salaries,package="car")
library(ggplot2)
ggplot(data=Salaries, aes(x=rank, y=salary, fill=sex)) +
geom_boxplot() +
scale_x_discrete(breaks=c("AsstProf", "AssocProf", "Prof"),
labels=c("Assistant\nProfessor",
"Associate\nProfessor",
"Full\nProfessor")) +
scale_y_continuous(breaks=c(50000, 100000, 150000, 200000),
labels=c("$50K", "$100K", "$150K", "$200K")) +
labs(title="Faculty Salary by Rank and Sex", x="", y="")

# Legends
data(Salaries,package="car")
library(ggplot2)
ggplot(data=Salaries, aes(x=rank, y=salary, fill=sex)) +
geom_boxplot() +
scale_x_discrete(breaks=c("AsstProf", "AssocProf", "Prof"),
labels=c("Assistant\nProfessor",
"Associate\nProfessor",
"Full\nProfessor")) +
scale_y_continuous(breaks=c(50000, 100000, 150000, 200000),
labels=c("$50K", "$100K", "$150K", "$200K")) +
labs(title="Faculty Salary by Rank and Gender",
x="", y="", fill="Gender") +
theme(legend.position=c(.1,.8))

# Scales
ggplot(mtcars, aes(x=wt, y=mpg, size=disp)) +
geom_point(shape=21, color="black", fill="cornsilk") +
labs(x="Weight", y="Miles Per Gallon",
title="Bubble Chart", size="Engine\nDisplacement")

data(Salaries, package="car")
ggplot(data=Salaries, aes(x=yrs.since.phd, y=salary, color=rank)) +
scale_color_manual(values=c("orange", "olivedrab", "navy")) +
geom_point(size=2)

ggplot(data=Salaries, aes(x=yrs.since.phd, y=salary, color=rank)) +
scale_color_brewer(palette="Set1") + geom_point(size=2)

library(RColorBrewer)
display.brewer.all()

# Themes
data(Salaries, package="car")
library(ggplot2)
mytheme <- theme(plot.title=element_text(face="bold.italic",
size="", color="brown"),
axis.title=element_text(face="bold.italic",
size=10, color="brown"),
axis.text=element_text(face="bold", size=9,
color="darkblue"),
panel.background=element_rect(fill="white",
color="darkblue"),
panel.grid.major.y=element_line(color="grey",
linetype=1),
panel.grid.minor.y=element_line(color="grey",
linetype=2),
panel.grid.minor.x=element_blank(),
legend.position="top") ggplot(Salaries, aes(x=rank, y=salary, fill=sex)) +
geom_boxplot() +
labs(title="Salary by Rank and Sex",
x="Rank", y="Salary") +
mytheme

# Multiple graphs per page
data(Salaries, package="car")
library(ggplot2)
p1 <- ggplot(data=Salaries, aes(x=rank)) + geom_bar()
p2 <- ggplot(data=Salaries, aes(x=sex)) + geom_bar()
p3 <- ggplot(data=Salaries, aes(x=yrs.since.phd, y=salary)) + geom_point() library(gridExtra)
grid.arrange(p1, p2, p3, ncol=3) # Saving graphs
ggplot(data=mtcars, aes(x=mpg)) + geom_histogram()
ggsave(file="E:\\mygraph.pdf")

最新文章

  1. git学习(五):克隆和推送远程仓库
  2. log4j使用--http://www.cnblogs.com/eflylab/archive/2007/01/11/618001.html
  3. SQL2000的三种“故障还原模型”
  4. HDU 4681 String 最长公共子序列
  5. UVa 11859 (Nim) Division Game
  6. Servlet 下载文件
  7. sdut 1570 c旅行
  8. Quality in the Test Automation Review Process and Design Review Template
  9. .NET源代码的内部排序实现
  10. putty完全使用手册--多窗口---git提交---连接数据库--自动日志显示
  11. Problem F: 多少个最大值?
  12. JavaScript数组的22种方法
  13. Tomcat系列(1)——Tomcat安装配置
  14. DSP 运行时间计算函数--_itoll(TSCH,TSCL);
  15. 前端--vue框架
  16. [leetcode]49. Group Anagrams变位词归类
  17. B - 低阶入门膜法 - D-query (查询区间内有多少不同的数)
  18. android实现3D Gallery 轮播效果,触摸时停止轮播
  19. 记一次win10 installer安装MySQL 5.7的过程
  20. CSU 1808 地铁(最短路变形)

热门文章

  1. 2020年大厂Java面试题(基础+框架+系统架构+分布式+实战)
  2. webpack--运行npm run dev自动打开浏览器运行首页的两种方式以及热加载
  3. Spring的aop思想
  4. 死磕java(4)
  5. 自己动手写个异步IO函数 --(基于 c# Task)
  6. Deep Learning for Chatbots(Introduction)
  7. python 使用记录
  8. (六)mybatis拦截器
  9. D语言-认识D语言&amp;安装
  10. Windows下无法颜色转义