I'm fairly new to R and I've been having trouble with a plot.
I'm trying to create a line plot with: $YEAR on the X axis $METRIC on the Y axis a different-colored line for each country (meaning, a total of 3 lines on the same plot)
$COUNTRY is a factor with 3 levels
COUNTRY YEAR METRIC
USA 2000 14.874
USA 2001 15.492
USA 2002 13.091
USA 2003 14.717
CAN 1999 15.031
CAN 2000 14.343
CAN 2001 12.972
CAN 2002 13.216
SWE 1999 14.771
SWE 2000 17.033
SWE 2001 15.932
SWE 2002 14.516
SWE 2003 15.655
When I create the plot with
plot(df$YEAR, df$METRIC, col=df$COUNTRY, type="p")
I get a plot with points for each (x,y) combination and different color for each level of the factor $COUNTRY
However, when I try to get a line for each country, with
plot(df$YEAR, df$METRIC, col=df$COUNTRY, type="l")
I get one non-stopping line, that starts with the 4 observations of "USA" and then goes back to the first year of the next country ("CAN").
Can anyone explain why is this happening? Is it possible to create this plot using only the pre-built functions?
Thank you in advance for any assistance.
plot
doesn't work likeggplot2
: to saycol=df$COUNTRY
does not mean it is going to group the lines, separating the endpoints, and apply colors intuitively. I think your immediate options (1) make a single base plot, then add each country's data as an individual call tolines
; (2) usesegments
and placeNA
s between each country (fragile and too much work, typically); or (3) switch toggplot2
orlattice
where grouping/faceting like what you want is a bit more natural. – r2evanslibrary(ggplot2); ggplot(df) + geom_line(aes(YEAR, METRIC, color=COUNTRY))
is more likely what you're looking for, and is relatively easy to read once you start migrating your thought-process from base-R to grammar-of-graphics. – r2evanslines
(and thereforeplot(..., type=";")
) will only use the first color unlesstype="h"
(which is not what you are trying to do here). – r2evans