Home > Enterprise >  How do I add triangles to a ggplot to point out dosing time points?
How do I add triangles to a ggplot to point out dosing time points?

Time:01-06

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.

CodePudding user response:

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))
  • Related