让 facet_grid 中的每个图都有自己的 Y 轴值

Wok*_*kel 4 r ggplot2

目前,我无法根据自己的喜好显示图表 Y 轴。我想要的是每个单独的图显示取决于其自身分数的点宽度。请参阅下图,了解我拥有什么以及我想要什么。

在此处输入图片说明

基本上,我希望每个图都依赖于它自己的索引,即Silhouette、Davies-Bouldin 等。就像第一张图(左边的Carlinski-Harabasz)所显示的那样。

这是迄今为止的数据和代码

algorithms <- as.factor(c(rep("kmeans", 4), rep("pam", 4), rep("cmeans", 4)))
index <- as.factor(c(rep("Silhouette", 12), rep("Carlinski-Harabasz", 12)
, rep("Davies-Bouldin",12)))
score <- c(0.12,0.08,0.07,0.05,0.1,0.07,0.09,0.08,0.13,0.11,0.1,0.1,142,106,84,74,128,
99,91,81,156,123,105,95,2.23,2.31,2.25,2.13,2.55,2.12,2.23,2.08,2.23,2.12,2.17,1.97)
clusters <- as.factor(rep(3:6,9))
indices <- data.frame(algorithms, index, score, clusters)

#Some ggplot code
ggplot(indices, aes(clusters, score)) + 
geom_point(aes(size = score, color = algorithms)) + 
facet_grid(.~index, scales = "free_y") 
#I thought the scales function in facet grid might do the trick...
Run Code Online (Sandbox Code Playgroud)

据我了解,我必须围绕 Y 轴比例进行工作。然而,事实证明这对我来说非常棘手。

更新答案

ggplot(indices, aes(clusters, score)) + 
geom_point(aes(size = score, color = algorithms)) + 
facet_wrap(~index, scales = "free_y")
Run Code Online (Sandbox Code Playgroud)

做到了。谢谢你指出。

更新了第二个答案

此外,多亏了 camille,更好的可视化是使用facet_grid2 个变量。因此,最终的代码将是:

ggplot(indices, aes(clusters, score)) + 
geom_point() + facet_grid(index ~ algorithms, scales = "free_y") + 
theme_bw() + labs(y="Score per index", x="Clusters")
Run Code Online (Sandbox Code Playgroud)

cam*_*lle 7

我遇到了这个问题,并意识到尺度的解释略有不同:在 中facet_grid每行/列方面facet_wrap的尺度可以自由更改,而使用,尺度可以根据每个方面自由更改,因为没有困难& 赋予行或列的快速含义。把它想象成grid宏观层面的缩放和wrap微观层面。

一个优点facet_grid是快速将一个变量的所有值放在一行或一列中,以便于查看发生了什么。但是您可以facet_wrap通过在单行或单列上设置方面来模拟它,就像我在下面使用nrow = 1.

library(tidyverse)

algorithms <- as.factor(c(rep("kmeans", 4), rep("pam", 4), rep("cmeans", 4)))
index <- as.factor(c(rep("Silhouette", 12), rep("Carlinski-Harabasz", 12)
                     , rep("Davies-Bouldin",12)))
score <- c(0.12,0.08,0.07,0.05,0.1,0.07,0.09,0.08,0.13,0.11,0.1,0.1,142,106,84,74,128,
           99,91,81,156,123,105,95,2.23,2.31,2.25,2.13,2.55,2.12,2.23,2.08,2.23,2.12,2.17,1.97)
clusters <- as.factor(rep(3:6,9))
indices <- data.frame(algorithms, index, score, clusters)


ggplot(indices, aes(clusters, score)) + 
  geom_point(aes(size = score, color = algorithms)) + 
  facet_grid(. ~ index, scales = "free_y") 
Run Code Online (Sandbox Code Playgroud)

ggplot(indices, aes(clusters, score)) + 
  geom_point(aes(size = score, color = algorithms)) + 
  facet_wrap(~ index, scales = "free_y", nrow = 1) 
Run Code Online (Sandbox Code Playgroud)

当您使用facet_grid两个变量时,差异更加明显。使用mpg来自的数据集ggplot2,第一个图没有自由标度,因此每行的 y 轴都有 10 到 35 之间的刻度线。也就是说,每行小平面的 y 轴是固定的。使用 时facet_wrap,每个方面都会进行这种缩放。

ggplot(mpg, aes(x = hwy, y = cty)) +
  geom_point() +
  facet_grid(class ~ fl)
Run Code Online (Sandbox Code Playgroud)

设置scales = "free_y"infacet_grid意味着每一行刻面都可以独立于其他行设置其 y 轴。因此,例如,紧凑型汽车的所有方面都受制于一个 y 尺度,但它们不受皮卡 y 尺度的影响。

ggplot(mpg, aes(x = hwy, y = cty)) +
  geom_point() +
  facet_grid(class ~ fl, scales = "free_y")
Run Code Online (Sandbox Code Playgroud)

reprex 包(v0.2.0)于 2018 年 8 月 3 日创建。