So, I have a dataset which I want to draw as bars. However, R re-orders my dataset as soon as I add colors!
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. To get the same grouping as in your first plot map backend
on the group
aes
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.