简体   繁体   English

如何使用ggplot在绘图上写入某些参数的值

[英]How to write values for certain parameters on a plot using ggplot

I want to resolve the following bit for a shiny app I am developing. 对于正在开发的闪亮应用程序,我想解决以下问题。

My question is: How I can write the values for AUC, Cmax and their standard deviations on a ggplot. 我的问题是:如何在ggplot上写AUC,Cmax及其标准偏差的值。

An example data looks like this: 示例数据如下所示:

data <-
ID   TIME   DV   AUC   Cmax   SD_AUC SD_Cmax
1     0     0    1.4   4.1    0.8    0.5
1     1     0.5  1.4   4.1    0.8    0.5
1     2     2    1.4   4.1    0.8    0.5
1     3     4    1.4   4.1    0.8    0.5
1     4     6    1.4   4.5    0.8    0.5
1     5     3    1.4   4.5    0.8    0.5
1     6     2    1.4   4.5    0.8    0.5
1     7     1    1.4   4.5    0.8    0.5

Code for plotting: 绘图代码:

plotobj <- ggplot(data)
plotobj <- plotobj + geom_line(aes(x=TIME, y=DV), colour="red", size=1)

The plot should be something close to this: (I did the plot in excel for demonstration only) 该图应该与以下内容接近:(我在excel中进行了该图,仅用于演示)

在此处输入图片说明

How can I achieve this in R using ggplot ? 我如何使用ggplot在R中实现呢?

Note: The values for AUC, Cmax and SD changes (updates) depending on certain parameters in the shinyapp. 注意:AUC,Cmax和SD的值会根据Shinyapp中的某些参数进行更改(更新)。 So, the ggplot code should be general and takes whatever the value in the corresponding column is. 因此,ggplot代码应为通用代码,并采用相应列中的任何值。

I used this code for AUC as an example: 我以AUC的代码为例:

plotobj <- plotobj + annotate("text", x = 6, y = 5, label = "median AUC", colour = "red", size = 6)

but couldn't get the numbers there. 但那里没有数字。

Let's excerpt some of your data: 让我们摘录一些数据:

AUC   Cmax   SD_AUC SD_Cmax
1.4   4.1    0.8    0.5
1.4   4.1    0.8    0.5
1.4   4.1    0.8    0.5
1.4   4.1    0.8    0.5
1.4   4.5    0.8    0.5
1.4   4.5    0.8    0.5

Pretty repetitive and boring! 非常重复和无聊! This is unnecessary. 这是不必要的。 Pick your favorite data transformation method and turn it into this: 选择您喜欢的数据转换方法并将其转换为:

metrics = structure(list(Label = structure(c(1L, 3L, 2L, 4L),
    Label = c("Median AUC", "Median Cmax", "SD AUC", "SD Cmax"), class = "factor"),
    Value = c(1.4, 0.8, 4.5, 0.5)),
    .Names = c("Label", "Value"),
    class = "data.frame", row.names = c(NA, -4L))

metrics

        Label  Value
 'Median AUC'    1.4
     'SD AUC'    0.8
'Median Cmax'    4.5
    'SD Cmax'    0.5

Really, you can go straight to the next step if you'd prefer, but the data above should be easy to get to and a great starting place. 确实,如果愿意,可以直接进行下一步,但是上面的数据应该很容易获得并且是一个很好的起点。

annotation = paste(metrics$Label, metrics$Value, sep = " = ", collapse = "\n")

Then you can add to your plot object: 然后,您可以添加到绘图对象:

annotate(geom = "text" label = annotation, x = 6, y = 5)

You'll have to play with the x and y values to position it nicely. 您必须使用xy值才能很好地定位它。

Since the values for AUC and Cmax updates every time you change a parameter in the Shiny app then using geom_text is the way to go. 由于每次您在Shiny应用程序中更改参数时,AUC和Cmax的值都会更新,因此使用geom_textgeom_text的方法。 I can see the logic behind Gregor's answer above but using annotate can add text but not numbers that are changing upon updating the Shiny app. 我可以看到上面Gregor回答的逻辑,但是使用annotate可以添加文本,但不能添加在更新Shiny应用程序时会更改的数字。

The job requested by the question can be done using the following: 问题所要求的工作可以使用以下方法完成:

     yscalemax <- max(data$DV)      #set values for plot limits
      xscalemax <- max(data$TIME)*1.1
      plotobj <- ggplot(data)
      plotobj <- plotobj + geom_line(aes(x=TIME, y=DV), colour="red", size=1)
      plotobj <- plotobj + stat_summary(aes(x=TIME, y=DV), fun.y=median, geom="line", colour="red", size=1)
      plotobj <- plotobj + geom_text(aes(label = paste("AUC =",AUC)), x = xscalemax*0.8, y = yscalemax*0.6, colour = "red", size = 6)
      plotobj <- plotobj + geom_text(aes(label = paste("SD AUC =",SD_AUC)), x = xscalemax*0.8, y = yscalemax*0.55, colour = "red", size = 6)

And so on for Cmax. 以此类推。 In this case the position of the text will be adjusted depending on max(DV) and max(TIME) with the calculated values for AUC , SD_AUC sticked on the plot. 在这种情况下,文本的位置将根据max(DV)max(TIME)进行调整,并将计算出的AUCSD_AUCSD_AUC在绘图上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM