繁体   English   中英

R 在更改条形颜色时重新排序数据。 我如何防止这种情况?

[英]R reorders data when changing bar color. How do I prevent this?

所以,我有一个数据集,我想把它画成条形。 但是,一旦我添加颜色,R 就会重新排序我的数据集!

library("ggplot2")

data <- read.csv(file = "data.csv",
                 stringsAsFactors = TRUE)

# This is what I want position-wise
plot <- ggplot(data, aes(y=median,x=backend)) +
  geom_bar(stat = "identity", aes(fill=generic), position = position_dodge2(padding = .2), alpha = 0.75) +
  labs(title="Median", y="", x="Backends",fill="Generic")
print(plot)

# This is what I want color-wise
anydsl.plot <- ggplot(data, aes(y=median,x=backend)) +
  geom_bar(stat = "identity", aes(fill=ifelse(generic, "Yes", "No")), position = position_dodge2(padding = .2), alpha = 0.75) +
  labs(title="Median", y="", x="Backends", fill="Generic")
print(plot)

数据集如下:

backend,median,minimum,maximum,generic,threads
cpu,8.70,8.40,14.06,1,1
cpu,8.10,7.65,13.23,0,1
cpu,5.77,5.54,10.04,1,2
cpu,5.42,4.24,9.34,0,2
cpu,4.44,4.12,7.07,1,3
cpu,4.26,3.79,6.82,0,3
cpu,3.70,3.52,6.42,1,4
cpu,3.77,3.48,6.04,0,4
cpu,3.49,3.20,5.40,1,5
cpu,3.53,3.08,5.45,0,5
cpu,3.42,3.08,5.23,1,6
cpu,3.51,3.20,4.79,0,6
cpu,3.46,3.17,4.84,1,7
cpu,3.48,2.98,4.75,0,7
cpu,3.46,3.14,4.76,1,8
cpu,3.47,3.12,4.60,0,8
avx,4.49,4.47,8.85,1,1
avx,4.33,4.31,8.53,0,1
avx,3.58,3.27,6.10,1,2
avx,3.49,3.25,5.99,0,2
avx,3.38,2.97,5.36,1,3
avx,3.40,3.01,5.31,0,3
avx,3.43,2.98,4.62,1,4
avx,3.38,2.84,4.72,0,4
avx,3.40,2.93,4.60,1,5
avx,3.42,2.95,4.53,0,5
avx,3.43,2.97,4.49,1,6
avx,3.42,2.80,4.47,0,6
avx,3.46,3.06,4.49,1,7
avx,3.45,2.91,4.50,0,7
avx,3.46,2.98,5.18,1,8
avx,3.47,2.98,4.42,0,8
cuda,3.04,2.98,3.10,1,1
cuda,0.33,0.32,0.34,0,1
nvvm,3.40,3.33,3.46,1,1
nvvm,2.72,2.68,2.77,0,1
opencl,3.04,3.00,3.09,1,1
opencl,0.33,0.32,0.34,0,1

没有我的自定义颜色的输出是: 第一个打印(绘图)输出

我的自定义颜色的输出是: 第二次打印(绘图)输出

如何获得第一个图但使用第二个图的颜色?

问题是,通过在fill上映射ifelse(generic, "Yes", "No")你隐式地改变了分组变量。 获得与group aes 上的第一个绘图backend相同的分组

library(ggplot2)
library(dplyr)

data <- read.table(text = "backend,median,minimum,maximum,generic,threads
cpu,8.70,8.40,14.06,1,1
cpu,8.10,7.65,13.23,0,1
cpu,5.77,5.54,10.04,1,2
cpu,5.42,4.24,9.34,0,2
cpu,4.44,4.12,7.07,1,3
cpu,4.26,3.79,6.82,0,3
cpu,3.70,3.52,6.42,1,4
cpu,3.77,3.48,6.04,0,4
cpu,3.49,3.20,5.40,1,5
cpu,3.53,3.08,5.45,0,5
cpu,3.42,3.08,5.23,1,6
cpu,3.51,3.20,4.79,0,6
cpu,3.46,3.17,4.84,1,7
cpu,3.48,2.98,4.75,0,7
cpu,3.46,3.14,4.76,1,8
cpu,3.47,3.12,4.60,0,8
avx,4.49,4.47,8.85,1,1
avx,4.33,4.31,8.53,0,1
avx,3.58,3.27,6.10,1,2
avx,3.49,3.25,5.99,0,2
avx,3.38,2.97,5.36,1,3
avx,3.40,3.01,5.31,0,3
avx,3.43,2.98,4.62,1,4
avx,3.38,2.84,4.72,0,4
avx,3.40,2.93,4.60,1,5
avx,3.42,2.95,4.53,0,5
avx,3.43,2.97,4.49,1,6
avx,3.42,2.80,4.47,0,6
avx,3.46,3.06,4.49,1,7
avx,3.45,2.91,4.50,0,7
avx,3.46,2.98,5.18,1,8
avx,3.47,2.98,4.42,0,8
cuda,3.04,2.98,3.10,1,1
cuda,0.33,0.32,0.34,0,1
nvvm,3.40,3.33,3.46,1,1
nvvm,2.72,2.68,2.77,0,1
opencl,3.04,3.00,3.09,1,1
opencl,0.33,0.32,0.34,0,1", sep = ",", header = TRUE)

# This is what I want color-wise
anydsl.plot <- ggplot(data, aes(y=median,x=backend, group = backend)) +
  geom_bar(stat = "identity", aes(fill = ifelse(generic, "Yes", "No")), position = position_dodge2(padding = .2), alpha = 0.75) +
  labs(title="Median", y="", x="Backends", fill="Generic")
print(anydsl.plot)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM