R语言与医学统计图形-【9】过渡函数qplot

ggplot2绘图系统

基础绘图包向ggplot2过渡——qplot

绘图理念的不同:
基础绘图包是先铺好画布,再在这张画布上作图(常规思维);
ggplot2打破常规,采用图层叠加的方法。

qplot函数既有plot特点,又体现了ggplot2的特征,是一个过渡函数。

library(ggplot2)

data("diamonds")
qplot(x=carat,
      y=price,
      data=diamonds,
      geom = 'point',
      color=color)
#geom几何对象:smooth/boxplot/path/line/freqpoly/density/jitter/bar
#color映射变量

set.seed(2020)
dsmall <- diamonds[sample(nrow(diamonds),1000),]
qplot(x=carat, #qplot中x/y不能省
      y=price,
      data=dsmall,
      geom='point',
      color=color,
      shape=cut) #映射形状

qplot(x=color,
      data=dsmall,
      geom = 'bar',
      fill='green',  #填充色
      color='red') #边框色
#此处green和red都视为了一个变量

qplot(x=color,
      data=dsmall,
      geom='bar',
      fill=cut)

qplot(x=color,
      data=dsmall,
      geom='bar',
      fill=I('skyblue'))
#加上I函数后可手动设置颜色或形状

qplot(x=color,
      data=dsmall,
      geom='bar',
      fill=I('skyblue'),
      weight=price)
#纵轴变化,price映射到weight参数


其他类型图形。

qplot(x=cut,
      y=price,
      data=dsmall,
      geom='boxplot',
      fill=cut)

qplot(x=price,data=dsmall,geom = 'histogram',fill=cut)
#直方图默认30组
qplot(x=price,data = dsmall,geom = 'density',color=cut)


透明度。

qplot(x=price,data = dsmall,
      geom = 'density',
      fill=cut,
      alpha=I(0.5))

分面(facets)。
row_var ~ col_var按分类变量分成几行几列,点表占位符(可看成1)。

qplot(x=carat,
      y=price,
      facets = color~.,#对颜色分面,点不可少
      data=dsmall)

ggplot2无处不对象,这些对象均以图层叠加形式出现。

原文地址:https://www.cnblogs.com/jessepeng/p/12307687.html

时间: 2024-08-24 22:04:55

R语言与医学统计图形-【9】过渡函数qplot的相关文章

R语言与医学统计图形-【14】ggplot2几何对象之直方密度图

ggplot2绘图系统--几何对象之直方图.密度图 1.直方图 参数. geom_histogram(mapping = , data = , stat = 'bin', #统计变换,概率密度为density position = 'stack', binwidth = , #条柱宽度 bins = , #条柱数目,默认30 na.rm = FALSE, show.legend = NA, inherit.aes = TRUE ) 示例. ggplot(diamonds,aes(carat))+

R语言与医学统计图形-【15】ggplot2几何对象之线图

ggplot2绘图系统--几何对象之线图 曲线:点连线.路径曲线.时间序列曲线.模型拟合曲线...... 直线:水平直线.垂直直线.斜线. 1.曲线 对象及其参数. #路径图 geom_path(mapping = , data = , stat = 'identity', position = 'identity', lineend = 'butt', #线段两端样式,round/square linejoin = 'round', #线段交叉样式,mitre/bevel linemitre

R语言与医学统计图形-【12】ggplot2几何对象之条图

ggplot2绘图系统--几何对象之条图(包括误差条图) 1.条图 格式: geom_bar(mapping = , data = , stat = 'count', #统计变换默认计数 position = 'stack', #默认堆栈 width = , #条形宽度 binwidth = , na.rm = FALSE, show.legend = , inherit.aes = TRUE) positon: dodge并排 fill堆叠填充标准化为1 stack堆栈 identity不做调

R语言与医学统计图形-【10】ggplot2图形映射

ggplot2绘图系统--图形映射 颜色的映射. #aes中映射变量 ggplot()+geom_point(aes(x=carat,y=price,color='blue'),#color视为单一变量 data=dsmall) #映射外的颜色 ggplot()+geom_point(aes(x=carat,y=price), data=dsmall,color='blue') #加I函数后,不管位置 #同样适用于fill/alpha/size/shape等属性 ggplot()+geom_po

R语言与医学统计图形-【11】ggplot2几何对象之散点图

ggplot2绘图系统--几何对象之散点图 以geom开头的函数超过30个.几何对象和标度函数scale密不可分.只有在aes中传入某个变量,scale才能发挥作用. 所谓标度scale,就是图形遥控器,用于控制元素属性.相对于color/shape等参数而言,可以进行更多.更精确的设置. 颜色标度设置 颜色梯度(gradient)标度(scale)函数. #双色梯度函数 scale_color_gradient(...,high='#56B1F7',low='#132B43',...) sca

R语言与医学统计图形-【32】海盗图、词云图、日历图

1.海盗图 参数众多,其语法与基础包类似. 基础图. #devtools::install_github('ndphillips/yarrr') #install.packages('yarrr') library(yarrr) #基本海盗图 str(pirates) pirateplot(formula = age ~ favorite.pirate, data = pirates, xlab = 'Favorite Pirate', ylab = 'Age', main="") 散

R语言与医学统计图形-【13】ggplot2几何对象之盒形图

ggplot2绘图系统--几何对象之盒形图 参数: geom_boxplot(mapping = , #lower,middle,upper,x,ymax,ymin必须(有默认) #alpha/color/fill/linetype/shape/size/weight可选 data = , stat = 'boxplot', position = 'dodge', outlier.color = , #离群点颜色 outlier.shape = 19, outlier.size = 1.5, o

R语言结合概率统计的体系分析---数字特征

现在有一个人,如何对这个人怎么识别这个人?那么就对其存在的特征进行提取,比如,提取其身高,其相貌,其年龄,分析这些特征,从而确定了,这个人就是这个人,我们绝不会认错. 同理,对数据进行分析,也是提取出数据的特征,对其特征进行分析,从而确定这些数据所呈现的信息状况,从而确定了这些数据的独特性和唯一性,因为他呈现的信息是唯一的,绝不与别的是相同的. 那么这些特征是什么呢?拥有哪些特征呢?似乎应该是经过无数科学家的总结,终于发现了几个重要的特征,包括数字特征和分布特征,这个数字特征,包括集中位置,分散

吴裕雄--天生自然 R语言开发学习:图形初阶(续一)

# ----------------------------------------------------# # R in Action (2nd ed): Chapter 3 # # Getting started with graphs # # requires that the Hmisc and RColorBrewer packages # # have been installed # # install.packages(c("Hmisc", "RColorB