簡體   English   中英

Plot box plot 按類別變量分組的變量

[英]Plot box plot of variables grouping by a categorical variable

我想 plot 所有變量的 hist 和 box plot 按cluster變量分組:

results <- kmeans(mtcars, 5)
mtcars$cluster <- results$cluster

mtcars

                     mpg cyl  disp  hp drat    wt  qsec vs am gear carb cluster         PC1         PC2
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4       4  -79.589373    2.103380
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4       4  -79.591518    2.118623
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1       5 -133.900768   -5.039705
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1       1    8.517503   44.975306
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2       1  128.685998   30.819637
Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1       4  -23.213968   35.077917
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4       3  159.318358  -32.279040
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2       5 -112.623027   39.715619
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2       4 -103.527300    7.481407
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4       4  -67.039911   -6.235141
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4       4  -66.990549   -6.233014
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3       1   55.212458  -10.371970
Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3       1   55.174696  -10.360343
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3       1   55.252387  -10.369412
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4       2  242.808289   52.527661
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4       2  236.363453   38.308137
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4       2  224.731795   16.141478
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1       5 -172.369965    6.588509
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2       5 -181.073197   17.794233
Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1       5 -179.704063    4.200915
Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1       5 -121.230928   -3.326647
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2       1   80.159574   34.980520
AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2       1   67.572806   28.891119
Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4       3  150.364099  -36.653607
Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2       2  164.646844   48.259647
Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1       5 -171.903546    6.656677
Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2       5 -123.811826    2.051162
Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2       5 -137.089266  -28.654587
Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4       3  159.422698  -53.335167
Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6       4  -64.755070  -62.972640
Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8       3  145.371926 -139.055094
Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2       5 -115.188610  -13.805581

現在我想要一個兩個不同圖中的所有變量的histBoxPlot相應地。 例如, hist 1、集群 2 中Mazda Rx 4 的歷史,集群 1 中Mazda Rx4 Wag hist歷史,集群 2 集群 3,...

誰能幫我解決這個問題。 我怎么能在ggplot中做到這一點?

嘗試分面並將數據重塑為更長的直方圖:

library(ggplot2)
library(tidyverse)
#Code
results <- kmeans(mtcars, 5)
mtcars$cluster <- results$cluster
#Plot
mtcars %>% pivot_longer(cols = -cluster) %>%
  ggplot(aes(x=value,fill=name))+
  geom_histogram()+
  facet_grid(cluster~name,scales = 'free',switch = "y")+
  theme_bw()+
  theme(panel.grid = element_blank(),
        axis.text = element_text(face='bold',color='black'),
        axis.title = element_text(face='bold',color='black'),
        strip.text = element_text(face='bold',color='black'),
        legend.text = element_text(face='bold',color='black'),
        legend.title = element_text(face='bold',color='black'),
        strip.background = element_blank())

Output:

在此處輸入圖像描述

對於箱線圖,我不確定您希望 x 軸上的每輛車如何,因為它會在 x 軸上造成混亂,因此我將為箱線圖添加代碼,並在最后一部分為您考慮使用的代碼x軸的汽車:

#Boxplots
mtcars %>% mutate(id='Var') %>%pivot_longer(cols = -c(id,cluster)) %>%
  ggplot(aes(x=id,y=value,fill=name))+
  geom_boxplot()+
  facet_grid(cluster~name,scales = 'free',switch = "y")+
  theme_bw()+
  theme(panel.grid = element_blank(),
        axis.text = element_text(angle=90,face='bold',color='black'),
        axis.title = element_text(face='bold',color='black'),
        strip.text = element_text(face='bold',color='black'),
        legend.text = element_text(face='bold',color='black'),
        legend.title = element_text(face='bold',color='black'),
        strip.background = element_blank())

Output:

在此處輸入圖像描述

以及在 x 軸上使用汽車的代碼:

#Boxplots 2
mtcars %>% mutate(id=rownames(mtcars)) %>%pivot_longer(cols = -c(id,cluster)) %>%
  ggplot(aes(x=id,y=value,fill=name))+
  geom_boxplot()+
  facet_grid(cluster~name,scales = 'free',switch = "y")+
  theme_bw()+
  theme(panel.grid = element_blank(),
        axis.text = element_text(angle=90,face='bold',color='black'),
        axis.title = element_text(face='bold',color='black'),
        strip.text = element_text(face='bold',color='black'),
        legend.text = element_text(face='bold',color='black'),
        legend.title = element_text(face='bold',color='black'),
        strip.background = element_blank())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM