multilayers shadow with geom_react

Hi all,

I have data frame to visualize with ggplot2 and geom_rect function, this is the data with command I used

a.d = read.table(text="Taxa	Time	Treatment	Value
ASV23	T6	Ph1	0.049406532
ASV38	T6	Ph1	0.052542339
ASV125	T6	Ph1	0.092199603
ASV193	T6	Ph1	0.010195662
ASV219	T6	Ph1	0.09468832
ASV23	T12	Ph1	0.031924423
ASV38	T12	Ph1	0.18665094
ASV125	T12	Ph1	0.12314535
ASV193	T12	Ph1	0.031932642
ASV219	T12	Ph1	0.60457291
ASV23	T24	Ph1	0.049734573
ASV38	T24	Ph1	0.205076045
ASV125	T24	Ph1	0.162962756
ASV193	T24	Ph1	0.00786734
ASV219	T24	Ph1	0.16383689
ASV23	T48A	Ph1	0.049406532
ASV38	T48A	Ph1	0.052542339
ASV125	T48A	Ph1	0.092199603
ASV193	T48A	Ph1	0.010195662
ASV219	T48A	Ph1	0.09468832
ASV23	T48B	Ph2	0.049406532
ASV38	T48B	Ph2	0.052542339
ASV125	T48B	Ph2	0.092199603
ASV193	T48B	Ph2	0.010195662
ASV219	T48B	Ph2	0.09468832
ASV23	T66	Ph2	0.031924423
ASV38	T66	Ph2	0.18665094
ASV125	T66	Ph2	0.12314535
ASV193	T66	Ph2	0.031932642
ASV219	T66	Ph2	0.40457291
ASV23	T96A	Ph2	0.049734573
ASV38	T96A	Ph2	0.205076045
ASV125	T96A	Ph2	0.162962756
ASV193	T96A	Ph2	0.00786734
ASV219	T96A	Ph2	0.16383689
ASV23	T96B	Ph3	0.049734573
ASV38	T96B	Ph3	0.205076045
ASV125	T96B	Ph3	0.162962756
ASV193	T96B	Ph3	0.00786734
ASV219	T96B	Ph3	0.16383689
ASV23	T108	Ph3	0.049406532
ASV38	T108	Ph3	0.052542339
ASV125	T108	Ph3	0.092199603
ASV193	T108	Ph3	0.010195662
ASV219	T108	Ph3	0.09468832
ASV23	T120	Ph3	0.031924423
ASV38	T120	Ph3	0.18665094
ASV125	T120	Ph3	0.12314535
ASV193	T120	Ph3	0.031932642
ASV219	T120	Ph3	0.20457291
ASV23	T144	Ph3	0.049734573
ASV38	T144	Ph3	0.205076045
ASV125	T144	Ph3	0.162962756
ASV193	T144	Ph3	0.00786734
ASV219	T144	Ph3	0.16383689",
                 header=TRUE)
####
(a.d %>%
    arrange(Treatment, Value) %>%
    group_by(Treatment) %>%
    mutate(
      Xmin = min(Time),
      Xmax = max(Time)
    ) %>%
    ungroup() %>%
    mutate(Treatment = as.factor(Treatment)) -> dt_for_plot)

minrate <- min(dt_for_plot$Value)
maxrate <- max(dt_for_plot$Value)

(rect_data <- dt_for_plot %>% select(Treatment, Xmin, Xmax) %>% distinct())

(alt_rect_data <- mutate(rect_data,
                         Xmax3=lead(Xmin),
                         Xmax=if_else(is.na(Xmax3),Xmax,Xmax3)))

ggplot(a.d) +
  geom_rect(
    data = alt_rect_data, 
    mapping = aes(
      xmin = Xmin,
      xmax = Xmax,
      ymin = minrate,
      ymax = maxrate,
      fill = Treatment,
      ymin=0, ymax=5, alpha=0.2)) +
  geom_line(aes(x = Time, y = Value, 
                group = Taxa, color = Taxa,
                linetype = Taxa),
            size = 0.8) + 
  geom_point(aes(y= Value, x=Time, shape = Taxa), 
             color="navy", size=1.5) + 
  scale_shape_manual(values = c(16, 14, 22, 17, 15, 23)) +  
  scale_fill_brewer("Taxa", palette = "Blues") +
  theme_classic() + scale_x_discrete(limits = c("T6", "T12", "T24", "T48A", "T48B", "T66", "T96A", "T96B", "T108", "T120", "T144")) 

