0

i am a novice in R. my data looks like below - i want to plot bargraph without using any packages. the graph should have x-axis for QR(1:5) and y-axis with mean. there should five bars expected for each of 1,2,3,4 and 5. the real data has thousands of records - this is sample. any helps greatly appreciated.

         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

as described in summary section

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
Bensstats
  • 988
  • 5
  • 17
Texan
  • 15
  • 5

1 Answers1

1

a barplot is not convenient for your problem since you have multiple means for each QR. A boxplot is more suited for you, especially if you have thousands of rows. If the name of your data frame is df, here is the code :

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

Wich shows this graph :

EDIT :

If you really want to display a barplot, you could but you will have to display the standard deviation of the five categories of QR. You can use the code of this topic, which gives us this code :

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)

and here is the graph :

GBlondel
  • 26
  • 1
  • 3