多个散点图和标记点/熔化数据

时间:2018-07-12 13:02:41

标签: r plot ggplot2 scatter

不幸的是,我正在为这个愚蠢的问题创建新的帖子...

我有以下数据

Substrate observed pred.cs pred.ainslie
Alfentanil     1.60     1.9         1.50
Alprazolam     1.10     1.1         1.20
Atorvastatin     1.20     3.1         4.00
Buspirone     2.00     1.9         4.20
Cyclosporine     1.90     2.3         1.70
Felodipine     2.00     2.3         1.90
Methadone     1.10     3.1         1.20
Midazolam     1.70     1.9         1.60
Nifedipine     1.10     1.2         1.20
Nisoldipine     3.05     2.3         8.10
Sildenafil     1.20     2.0         1.10
Simvastatin     3.60     3.1         1.50
Quinidine     1.05     0.8         1.30
Tacrolimus     6.60     1.7         0.95
Triazolam     2.00     1.7         1.50

我想绘制一个散点图,其中x轴为Observed值,对于Y值,pred.cs和pred.ainslie都

我知道,要做的合理的事情是融化数据帧,使其可以由ggplot处理,但我不知道如何...

理想情况下,其外观应类似于https://i.stack.imgur.com/9udmg.jpg 在数据点周围有一个置信区间,并在外面指示那些数据点(通过其底物名称)。

如果有一种方法可以根据它们来自哪一列为点着色,那就太好了,即pred.cs说黑色和pred.ainslie是白色

很抱歉,如果这真的很基础,但是过去2个小时我一直在努力,没有任何进展!

谢谢

编辑

感谢所有回答的人,感谢您的回答。

我现在已经到了这一点(使用提供的帮助和代码):

data %>% 
  gather(val.type, value, pred.cs:pred.ainslie) %>% 
  ggplot(aes(x = observed, y = value, shape = val.type, color = "black")) + 
  geom_point(size = 3, color = "black", shape = c(rep(1,15),rep(19,15))) +
  geom_abline(intercept= 0, slope =1)+
  geom_abline(intercept= 0, slope = 0.75, linetype= "dashed")+
  geom_abline(intercept= 0, slope = 1.25, linetype= "dashed")+
  scale_shape_manual(name = "Study", values = c(pred.cs = 1, 
    red.ainslie=21))+
  theme( axis.line = element_line(colour = "black", size = 0.2, linetype= 
    "solid")) +
  scale_x_continuous(expand = c(0,0),limits = c(0,10)) +
  scale_y_continuous(expand = c(0,0),limits = c(0,10))

产生这个:https://i.stack.imgur.com/DkXmY.png

现在的问题变成了...是否有办法标记位于我创建的圆锥线之外的点?理想情况下,它将是一个黑色箭头指向带有Sustrate标识符的点

再次感谢您!

3 个答案:

答案 0 :(得分:0)

您可以组合使用dplyrggplot2。排列数据:

dat %>% 
gather(val.type, value, pred.cs:pred.ainslie) %>% 
ggplot(aes(x = observed, y = value, shape = val.type, color = Substrate)) + 
geom_point()

我想这就是你要得到的。

答案 1 :(得分:0)

数据:

data <- read.table(text="Substrate observed pred.cs pred.ainslie
                         Alfentanil    1.60     1.9         1.50
                         Alprazolam    1.10     1.1         1.20
                         Atorvastatin  1.20     3.1         4.00
                         Buspirone     2.00     1.9         4.20
                         Cyclosporine  1.90     2.3         1.70
                         Felodipine    2.00     2.3         1.90
                         Methadone     1.10     3.1         1.20
                         Midazolam     1.70     1.9         1.60
                         Nifedipine    1.10     1.2         1.20
                         Nisoldipine   3.05     2.3         8.10
                         Sildenafil    1.20     2.0         1.10
                         Simvastatin   3.60     3.1         1.50
                         Quinidine     1.05     0.8         1.30
                         Tacrolimus    6.60     1.7         0.95
                         Triazolam     2.00     1.7         1.50", header=T)

a <- data[1:2]
b <- stack(data, select=c(pred.cs, pred.ainslie))
data2 <- cbind(a, b)

ggplot(data2, aes(x=observed, y=values, colour=ind)) +
  geom_point() +
  geom_smooth(method="lm", se=FALSE)

enter image description here

答案 2 :(得分:0)

从标签问题入手:

data_label <- data %>%
gather(val.type, value, pred.cs:pred.ainslie) %>% 
mutate(label_this = ifelse(value > 1.25 * observed | value < 0.75 * observed, "YES", "NO")) %>%
filter(label_this == "YES")

data %>%
gather(val.type, value, pred.cs:pred.ainslie) %>% 
ggplot(aes(x = observed, y = value, shape = val.type, color = "black")) +
geom_point(size = 3, color = "black", shape = c(rep(1,15),rep(19,15))) +
geom_abline(intercept= 0, slope =1)+
geom_abline(intercept= 0, slope = 0.75, linetype= "dashed")+
geom_abline(intercept= 0, slope = 1.25, linetype= "dashed")+
geom_text(data = data_label, aes(x = observed, y = value, label = Substrate), nudge_y = 0.5, color = "black")+
scale_shape_manual(name = "Study", values = c(pred.cs = 1, 
red.ainslie=21))+
theme( axis.line = element_line(colour = "black", size = 0.2, linetype= "solid"),
    legend.position = "none") +
scale_x_continuous(expand = c(0,0),limits = c(0,10)) +
scale_y_continuous(expand = c(0,0),limits = c(0,10))

那会让你接近。标签是从geom_text生成的。

输出:

enter image description here

您需要继续处理位置和箭头。