如何对一维数据进行可视化? geom_histogram:为频率分布直方图,专门用于绘制一列数据的分布,即使是一列数据我们可以看到也是数据框格式的。 # 加载需要包 library(ggplot2) library(tidyverse) ## -- Attaching packages ------------------------------------------------------------------------------------ tidyverse 1.2.1 -- ## √ tibble 2.1.3 √ purrr 0.3.2 ## √ tidyr 0.8.3 √ dplyr 0.8.3 ## √ readr 1.3.1 √ stringr 1.4.0 ## √ tibble 2.1.3 √ forcats 0.4.0 ## -- Conflicts --------------------------------------------------------------------------------------- tidyverse_conflicts() -- ## x dplyr::filter() masks stats::filter() ## x dplyr::lag() masks stats::lag() # 数据来源 data=data.frame(value=rnorm(100)) head(data) ## value ## 1 -0.8793757 ## 2 0.7260648 ## 3 -0.6727662 ## 4 0.9066471 ## 5 1.0111092 ## 6 1.5884468 # 基本柱形图展示 (p <- ggplot(data, aes(x=value)) + geom_histogram()) ## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. ### 调整图形宽度 %>%为管道符操作,再tidyverse包中定义的函数,可以支持其中八个包的管道操作,方便减少变量的设置。
通过binwidth参数调整柱子宽度来获得最佳效果 # 加载需要包 library(tidyverse) library(hrbrthemes)
# 作者是github上获得数据,这里我们下载到本地,方便重复运行 data <- read.table("./1_OneNum.csv", header=TRUE) # data # 图形展示 # %>%将左边值传递给右边 # filter 对数据进行过滤 binwidth 图形宽度 ggtitle设置标题 Arial Narrow 设置主题(字体...) p <- data %>% filter( price<300 ) %>% ggplot( aes(x=price)) + geom_histogram( binwidth=3, fill="#69b3a2", color="#e9ecef", alpha=0.9) + ggtitle("Bin size = 3") + theme_ipsum() + theme( plot.title = element_text(size=15) ) p 
如果我们想比对两组数据的整体分布,此时密度图可以给我们很大帮助,通过不同的颜色区分不两组数据,展示全部数据分布。 # 加载包 library(ggplot2) library(hrbrthemes)
# 设置数据,rnorm()函数产生一系列的随机数,mean均值为2 data <- data.frame( var1 = rnorm(1000), var2 = rnorm(1000, mean=2) ) head(data) ## var1 var2 ## 1 0.2859960 2.173788 ## 2 -0.4200856 1.111710 ## 3 0.9437874 3.010721 ## 4 -1.7653630 3.106633 ## 5 -0.6479067 1.395062 ## 6 0.1049380 1.941582 # 图形 p <- ggplot(data, aes(x=x) ) + # 上端 geom_density( aes(x = var1, y = ..density..), fill="#69b3a2" ) + geom_label( aes(x=4.5, y=0.25, label="variable1"), color="#69b3a2") + # 下端 geom_density( aes(x = var2, y = -..density..), fill= "#404080") + geom_label( aes(x=4.5, y=-0.25, label="variable2"), color="#404080") + theme_ipsum() + xlab("value of x")
p 同一轴上展示多个图形
两组数据geom_histogram通过直方图也可以展示类似密度图的效果,通过透明度的设置似乎数据的全貌都被我们探查了,这实在是太好了。 library(ggplot2) library(dplyr) library(hrbrthemes)
# 设置多组数据 #rep()对数值进行复制.variable 1复制1000次 data <- data.frame( type = c( rep("variable 1", 1000), rep("variable 2", 1000) ), value = c( rnorm(1000), rnorm(1000, mean=4) ) ) # data # 图形输出 p <- data %>% ggplot( aes(x=value, fill=type)) + geom_histogram( color="#e9ecef", alpha=0.6, position = 'identity') + scale_fill_manual(values=c("#69b3a2", "#404080")) + theme_ipsum() + labs(fill="") p 
多组数据展示 通过分面library(tidyverse) library(hrbrthemes) library(viridis) library(forcats)
# # 加载数据 data <- read.table("./probly.csv", header=TRUE, sep=",") data <- data %>% #gather()对数据格式进行调整,新生成两列命名为 text(包括源数据文字列),value(源数据数值列) gather(key="text", value="value") %>% #gsub("目标字符", "替换字符", 对象).将text中\\.替换 mutate(text = gsub("\\.", " ",text)) %>% # round()对数据进行处理,0代表输出整数 as.numeric()将value转换为数值变量 mutate(value = round(as.numeric(value),0))
# 图形 p <- data %>% # fct_reorder()对数据进行排序,按照value对text数据进行重排 mutate(text = fct_reorder(text, value)) %>% # value作为x轴,用text区分,填充 ggplot( aes(x=value, color=text, fill=text)) + geom_histogram(alpha=0.6, binwidth = 5) + # 使用调色板 scale_fill_viridis(discrete=TRUE) + scale_color_viridis(discrete=TRUE) + # 设置主题 theme_ipsum() + theme( #设置图例位置 ,none隐藏图例 legend.position="none", #多个绘图区的距离 panel.spacing = unit(0.1, "lines"), #分面文本x标签 strip.text.x = element_text(size = 8) ) + xlab("") + ylab("Assigned Probability (%)") + #按text分面 facet_wrap(~text) p 
# ??panel.spacing() 通过组合图形全方位的展示两组数据额全部特征 library(ggplot2) # install.packages("ggExtra") library(ggExtra)
# head查看矩阵前六列数据(默认) head(mtcars) ## mpg cyl disp hp drat wt qsec vs am gear carb ## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 ## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 ## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 ## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 ## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 ## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 # p <- ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, size=cyl)) + geom_point() + theme(legend.position="none") p # 边缘添加柱形图 p1 <- ggMarginal(p, type="histogram") p1 # 边缘添加密度图 p2 <- ggMarginal(p, type="density") p2 # 边缘添加 箱线图 p3 <- ggMarginal(p, type="boxplot") p3 
对边缘图形进行更多修饰library(ggplot2) library(ggExtra) head(mtcars) ## mpg cyl disp hp drat wt qsec vs am gear carb ## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 ## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 ## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 ## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 ## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 ## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 p <- ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, size=cyl)) + geom_point() + theme(legend.position="none") p # size=10 代表主图为边缘图十倍大小 p1 <- ggMarginal(p, type="histogram", size=10) p1 # fill = "slateblue"调整配色,xparams针对x轴图形进行调整 list()创建列,bin展示的柱子数, p2 <- ggMarginal(p, type="histogram", fill = "slateblue", xparams = list( bins=10)) p2 # margins = 'x'只对X轴边缘进行图形绘制 p3 <- ggMarginal(p, margins = 'x', color="purple", size=4) p3 
|