ggplot2

时间:2016-07-31 20:30:42

标签: r plot ggplot2

我在通过geom_line和stat_smooth选项可视化我的数据集时遇到问题。

以下是我通过dput创建的数据集中的示例:

example_data <- structure(list(treatment1 = c(-1.41, -1.49, -1.6, -1.65, -1.64, 
                                              -1.76, -1.73, -1.55, -1.35, -1.22, -1.36, -1.49, -1.49, -1.45, 
                                              -1.45, -1.43, -1.36, -1.45, -1.31, -1.31, -1.31, -1.19, -0.95, 
                                              -1.07, -1.07, -1.13, -1.1, -1.02, -1.01, -0.9, -0.88, -0.78, 
                                              -0.76, -0.7, -0.6, -0.33, -0.28, -0.25, -0.07, 0.18, 0.21, 0.35, 
                                              0.49, 0.54, 0.8, 1.11, 1.28, 1.41, 1.62, 1.67, 1.62, 1.66, 1.47, 
                                              1.43, 1.23, 1.02, 1.04, 0.91, 0.72, 0.67, 0.53, 0.43, 0.16, -0.04, 
                                              -0.13, -0.12, -0.15, -0.28, -0.49, -0.38, -0.65, -0.83, -0.76, 
                                              -0.76, -0.73, -0.81, -0.85, -0.86, -0.85, -0.97, -0.94, -0.99, 
                                              -1.05, -1.03, -1.01, -1.13, -1.06, -1.07, -1.18, -1.22, -1.34, 
                                              -1.39, -1.39, -1.48, -1.34, -1.46, -1.47), treatment2 = c(-1.36, 
                                                                                                        -1.39, -1.37, -1.34, -1.37, -1.4, -1.38, -1.36, -1.36, -1.35, 
                                                                                                        -1.34, -1.34, -1.32, -1.28, -1.29, -1.28, -1.22, -1.21, -1.2, 
                                                                                                        -1.17, -1.18, -1.16, -1.11, -1.08, -1.06, -1.03, -0.98, -0.95, 
                                                                                                        -0.88, -0.82, -0.77, -0.68, -0.66, -0.56, -0.48, -0.38, -0.3, 
                                                                                                        -0.19, -0.08, 0.08, 0.22, 0.34, 0.5, 0.64, 0.89, 1.18, 1.41, 
                                                                                                        1.52, 1.66, 1.68, 1.67, 1.64, 1.62, 1.53, 1.39, 1.25, 1.09, 0.93, 
                                                                                                        0.71, 0.51, 0.3, 0.11, -0.06, -0.23, -0.39, -0.53, -0.6, -0.71, 
                                                                                                        -0.81, -0.88, -0.97, -1.02, -1.07, -1.13, -1.15, -1.18, -1.2, 
                                                                                                        -1.24, -1.25, -1.32, -1.32, -1.33, -1.37, -1.38, -1.41, -1.43, 
                                                                                                        -1.42, -1.47, -1.45, -1.46, -1.44, -1.47, -1.47, -1.5, -1.49, 
                                                                                                        -1.5, -1.48), treatment3 = c(-1.28, -1.27, -1.35, -1.31, -1.37, 
                                                                                                                                     -1.25, -1.12, -0.97, -1.17, -1.04, -1.08, -1.35, -1.26, -1.33, 
                                                                                                                                     -1.16, -1.05, -0.93, -1.01, -1.06, -1.05, -0.93, -1.1, -1.12, 
                                                                                                                                     -0.93, -0.97, -0.85, -0.85, -0.76, -0.72, -0.82, -0.82, -0.58, 
                                                                                                                                     -0.61, -0.55, -0.49, -0.36, -0.3, -0.16, -0.23, -0.12, -0.02, 
                                                                                                                                     0.14, 0.22, 0.54, 0.75, 0.87, 1.35, 1.32, 1.51, 1.6, 1.7, 1.6, 
                                                                                                                                     1.59, 1.53, 1.2, 1.05, 0.78, 0.67, 0.56, 0.35, 0.13, -0.09, -0.11, 
                                                                                                                                     -0.35, -0.47, -0.5, -0.66, -0.7, -0.89, -0.88, -1.17, -1.01, 
                                                                                                                                     -1.03, -1.18, -0.98, -1.06, -1.07, -1.17, -1.18, -1.16, -1.31, 
                                                                                                                                     -1.21, -1.18, -1.3, -1.33, -1.14, -1.24, -1.21, -1.08, -1.27, 
                                                                                                                                     -1.23, -1.26, -1.26, -1.35, -1.38, -1.32, -1.32), position = c(195, 
                                                                                                                                                                                                    245, 295, 345, 395, 445, 495, 545, 595, 645, 695, 745, 795, 845, 
                                                                                                                                                                                                    895, 945, 995, 1045, 1095, 1145, 1195, 1245, 1295, 1345, 1395, 
                                                                                                                                                                                                    1445, 1495, 1545, 1595, 1645, 1695, 1745, 1795, 1845, 1895, 1945, 
                                                                                                                                                                                                    1995, 2045, 2095, 2145, 2195, 2245, 2295, 2345, 2395, 2445, 2495, 
                                                                                                                                                                                                    2545, 2595, 2645, 2695, 2745, 2795, 2845, 2895, 2945, 2995, 3045, 
                                                                                                                                                                                                    3095, 3145, 3195, 3245, 3295, 3345, 3395, 3445, 3495, 3545, 3595, 
                                                                                                                                                                                                    3645, 3695, 3745, 3795, 3845, 3895, 3945, 3995, 4045, 4095, 4145, 
                                                                                                                                                                                                    4195, 4245, 4295, 4345, 4395, 4445, 4495, 4545, 4595, 4645, 4695, 
                                                                                                                                                                                                    4745, 4795, 4845, 4895, 4945, 4995)), .Names = c("treatment1", 
                                                                                                                                                                                                                                                     "treatment2", "treatment3", "position"), row.names = c("195", 
                                                                                                                                                                                                                                                                                                            "245", "295", "345", "395", "445", "495", "545", "595", "645", 
                                                                                                                                                                                                                                                                                                            "695", "745", "795", "845", "895", "945", "995", "1045", "1095", 
                                                                                                                                                                                                                                                                                                            "1145", "1195", "1245", "1295", "1345", "1395", "1445", "1495", 
                                                                                                                                                                                                                                                                                                            "1545", "1595", "1645", "1695", "1745", "1795", "1845", "1895", 
                                                                                                                                                                                                                                                                                                            "1945", "1995", "2045", "2095", "2145", "2195", "2245", "2295", 
                                                                                                                                                                                                                                                                                                            "2345", "2395", "2445", "2495", "2545", "2595", "2645", "2695", 
                                                                                                                                                                                                                                                                                                            "2745", "2795", "2845", "2895", "2945", "2995", "3045", "3095", 
                                                                                                                                                                                                                                                                                                            "3145", "3195", "3245", "3295", "3345", "3395", "3445", "3495", 
                                                                                                                                                                                                                                                                                                            "3545", "3595", "3645", "3695", "3745", "3795", "3845", "3895", 
                                                                                                                                                                                                                                                                                                            "3945", "3995", "4045", "4095", "4145", "4195", "4245", "4295", 
                                                                                                                                                                                                                                                                                                            "4345", "4395", "4445", "4495", "4545", "4595", "4645", "4695", 
                                                                                                                                                                                                                                                                                                            "4745", "4795", "4845", "4895", "4945", "4995"), class = "data.frame")

