-1

I have seen several conditional plotting as well as grouped data questions (e.g. here and here) but my problem is different. I have following sample data dat in my R dataframe. The data contains five columns, i.e., Grp (character), Var (integer) (unequal rows for each group), Col1 (numeric), Col2 (numeric), Col3 (numeric).

Grp     Var    Col1  Col2    Col3
grp_1   8      46.8  50.0   50.6
grp_1   16     95.6  47.4   48.0
grp_1   24     45.1  45.6   46.4
grp_1   32     68.8  44.3   58.2
grp_1   40     44.6  52.2   44.3
grp_1   48     86.5  42.2   68.6
grp_2   40     63.2  95.6   63.0
grp_2   60     66.7  67.5   65.6
grp_2   80     69.6  70.7   67.9
grp_2   100    71.9  73.4   69.3
grp_2   120    73.8  75.7   48.0
grp_3   500    51.9  50.0   50.5
grp_3   1000   65.5  53.0   53.4
grp_3   5000   61.2  99.0   59.9
grp_3   10000  80.1  63.0   62.8
grp_3   30000  25.9  33.8   14.2

For each group, I would like to plot columns Col1, Col2, Col3 (y-axis) against Var (x-axis), and conditionally change x-axis title as "steps", "number1", and "number2" for Var values corresponding to grp_1, grp_2 and grp_3. I see a relevant question on conditionally changing labels for two variables here but couldn't find for three variables. I need to plot above data for several groups (both in individual plots written to local disk and grid of 4 plots) so I am looking for suggestions on loop and/or function approaches. Can somebody suggest on how this could be achieved?

4
  • Using your links, please make an attempt and show (not tell) your desired output. Commented Apr 21, 2020 at 13:41
  • I am not sure I understand correctly. Your x-axis is numeric but you want to modify the axis ticks labels to show "step 1", "step 2" etc instead of 8, 16 etc? Commented Apr 21, 2020 at 14:06
  • My bad, I meant axis titles. I meant, for each group, if Var values are c(8,16.24,32,40,48) then x-axis title needs to be "steps", if c(40,60,80,100,120) then title "number1" and if c(500,1000,5000,10000,30000) then "number2". Hope it makes sense now? Commented Apr 21, 2020 at 14:15
  • @desval: Just updated the question. Commented Apr 21, 2020 at 14:16

1 Answer 1

1

I am still not sure about the desired output, you should maybe show what you have tried so far...

d <- read.table(text = 
  "Grp     Var    Col1  Col2    Col3
grp_1   8      46.8  50.0   50.6
grp_1   16     95.6  47.4   48.0
grp_1   24     45.1  45.6   46.4
grp_1   32     68.8  44.3   58.2
grp_1   40     44.6  52.2   44.3
grp_1   48     86.5  42.2   68.6
grp_2   40     63.2  95.6   63.0
grp_2   60     66.7  67.5   65.6
grp_2   80     69.6  70.7   67.9
grp_2   100    71.9  73.4   69.3
grp_2   120    73.8  75.7   48.0
grp_3   500    51.9  50.0   50.5
grp_3   1000   65.5  53.0   53.4
grp_3   5000   61.2  99.0   59.9
grp_3   10000  80.1  63.0   62.8
grp_3   30000  25.9  33.8   14.2
  ", header=T
)

library(ggplot2)
library(data.table)

d <- setDT(d)
dd <- melt(d, id.vars = c("Grp", "Var"), measure.vars = patterns(col="Col") )

ggplot(dd, aes(Var, value, color=variable)) + 
  geom_point() +
  facet_wrap(~Grp,
             scales = "free",
             labeller = as_labeller(c(grp_1 = "Step 1", grp_2 = "Step 2", grp_3 = "Step 3")  ),
             strip.position = "bottom") +
  xlab(NULL) +
  theme(strip.background = element_blank(),
        strip.placement = "outside")

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

I noticed in your answer, you used c(grp_1="Step1"....) to name the x-axis. I needed condition here, maybe ifelse, to check if x-axis values are e.g. c(8,16,24,32,40,48) then label needs to be "Steps" and so on.., please see my updated explanation in the comment above.
You will probably make your life a lot simpler if you check the condition before you plot the data, since the x-axis values are determined by your facet variable. In the data you provided, each group has different x-axis variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.