this was the output

my question is how to make the color start from time6 not form 12 and at the end to be till T144?

thanking your help

Your issue stems from this bit:

Your time is a character object, so it sorts in alpha-numerical order rather than what you consider to be chronological order:

a.d = read.table(text="Taxa Time    Treatment   Value
ASV23   T6  Ph1 0.049406532
ASV38   T6  Ph1 0.052542339
# <...your data here...>
ASV219  T144    Ph3 0.16383689",
                 header=TRUE)

sort(a.d$Time)
#>  [1] "T108" "T108" "T108" "T108" "T108" "T12"  "T12"  "T12"  "T12"  "T12" 
#> [11] "T120" "T120" "T120" "T120" "T120" "T144" "T144" "T144" "T144" "T144"
#> [21] "T24"  "T24"  "T24"  "T24"  "T24"  "T48A" "T48A" "T48A" "T48A" "T48A"
#> [31] "T48B" "T48B" "T48B" "T48B" "T48B" "T6"   "T6"   "T6"   "T6"   "T6"  
#> [41] "T66"  "T66"  "T66"  "T66"  "T66"  "T96A" "T96A" "T96A" "T96A" "T96A"
#> [51] "T96B" "T96B" "T96B" "T96B" "T96B"
min(a.d$Time)
#> [1] "T108"
max(a.d$Time)
#> [1] "T96B"

To resolve, you could try converting the time into a factor object and setting the levels using the vector you've used in scale_x_discrete():

a.d %>%
  mutate(Time = factor(Time, levels = c("T6", "T12", "T24", "T48A", "T48B", "T66", "T96A", "T96B", "T108", "T120", "T144")))

and then continuing as before.

1 Like

Hi @keithn
I appreciated your reply and help,

The time in the df was changed to be a numeric, that solved the issue with same code. But, the output looks like

then the scale_x_discrete() was removed this resulted in remove the time chronological order
and make it as follows 0, 50, 100 , and150

do you have any suggestion?
Thank you

You'll have to show what a.d looks like with Time converted to numeric because it's not clear how T48A and T48B convert to distinct times. Show us the output of running dput(a.d) after the Time conversion.

Alternatively, try this code which leaves the x-axis discrete:

(a.d %>%
    mutate(Time = factor(Time, levels = c("T6", "T12", "T24", "T48A", "T48B", "T66", "T96A", "T96B", "T108", "T120", "T144"), ordered = TRUE)) %>% 
    arrange(Treatment, Value) %>%
    group_by(Treatment) %>%
    mutate(
      Xmin = min(Time),
      Xmax = max(Time)
    ) %>%
    ungroup() %>%
    mutate(Treatment = as.factor(Treatment)) -> dt_for_plot)

minrate <- min(dt_for_plot$Value)
maxrate <- max(dt_for_plot$Value)

(rect_data <- dt_for_plot %>% select(Treatment, Xmin, Xmax) %>% distinct())

(alt_rect_data <- mutate(rect_data,
                         Xmax3=lead(Xmin),
                         Xmax=if_else(is.na(Xmax3),Xmax,Xmax3)))

ggplot(a.d) +
  geom_rect(
    data = alt_rect_data, 
    mapping = aes(
      xmin = Xmin,
      xmax = Xmax,
      ymin = minrate,
      ymax = maxrate,
      fill = Treatment,
      ymin=0, ymax=5, alpha=0.2)) +
  geom_line(aes(x = Time, y = Value, 
                group = Taxa, color = Taxa,
                linetype = Taxa),
            size = 0.8) + 
  geom_point(aes(y= Value, x=Time, shape = Taxa), 
             color="navy", size=1.5) + 
  scale_shape_manual(values = c(16, 14, 22, 17, 15, 23)) +  
  scale_fill_brewer("Taxa", palette = "Blues") +
  theme_classic() + scale_x_discrete(limits = c("T6", "T12", "T24", "T48A", "T48B", "T66", "T96A", "T96B", "T108", "T120", "T144"))
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.