R 使用子集重新排序()ggplot2散点图X轴';s Y值

R 使用子集重新排序()ggplot2散点图X轴';s Y值,r,ggplot2,dplyr,R,Ggplot2,Dplyr,问题 为了解决这个问题,我查看了很多SO问题/R Studio博客,但到目前为止没有任何帮助。我尝试在reorder()函数中使用各种函数,创建并使用一个广泛的数据集 输出目标 每个X点有3个Y值-一个是基准。为了显示其他两个点的性能,我需要以降序方式对基准进行排序,以创建一个类似这样的图(红色为基准): 上面的图表模拟显示了目标。请忽略少数红色异常点 当前方法 sample.chart <- ggplot(sample.data, aes( x = reorder(s

问题

为了解决这个问题,我查看了很多SO问题/R Studio博客,但到目前为止没有任何帮助。我尝试在reorder()函数中使用各种函数,创建并使用一个广泛的数据集

输出目标

每个X点有3个Y值-一个是基准。为了显示其他两个点的性能,我需要以降序方式对基准进行排序,以创建一个类似这样的图(红色为基准):

上面的图表模拟显示了目标。请忽略少数红色异常点

当前方法

sample.chart <-
  ggplot(sample.data, aes(
    x = reorder(store, -scaling),
    y = scaling,
    color=version
  )) +
  geom_point(alpha = 0.7) +
  theme(
    axis.text.x = element_blank()
  )
数据

> dput(sample.data)

structure(list(store = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
17L, 18L, 19L, 20L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L), scaling = c(3.66666666666667, 
17.5, 51, 7.6, 49, 0.333333333333333, 7.25, 13, 1.66666666666667, 
9.73333333333333, 0.307692307692308, 0.74468085106383, 5, 1.27272727272727, 
0.259259259259259, 0.866666666666667, 2.625, 1.58333333333333, 
2.71428571428571, 0.625, 5.5, 35, 51, 9.5, 49, 3, 4.83333333333333, 
8.66666666666667, 3.33333333333333, 4.17142857142857, 0.666666666666667, 
2.91666666666667, 1.42857142857143, 2.8, 0.424242424242424, 0.8125, 
1.82608695652174, 1.72727272727273, 2.375, 0.571428571428571, 
66, 62.78461538, 56.1, 53.9, 49.5, 47.3, 39.1, 39.05, 37.2, 30.8, 
29.7, 29.15, 28.6, 23.61333333, 20.8, 19.25, 18.61538462, 17.74666667, 
17.11111111, 16.8), version = c("test.1", "test.1", "test.1", 
"test.1", "test.1", "test.1", "test.1", "test.1", "test.1", "test.1", 
"test.1", "test.1", "test.1", "test.1", "test.1", "test.1", "test.1", 
"test.1", "test.1", "test.1", "test.2", "test.2", "test.2", "test.2", 
"test.2", "test.2", "test.2", "test.2", "test.2", "test.2", "test.2", 
"test.2", "test.2", "test.2", "test.2", "test.2", "test.2", "test.2", 
"test.2", "test.2", "benchmark", "benchmark", "benchmark", "benchmark", 
"benchmark", "benchmark", "benchmark", "benchmark", "benchmark", 
"benchmark", "benchmark", "benchmark", "benchmark", "benchmark", 
"benchmark", "benchmark", "benchmark", "benchmark", "benchmark", 
"benchmark")), .Names = c("store", "scaling", "version"), row.names = c(NA, 
-60L), class = c("tbl_df", "tbl", "data.frame"))
这个怎么样

ggplot(sample.data, aes(
  x = reorder(store, -scaling*(version=="benchmark"), max),
  y = scaling,
  color=version
)) +
  geom_point(alpha = 0.7) +
  theme(
    axis.text.x = element_blank()
  )
在这里,我们将非基准分数乘以0(FALSE~=0),然后取最大值,仅根据基准分数对每组重新排序

无论您如何对x轴值进行排序,因为您将它们保留为数值,所以ggplot将对它们进行数字排序。相反,您需要将它们转换为因子,按y值的降序排列。因子在技术上是以序数值存储的,因此ggplot将按这些新的整数索引对点进行排序:
图书馆(dplyr)
图书馆(供猫用)
样本数据%
arrange(version,desc(scaling))%>%#基准按字母顺序排在第一位,因此它将首先使用所有这些值进行排序
变异(存储=as_因子(as.character(存储)))
ggplot(样本数据、aes(
x=商店,
y=缩放,
颜色=版本
)) +
几何点(α=0.7)+
主题(
axis.text.x=元素_blank()
)

Whoa这真是太聪明了-因为我们在每x点上操作一个值,所以我想我也可以将该字段保留为空白(因此默认为按单个值排序?),这将产生相同的结果。@RobertTan好吧,我们实际上不是在子集设置。通过乘以布尔值,我们将一些值设置为0,而其他值保持不变。因此,如果每个
x
具有相同数量的值,则取
mean()
将给出相同的顺序,但如果某些值具有不同数量的零,则平均值将改变,即
mean(c(5,0,0))!=平均值(5)
ggplot(sample.data, aes(
  x = reorder(store, -scaling*(version=="benchmark"), max),
  y = scaling,
  color=version
)) +
  geom_point(alpha = 0.7) +
  theme(
    axis.text.x = element_blank()
  )
Regardless of how you order the x-axis values, since you keep them as numeric values, ggplot will order them numerically.  Instead, you need to turn them into factors, ordered by descending order of the y-value.  Factors are technically stored with ordinal values, so ggplot will order the points by those new integer indices instead:

library(dplyr)
library(forcats)

sample.data <- sample.data %>% 
    arrange(version, desc(scaling)) %>% #benchmark comes first alphabetically, so it will use all of those values first for sorting
    mutate(store = as_factor(as.character(store)))

ggplot(sample.data, aes(
    x = store,
    y = scaling,
    color=version
  )) +
  geom_point(alpha = 0.7) +
  theme(
    axis.text.x = element_blank()
  )