0

The task at hand is to have all 17 counties charted at the same time in R. Hopefully, each line would be a different color. I know how to produce a single line chart for a single county. The question is how to get all 17 to show on one line chart in R.

Some background. The dataset that this is drawn from has all 17 counties and a statewide value in it. A small sample is below

      type      area      year     periodtype     period      unemprate
       04      000003     2020          03          01            4.2
       04      000003     2020          03          02            4.3
       04      000005     2020          03          01            4.6
       04      000005     2020          03          02            4.7
       04      000007     2020          03          01            4.9
       04      000007     2020          03          02            5.6

This is the code that I have been using for creating the single line chart

First, this creates the data frame

        labforce1m <- dbGetQuery(connection, "SELECT area,year,period,unemprate FROM labforce")

Second, this creates the date since this does not exist in the data

        labforce1m$date<- ymd(paste(labforce1m$year,labforce1m$period,"1",sep="-"))

Third, to produce the chart

         Countyxyz %>%
         Filter (area == '000003')
         ggplot(data=labforce1m, aes(x=date, y=unemprate))+geom_line()

I know that the following will do the required task but all lines will be the same color

        ggplot(data=labforce1m, aes(x=date, y=unemprate, group = 
        area))+geom_line()

My intuition is that I have to use a loop but nothing that I have read online is simple enough to retrofit to my simple example.

Tim Wilcox
  • 1,275
  • 2
  • 19
  • 43
  • 2
    Try to change `group` to `color` as in `aes(x=date, y=unemprate, color = factor(area))`. And remove the `filter`. – Rui Barradas Sep 29 '20 at 20:39
  • @RuiBarradas, That works. So that you get credit, write that up as an answer. – Tim Wilcox Sep 29 '20 at 20:41
  • I don't believe I should post as an answer, there are already some equivalent answers, like [this one](https://stackoverflow.com/a/53662161/8245406) if you want to give credit. That's even one of the goals of SO, to help the asker and others in the future :). – Rui Barradas Sep 29 '20 at 21:46

0 Answers0