0

I'm working on my research project and I want to add something to my ggplot. I have concentration-time graphs and I want to point out at what point a dose is given. I need to point this out with a triangle just above the graph so that the reader knows at what point a dose is given. An example of what I mean is added underneath.

Graph with triangles that point out dosing time points

The data is sensitive, so I can't give you that, but the idea is simple. It's concentration-time data. My code for the actual graph is:

ggplot(data = df, aes(x = "Time", y = "Concentration", col = "Species"))
+ ylab("Concentration (mg/mL)") + xlab ("Time (h)")
+ geom_point() + scale_color_viridis(discrete = T, option = "F", begin = 0, end = 0.8)
+ theme_bw() + scale_y_log10()

I know that there is an annotation() function, but I don't think there's an option for adding triangles to the graph. I haven't tried anything else yet, because I don't know what other options there are. I hope someone can help me with this problem.

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
AsGi
  • 17
  • 7
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. If you can't supply the real data, that's fine. But you should at least include some fake/sample data that can be used for testing. – MrFlick Jan 05 '23 at 15:40
  • 1
    @MrFlick thanks for your comment, I didn't think it would be necessary, but I will try to add some data asap. – AsGi Jan 05 '23 at 15:45
  • 3
    With `annotate` you can use a `point` geom, and the if you use `shape = 25` you can get an down-pointing filled-in triangle (see `?pch` for the built-in shapes). – Gregor Thomas Jan 05 '23 at 15:49

1 Answers1

3

Suppose your administration times are at 1, 6, 12 and 18 hours. Then you could do:

admin_times <- c(1, 6, 12, 18)

and

ggplot(data = df, aes(x = Time, y = Concentration, col = Species)) +
  ylab("Concentration (mg/mL)") + 
  scale_x_continuous("Time (h)", breaks = 0:4 * 6, limits = c(1, 24)) +
  geom_point() + 
  scale_color_viridis_d(option = "F", begin = 0, end = 0.8) +
  theme_bw() + 
  scale_y_log10() +
  annotate('point', x = admin_times, y = max(df$Concentration)*2,
           shape = 25, size = 6, color = 'gray80', fill = 'gray80')

enter image description here

Note that you don't put quotation marks around column names inside aes when creating a ggplot.


Data used:

df <- data.frame(Time = rep(1:24, 2),
                 Concentration = dexp(c(1:24, 1:24), 
                                      rep(c(0.1, 0.15), each = 24)),
                 Species = rep(c('A', 'B'), each = 24))
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87