博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
吴裕雄--天生自然 R语言开发学习:使用ggplot2进行高级绘图(续一)
阅读量:5297 次
发布时间:2019-06-14

本文共 6650 字,大约阅读时间需要 22 分钟。

#----------------------------------------------------------## R in Action (2nd ed): Chapter 19                         ## Advanced graphics with ggplot2                           ## requires packages ggplot2, RColorBrewer, gridExtra,      ##   and car (for datasets)                                 ## install.packages(c("ggplot2", "gridExtra",               # #      "RColorBrewer", "car"))                             ##----------------------------------------------------------#par(ask=TRUE)# Basic scatterplotlibrary(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 optionslibrary(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 groupingdata(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 geomsdata(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")# Groupinglibrary(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 optionsggplot(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()# Facetingdata(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 linesdata(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 axesdata(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="")# Legendsdata(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))# Scalesggplot(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()# Themesdata(Salaries, package="car")library(ggplot2)mytheme <- theme(plot.title=element_text(face="bold.italic",                                         size="14", 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 pagedata(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 graphsggplot(data=mtcars, aes(x=mpg)) + geom_histogram()ggsave(file="mygraph.pdf")

 

转载于:https://www.cnblogs.com/tszr/p/11177743.html

你可能感兴趣的文章
uva 10791
查看>>
JD路径配置及myeclipse主题和提示设置
查看>>
React 避免重渲染
查看>>
对象与包装类:ps踩过的写博客的坑,长记性
查看>>
【SAP业务模式】之ICS(六):发票输出类型
查看>>
powerdesigner从EXCEL读入数据
查看>>
UOJ #131 【NOI2015】 品酒大会
查看>>
CNN的学习笔记
查看>>
【论文学习】YOLO9000: Better,Faster,Stronger(YOLO9000:更好,更快,更强)
查看>>
全文索引FULLTEXT 的使用
查看>>
代码折叠
查看>>
Ubuntu通过apt-get安装指定版本和查询指定软件有多少个版本
查看>>
Ubuntu 16.04安装Ubuntu After Install工具实现常用软件批量安装
查看>>
CentOS Ubantu linux中设置history历史命令显示命令执行时间
查看>>
Android背景渐变色(shape,gradient)
查看>>
设计模式之装饰者模式
查看>>
知乎爬虫之3:请求分析(附赠之前爬取的数据一份)
查看>>
bzoj4896 补退选
查看>>
sql中char、nchar、varchar和nvarchar的区别
查看>>
JAVA中方法参数传递问题
查看>>