我的目标是将所有三种处理方法绘制成一个图表,其中包含所有数据点的偏差和拟合线,因此我做了:

x <- (ggplot(example_data) 
+ geom_line(aes(x=position , y = treatment1, col = "treatment1")) 
+ stat_smooth(data = example_data, aes(x = position , y = treatment1))
+ geom_line(aes(x= position, y = treatment2, col ="treatment2"))
+ stat_smooth(data = example_data, aes(x = position , y = treatment2))
+ geom_line(aes(x=position , y = treatment3, col = "treatment3"))
+ stat_smooth(data = example_data, aes(x = position , y = treatment3)))

然而,线条非常尖锐,stat_smooth似乎并不能代表最高峰。也许我使用的是错误的工具,是否有一种方法可以表示每次处理只包含一条线,但是包含的偏差却表示数据点比stat_smooth更准确?

由于我只是R中的初学者,特别是在ggplot中,我想知道是否可以更短的时间重写笨拙的代码,而不必另外提及aes(x = ..., y= ...)

1 个答案:

答案 0 :(得分:0)

我们需要为ggplot准备数据,将治疗组名称作为颜色从宽转换为长。

library(tidyr) #gather function
library(ggplot2)

# convert from wide to long format using gather function.
plotDat <- gather(example_data, treatment, value, -position)

# then plot with colours assigned to treatment group.
ggplot(plotDat, aes(position, value, col = treatment)) +
  geom_line() +
  geom_smooth()

enter image description here