[英]Let each plot in facet_grid have its own Y-axis value
Currently I'm having trouble with displaying the chart Y-axis to my likings. 目前,我无法按照自己的喜好显示图表Y轴。 What I want is that each separate plot shows the point width that depends on its own score.
我想要的是每个单独的图都显示点宽度,该点宽度取决于其自己的分数。 See the image below to see what I've got and what I want.
请看下面的图片,看看我有什么和想要什么。
Basically, I want each plot to be dependent on its own index, ie Silhouette, Davies-Bouldin etc. Just like the first graph (Carlinski-Harabasz on the left) is showing. 基本上,我希望每个图都依赖于它自己的索引,即Silhouette,Davies-Bouldin等。就像第一个图(左侧的Carlinski-Harabasz)显示一样。
This is the data and the code so far 这是到目前为止的数据和代码
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...
To my understanding I have to work around the Y-axis scale. 据我了解,我必须围绕Y轴比例尺工作。 However, this proves to be quite tricky for me.
但是,这对我来说非常棘手。
ggplot(indices, aes(clusters, score)) +
geom_point(aes(size = score, color = algorithms)) +
facet_wrap(~index, scales = "free_y")
Did the trick. 做到了。 Thanks for pointing it out.
感谢您指出。
In addition, thanks to camille, a better visualization is to use facet_grid
with 2 variables. 此外,感谢camille,更好的可视化方法是使用带有2个变量的
facet_grid
。 Therefore, the final code will be: 因此,最终代码将是:
ggplot(indices, aes(clusters, score)) +
geom_point() + facet_grid(index ~ algorithms, scales = "free_y") +
theme_bw() + labs(y="Score per index", x="Clusters")
I've had this problem, and realized the scales have slightly different interpretations: in facet_grid
, the scales are free to change per row / column of facets , whereas with facet_wrap
, the scales are free to change per facet , since there isn't a hard & fast meaning given to the rows or columns. 我遇到了这个问题,并意识到比例尺的解释略有不同:在
facet_grid
,比例尺可以按行/列的构面自由更改,而对于facet_wrap
,比例尺可以按构面自由更改,因为没有行或列的硬性和快速性含义。 Think of it like grid
does macro-level scaling and wrap
does micro-level. 可以将其视为
grid
宏级缩放,而wrap
微级。
One advantage that facet_grid
has is quickly putting all values of one variable in a row or column together, making it easy to see what's going on. facet_grid
一个优点是快速将一个变量的所有值放在一行或一列中,从而很容易看到正在发生的事情。 But you can mimic that in facet_wrap
by setting the facets up on a single row or column, as I did below with nrow = 1
. 但是您可以通过在
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)
The difference is more clear when you're using facet_grid
with two variables. 当您将
facet_grid
与两个变量一起使用时,区别更加明显。 Using the mpg
dataset from ggplot2
, this first plot doesn't have free scales, so each row's y-axis has tick marks between 10 and 35. That is, the y-axes of each row of facets are fixed. 使用来自
ggplot2
的mpg
数据集,该第一个图没有自由比例,因此每行的y轴上的刻度线在10到35之间。也就是说, 每行构面的y轴是固定的。 With facet_wrap
, this scaling would take place for each facet. 使用
facet_wrap
,将对每个构面进行缩放。
ggplot(mpg, aes(x = hwy, y = cty)) +
geom_point() +
facet_grid(class ~ fl)
Setting scales = "free_y"
in facet_grid
means that each row of facets can set its y-axis independent of the other rows. 在
facet_grid
设置scales = "free_y"
意味着每行构面可以独立于其他行设置其y轴。 So, for example, all facets of compact cars are subject to one y-scale, but they're unaffected by the y-scale of pickups. 因此,例如,紧凑型汽车的所有方面都受一个y尺度的约束,但不受皮卡y尺度的影响。
ggplot(mpg, aes(x = hwy, y = cty)) +
geom_point() +
facet_grid(class ~ fl, scales = "free_y")
Created on 2018-08-03 by the reprex package (v0.2.0). 由reprex软件包 (v0.2.0)创建于2018-08-03。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.