Reputation:
I don't know why but R doesn't allow me to add a line and points on my plot (i.e. pch
, lty
and lwt
don't work)
The x-axis values are factors. I just want to create a line with the observed values.
Why is this happening?
Here my code:
> df
Var1 Freq
1 Oct 234
2 Nov 100
3 Dec 1653
4 Jan 800
5 Feb 960
6 Mar 1182
7 Apr 389
8 May 1333
9 Jun 2251
10 Jul 2221
11 Aug 1012
12 Sep 362
x1 = factor(df$Var1, levels = c('Oct', 'Nov', 'Dec', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep'))
df$Freq = as.numeric(as.character(df$Freq))
plot(x1, df$Freq, type = 'b')
Upvotes: 0
Views: 142
Reputation: 50678
Is this what you're after?
# The sample data
df <- cbind.data.frame(
Var1 = c("Oct","Nov","Dec","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep"),
Freq = c(1101, 1158, 1753, 1918, 1296, 682, 389, 333, 251, 221, 312, 362));
x1 = factor(df$Var1, levels = c('Oct', 'Nov', 'Dec', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep'))
# This has no purpose as df$Freq is already numeric
df$Freq = as.numeric(as.character(df$Freq))
# Turn x1 factors into numeric values and
# plot as line plot without x-axis labels
plot(as.numeric(x1), df$Freq, type = 'l',
xaxt = "n",
xlab = "Month",
ylab = "Freq");
# Add custom x-axis with labels
# given by the levels of x1
axis(1, at = as.numeric(x1), labels = as.character(x1));
# Alternatively: Plot with points and line
plot(as.numeric(x1), df$Freq, type = 'b',
xaxt = "n",
xlab = "Month",
ylab = "Freq");
axis(1, at = as.numeric(x1), labels = as.character(x1));
Upvotes: 1