简体   繁体   English

R中的哑铃图

[英]dumbbell plot in R

Hi I have series of time intervals stored in data frame df.嗨,我在数据帧 df 中存储了一系列时间间隔。

replicate ID  timeA  timeB  mean
  1            60      80    70   
  2            10      70    40   
  3            25      35    30

I am trying to plot a dumbbell:我正在尝试绘制一个哑铃:

library(ggplot2)
devtools::install_github("hrbrmstr/ggalt")
library(ggalt) 
library(dplyr)


df <- arrange(df, timeA)
#calculate mean middle point between two values
df$mean <- rowMeans(df[2:3])
#add factor levels
df <- mutate(df, rep=factor(replicateID, levels=rev(replicateID)))

gg <- ggplot(df, aes(x=timeA, xend=timeB, y=rep))
gg <- gg + geom_dumbbell(colour="#a3c4dc",
                         point.colour.l="#0e668b",
                         point.colour.r="#0000ff",
                         point.size.l=2.5,
                         point.size.r=2.5) 
gg <- gg + geom_point(aes(y = df$mean), color = "red", linetype = "dotted") 

The dumbbell plot gets plotted correctly till a certain point, however, I would like to have the middle point of each pair of values displayed on the graph too and connect all the middle values with a line.哑铃图在某个点之前被正确绘制,但是,我也希望在图形上显示每对值的中点,并将所有中间值用一条线连接起来。 I tried to do that by adding geom_point but this doesn't work.我试图通过添加geom_point来做到这一点,但这不起作用。

Any suggestion?有什么建议吗?

First of all, the parameter/aesthetic names have changed, so don't get confused.首先,参数/美学名称已更改,所以不要混淆。 If you don't update you'll have to use your parameter names.如果不更新,则必须使用参数名称。 But for geom_segment and geom_point it will be like below:但是对于geom_segmentgeom_point它将如下所示:

#data

df=structure(list(replicateID = c(2, 3, 1), timeA = c(10, 25, 60
), timeB = c(70, 35, 80), mean = c(40, 30, 70), rep = structure(3:1, .Label = c("1", 
"3", "2"), class = "factor"), time_mean = c(40, 30, 70)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -3L), groups = structure(list(
    rep = structure(1:3, .Label = c("1", "3", "2"), class = "factor"), 
    .rows = list(3L, 2L, 1L)), row.names = c(NA, -3L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE))

gg <- gg + geom_dumbbell(colour="#a3c4dc",
                         colour_x = "#0e668b",
                         colour_xend="#0000ff",
                         size_x=2.5,
                         size_xend=2.5,
                         linetype = "dotted") 
gg <- gg + geom_point(aes(x = mean), color = "red") #draws the points

gg+geom_path(aes(x=mean,group=1)) #draws the line

You could also try to use geom_line with the same parameters.您也可以尝试使用具有相同参数的geom_line Here you would get a connection from ID 1-3 which might not be what you're looking for.在这里,您将获得来自 ID 1-3 的连接,这可能不是您要找的。 I'm not sure from your question.我不确定你的问题。

在此处输入图片说明

PS: in the future please consider posting the output from dput(df) as this is easier for others to read into an r-session PS:将来请考虑发布dput(df)的输出,因为这样其他人更容易读入 r-session

Using the dumbbell R package使用哑铃 R 包

##Load some libraries
library(tidyverse)
library(ggplot2) 
library(rlang)
library(utils)
library(data.table)
library(dumbbell)


## reformat the data
df2<-df %>% mutate("key"="Time") %>% mutate("diff"=timeA-timeB)
df3<-df2%>% arrange(desc(timeA))
df2$replicateID<-factor(df2$replicateID, df3$replicateID)

##Plot
dumbbell::dumbbell(df2,key="key",id="replicateID", column1 = "timeA", column2="timeB", lab1 = "timeA", lab2="timeB", pt_val = 1, delt=1, textsize = 3)  + geom_point(aes(x = df2$mean, y=df2$replicateID), color = "red") +
geom_path(aes(x=df2$mean,y=df2$replicateID,group=1))

Dont have enough points so here is the link to the plot没有足够的积分,所以这里是情节的链接

dumbbell R package哑铃R包

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

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