[英]Let each plot in facet_grid have its own Y-axis value
目前,我无法按照自己的喜好显示图表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...
据我了解,我必须围绕Y轴比例尺工作。 但是,这对我来说非常棘手。
ggplot(indices, aes(clusters, score)) +
geom_point(aes(size = score, color = algorithms)) +
facet_wrap(~index, scales = "free_y")
做到了。 感谢您指出。
此外,感谢camille,更好的可视化方法是使用带有2个变量的facet_grid
。 因此,最终代码将是:
ggplot(indices, aes(clusters, score)) +
geom_point() + facet_grid(index ~ algorithms, scales = "free_y") +
theme_bw() + labs(y="Score per index", x="Clusters")
我遇到了这个问题,并意识到比例尺的解释略有不同:在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")
ggplot(indices, aes(clusters, score)) +
geom_point(aes(size = score, color = algorithms)) +
facet_wrap(~ index, scales = "free_y", nrow = 1)
当您将facet_grid
与两个变量一起使用时,区别更加明显。 使用来自ggplot2
的mpg
数据集,该第一个图没有自由比例,因此每行的y轴上的刻度线在10到35之间。也就是说, 每行构面的y轴是固定的。 使用facet_wrap
,将对每个构面进行缩放。
ggplot(mpg, aes(x = hwy, y = cty)) +
geom_point() +
facet_grid(class ~ fl)
在facet_grid
设置scales = "free_y"
意味着每行构面可以独立于其他行设置其y轴。 因此,例如,紧凑型汽车的所有方面都受一个y尺度的约束,但不受皮卡y尺度的影响。
ggplot(mpg, aes(x = hwy, y = cty)) +
geom_point() +
facet_grid(class ~ fl, scales = "free_y")
由reprex软件包 (v0.2.0)创建于2018-08-03。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.