不使用包的Barplot

时间:2019-05-21 15:54:16

标签: r

我是R的新手。我的数据如下所示-我想在不使用任何程序包的情况下绘制条形图。该图应具有QR(1:5)的x轴和带有平均值的y轴。 1,2,3,4和5中的每一个都应该有五个条形。实际数据有成千上万的记录-这是示例。任何帮助都将不胜感激。

         date    QR        mean
1    6/29/2018     1        1.0336214
2    6/29/2018     2        1.0331653
3    6/29/2018     3        1.0323717
4    6/29/2018     4        1.0203561
5    6/29/2018     5        1.0051536
6    5/31/2018     1        1.0161869
7    5/31/2018     2        1.0187350
8    5/31/2018     3        1.0114061
9    5/31/2018     4        1.0192010
10   5/31/2018     5        1.0264293
11   4/30/2018     1        1.0173212
12   4/30/2018     2        1.0057920
13   4/30/2018     3        1.0284010
14   4/30/2018     4        1.0360230
15   4/30/2018     5        1.0195000
16   3/29/2018     1        0.9989350
17   3/29/2018     2        0.9981820
18   3/29/2018     3        0.9992832
19   3/29/2018     4        1.0237889
20   3/29/2018     5        1.0490677

如摘要部分所述

H <- mean
M <- QR
barplot.default(M,H,
         main = "Mean vs QR",
         xlab = "QR",
         ylab = "Mean",
         col  =   "Blue"
)

expected - five bars(in col QR - 1,2,3,4,5)  against the corresponding mean

1 个答案:

答案 0 :(得分:0)

由于您对每个QR都有多种方法,因此绘制小图不方便解决您的问题。箱形图更适合您,特别是如果您有数千行。 如果数据框的名称为df,则代码如下:

boxplot(mean ~ QR,
    data=df,
    main = "Mean vs QR",
    xlab = "QR",
    ylab = "Mean",
    col  =   "Blue"
)

会显示此图:

编辑:

如果您确实要显示条形图,可以,但是必须显示QR的五个类别的标准偏差。 您可以使用this topic的代码,该代码为我们提供:

df_QR1 <- subset(df, subset = (QR==1))
df_QR2 <- subset(df, subset = (QR==2))
df_QR3 <- subset(df, subset = (QR==3))
df_QR4 <- subset(df, subset = (QR==4))
df_QR5 <- subset(df, subset = (QR==5))

mean_1 <- mean(df_QR1$mean)
sd_1 <- sd(df_QR1$mean)
mean_2 <- mean(df_QR2$mean)
sd_2 <- sd(df_QR2$mean)
mean_3 <- mean(df_QR3$mean)
sd_3 <- sd(df_QR3$mean)
mean_4 <- mean(df_QR4$mean)
sd_4 <- sd(df_QR4$mean)
mean_5 <- mean(df_QR5$mean)
sd_5 <- sd(df_QR5$mean)

means <- c(mean_1, mean_2, mean_3, mean_4, mean_5)
sds <- c(sd_1, sd_2, sd_3, sd_4, sd_5)
names(means) <- c("QR1", "QR2", "QR3", "QR4", "QR5")
names(sds) <- c("QR1", "QR2", "QR3", "QR4", "QR5")

bar_plot <- barplot(height = means,
    main = "Mean vs QR",
    xlab = "QR",
    ylab = "Mean",
    col  =   "Blue",
    ylim=c(0, max(means)+max(sds))
)

arrows(x0=bar_plot, y0=means+sds, x1=bar_plot, y1=means-sds,angle=90,code=3)

这是图形: