I'm trying to plot frequency polygons based on the following: vector
x: c(48, 30, 35, 31, 21, 28, 34, 43, 36, 45, ,41, 33, 47, 47, 30, 47, 44, 45, 32, 46, 47, 23, 30, 23, 49, 20, 24, 20, 40, 50)
And the sample command is:
plot(x, y, type = "b", main = "DoThi", sub = "X", xlab = "Tuoi", ylab = "TS")
The difficulty is that I can't figure out how the variable y comes into being. Can anyone help me create a variable y to look like the picture. Thanks
CodePudding user response:
I understand the question in a different way then @Peter. I understand y
as being the counts of x
as in
x <- c(48, 30, 35, 31, 21, 28, 34, 43, 36, 45, 41, 33, 47, 47, 30,
47, 44, 45, 32, 46, 47, 23, 30, 23, 49, 20, 24, 20, 40, 50)
x_coord <- sort(unique(x))
y_coord <- as.integer(table(x))
plot(x_coord, y_coord, type = "b", ylim = c(0,5))
CodePudding user response:
Picking up on @Berhard's comment that you may be looking for the frequency, i.e. count of the values in the vector x
:
x <- c(48, 30, 35, 31, 21, 28, 34, 43, 36, 45, 41, 33, 47, 47, 30, 47, 44, 45, 32, 46, 47, 23, 30, 23, 49, 20, 24, 20, 40, 50)
#frequency count for the x vector
df <- data.frame(table(x))
df1 <- data.frame(x = min(x):max(x))
# add frequency of 0 for missing integer values within the x vector range
df <- merge(df1, df, all = TRUE)
df$Freq[is.na(df$Freq)] <- 0
plot(df, type = "l", main = "DoThi", sub = "X", xlab = "Tuoi", ylab = "TS", col = "red")
Created on 2021-09-15 by the reprex package (v2.0.0)