二维数据空间R绘图

最后发布时间:2020-06-06 13:13:50 浏览量:

图片alt

图片alt

类别变量与类别变量

library(tidyverse)
library(treemap)
# install.packages("treemap")

cjb <- read.csv("/home/wy/Downloads/cjb.csv",
                header = TRUE,
                stringsAsFactors = FALSE,
                fileEncoding = "UTF-8")
cjb %>%
  group_by(wlfk,bj,xb) %>%
  summarise(count=n()) %>%
  as.data.frame() %>%
  treemap(
    index = c("wlfk","bj","xb"),
    vSize = "count",
    vColor="count",
    type = "value"
  )

图片alt

图片alt

数值变量与数值变量

ggplot(cjb,aes(
    x=sx,
    y=sw,
    shape = wlfk,
    color =wlfk
  ))+
  geom_point(size = 2)+
  labs(x= "数学",
       y="生物",
       color="文理分科",
       shape="文理分科")

图片alt

图片alt

GGally::ggpairs(cjb,columns =4:12 )

图片alt

图片alt

计算相关系数以及绘图

cor_coef <- cor(cjb[,4:12])
cor_coef <- round(cor_coef,2)
cor_coef %>%
  as.data.frame()

图片alt

图片alt

添加列

cor_coef %>%
  as.data.frame() %>%
  rownames_to_column(var="km1")

图片alt

图片alt

行宽变换

cor_coef %>%
  as.data.frame() %>%
  rownames_to_column(var="km1") %>%
  gather(key = km2,value = cor_num,-km1)

图片alt

图片alt

数据分箱

cor_coef %>%
  as.data.frame() %>%
  rownames_to_column(var="km1") %>%
  gather(key = km2,value = cor_num,-km1) %>%
  mutate(cor_level = cut(cor_num,
                         breaks = c(0,0.3,0.5,0.8,1),
                         right = FALSE))

图片alt

图片alt

绘图

cor_coef %>%
  as.data.frame() %>%
  rownames_to_column(var="km1") %>%
  gather(key = km2,value = cor_num,-km1) %>%
  mutate(cor_level = cut(cor_num,
                         breaks = c(0,0.3,0.5,0.8,1),
                         right = FALSE)) %>%
  ggplot(aes(x=km1,y=km2,fill=cor_level))+
    geom_tile(color="white",size=1.5)+
    geom_text(aes(label=format(cor_num,digits = 2)))+
    scale_fill_brewer(palette = "YlGn",name="相关系数区间")

图片alt

图片alt

类别变量与数值变量

看看不同班级数学成绩分布

cjb$bj <- factor(cjb$bj)
ggplot(cjb,aes(x=bj,y=sx,fill=bj))+
  geom_boxplot(outlier.colour = "red",
               outlier.shape = 3,
               outlier.size = 1)+
  labs(x="班级",y="数学成绩")+
  theme(legend.position = "none")

图片alt

图片alt

library(ggridges)
library(viridis)
ggplot(cjb,aes(x=sx,y=bj,fill = ..x..))+
  geom_density_ridges_gradient(scale=2,
                               rel_min_height=0.01,
                               gradient_lwd = 1)+
  scale_fill_viridis(name="数学成绩",
                     option = "C")+
  labs(x="数学",y="班级")

图片alt

图片alt