antong antong - 3 months ago 16
R Question

Different Grouped Bar Plot in ggplot2

Using this set of data below:

GPU_Config,Job_Num,File_Amount,Seconds
0_1_2_3,4,1400,44015.833
0_1_2_3,8,1400,35509
0_1_2_3,12,1400,40544.75
02_13,2,1400,50209.5
02_13,4,1400,39379
02_13,6,1400,39507
02_13,8,1400,39871
02_13,10,1400,42823.75
02_13,12,1400,46727


Is there any way to make a bar graph of this sort with this specific set of data?:

enter image description here

I've tried things like this:

ggplot(jobtest, aes(x = GPU_Config, y = Seconds, fill = GPU_Config, color = GPU_Config)) + geom_bar(position = position_dodge(.908), stat = "identity")


Interestingly enough, setting position to position_jitter() or position_jitterdodge() does start to separate the bars a little bit, however just doing dodge does nothing at all.

But they don't seem to work. A lot of other R posts concerning grouping bar graphs are all slightly different and difficult to understand. I'm not an R expert by any means. Can someone please help?

EDIT




More new information come to light. Editing the data by adding a "Type" Column at the end of the CSV, setting each row to be a different letter, A-I, and setting Fill to equal "Type" works, however obviously all the bars are now different colors. Is there no way to group them in the same color?

New Data:

GPU_Config,Job_Num,Tiff_Amount,Seconds,Type
0_1_2_3,4,1400,44015.833,a
0_1_2_3,8,1400,35509,b
0_1_2_3,12,1400,40544.75,c
02_13,2,1400,50209.5,d
02_13,4,1400,39379,e
02_13,6,1400,39507,f
02_13,8,1400,39871,g
02_13,10,1400,42823.75,h
02_13,12,1400,46727,i


New Code:

ggplot(jobtest, aes(x = GPU_Config, y = Seconds, fill = Type, color = "Black")) + geom_bar(position = position_dodge(), stat = "identity")


enter image description here

Answer

Something like this?

text <- 
"GPU_Config,Job_Num,File_Amount,Seconds
0_1_2_3,4,1400,44015.833
0_1_2_3,8,1400,35509
0_1_2_3,12,1400,40544.75
02_13,2,1400,50209.5
02_13,4,1400,39379
02_13,6,1400,39507
02_13,8,1400,39871
02_13,10,1400,42823.75
02_13,12,1400,46727"

df <- read.table(textConnection(text), sep = ",", header = TRUE)

df$type <- as.factor(1:nrow(df))

library(ggplot2)
  ggplot(df) +
  geom_bar(aes(x = type, y = Seconds, fill = GPU_Config), 
  stat = "identity", position = "dodge") +
  facet_wrap(~ GPU_Config, scales = 'free_x')

enter image description here

If you don't want the faceting separation/want the GPU_config on the X-axis you can hide the color guide and manually specify the colors

ggplot(df) +
  geom_bar(aes(x = GPU_Config, y = Seconds, fill = type), 
  colour = 'black', stat = "identity", position = "dodge") +
  scale_fill_manual(guide = FALSE,
                    values=c("red", "red", "red", 
                            'blue', 'blue', 'blue', 'blue', 'blue', 'blue')
  )

enter image description here