简体   繁体   English

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

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

So, I have a dataset which I want to draw as bars.所以,我有一个数据集,我想把它画成条形。 However, R re-orders my dataset as soon as I add colors!但是,一旦我添加颜色,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)

The dataset is as follows:数据集如下:

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

The output without my custom colors is:没有我的自定义颜色的输出是: 第一个打印(绘图)输出

The output with my custom colors is:我的自定义颜色的输出是: 第二次打印(绘图)输出

How do I get the first plot but with the colors of the second?如何获得第一个图但使用第二个图的颜色?

The issue is that by mapping ifelse(generic, "Yes", "No") on fill you implictily change the grouping variable.问题是,通过在fill上映射ifelse(generic, "Yes", "No")你隐式地改变了分组变量。 To get the same grouping as in your first plot map backend on the group aes获得与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.

相关问题 为什么我会收到“错误:颜色必须是有效颜色”。 在 R 中使用 Flextable? - Why do I get 'Error: color must be a valid color.' using Flextable in R? 如何更改 R 中堆积条的颜色? - How do I change the color of stacked bar in R? How do I specify a fixed number of colors to appear in a color bar that labels binned data in an R plot generated with ggplot2? - How do I specify a fixed number of colors to appear in a color bar that labels binned data in an R plot generated with ggplot2? 如何将数据传递到R中的堆积条形图? - How do I pass data to a stacked bar chart in R? 如何在 R 中为数据加载创建进度条? - How do I create a progress bar for data loading in R? 如何在 ggplot/R 中按特定顺序制作堆叠条形图,同时保持特定的配色方案? - How do I make stacked bar chart in specific order in ggplot/R while maintaining a specific color scheme? 更改 R 图中的颜色条字体 - Changing the color bar font in R plots 如何使用 R 的 ggplot2 包更改条形图的颜色? - How do I change the color of my bar plot using ggplot2 package of R? 如何更改闪避条形图,使条形遵循选定的颜色模式,而不是渐变,以及如何防止条形改变位置? - How do I change a dodged bar graph so the bars follow a selected color pattern,not a gradient, and how to keep bars from changing positions? 当已经具有 event_data(&quot;plotly_click&quot;) 时,如何使用 r、plolty、shin 为条形图中单击的条着色 - How to color a clicked bar from barchart with r, plolty, shiny when having already event_data("plotly_click")
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM