antong antong - 3 months ago 12
R Question

ggplot2 graphing two subsets

I have a CSV data set that looks like this:

GPU_Config,Job_Num,Stack_Num,Seconds
02_13,2,16double,1106
02_13,4,16double,906.25
02_13,6,16double,914.75
02_13,8,16double,982.5
02_13,10,16double,1013.25
02_13,12,16double,1067.5
02_13,16,16double,1026.25
0_1_2_3,4,16double,959.75
0_1_2_3,8,16double,847.5
0_1_2_3,12,16double,976.5
0_1_2_3,16,16double,972.75


I want to graph two
geom_smooth()
, each using a separate type of
GPU_Config
,
02_13
and
0_1_2_3
. I can use
R
's subset method within the
ggplot()
parameters like this:

ggplot(subset(test, GPU_Config %in% c("02_13", "0_1_2_3")), aes(y = Seconds, x = Job_Num, color = GPU_Config)) + geom_smooth() + geom_point()


However, this results in a graph looking like this:

Test Graph

I wish to separate the two smoothers, to be able to manipulate each individually for each data subset (
gpu type
). However, something like this below, does not work:

ggplot(test, aes(y = Seconds, x = Job_Num)) + geom_smooth(subset(test, GPU_Config %in% c("02_13"))) + geom_smooth(subset(test, GPU_Config %in% c("0_1_2_3"))) + geom_point()


and results in this error:

Error: Mapping must be created by `aes()` or `aes_()`


Could someone please help with getting this done? Note I am not an R expert by any means.

Answer

It works if you also stratify the fill colour:

ggplot(subset(test, GPU_Config %in% c("02_13", "0_1_2_3"))) +
    aes(y = Seconds, x = Job_Num, color = GPU_Config, fill = GPU_Config) +
    geom_smooth() +
    geom_point()

However, in all cases your 02_13 config runs into problems with local fitting — your code issues appropriate warnings, even in the original version. To fix this, a different smoothing method is required, e.g.:

ggplot(subset(test, GPU_Config %in% c("02_13", "0_1_2_3"))) +
    aes(y = Seconds, x = Job_Num, color = GPU_Config, fill = GPU_Config) +
    geom_smooth(method = lm) +
    geom_point()

This will separate the confidence intervals by GPU_Config.