0

I have a the following table and I need to plot this to show (week in x-axis and percent in y-axis). MY following code plots nothing but gives me a message. Can someone help me to fix this?

Any help is appreciated.

dfx1:

Year  State  Cty        Week   ac_sum    percent
1998  KS     Coffey     10-1   79        6.4
1998  KS     Coffey     10-3   764       62
1998  KS     Coffey     10-4   951       77.2
1998  KS     Coffey     10-5   1015      82.4
1998  KS     Coffey     11-2   1231      100
1998  KS     Crawford   10-3   79        6.1
1998  KS     Crawford   10-4   764       15.8
1998  KS     Crawford   10-5   951       84.1
1998  KS     Crawford   11-2   1015      100
.
.
.
.


gg <- ggplot(dfx1, aes(Week,percent, col=Year))
gg <- gg + geom_line()
gg <- gg + facet_wrap(~Cty, 2, scales = "fixed")
gg <- gg + xlim(c(min(dfx1$Week), max(dfx1$Week)))
plot(gg)

geom_path: Each group consists of only one observation. Do you need to
adjust the group aesthetic?
  • Just add `group=1` to you r code and It will work. `ggplot(dfx1, aes(Week,percent, col=Year, group=1))` – S Rivero Oct 30 '17 at 18:18

2 Answers2

1

Is this what you want?

dfx1 <- read.table(text="Year  State  Cty        Week   ac_sum    percent
1998  KS     Coffey     10-1   79        6.4
1998  KS     Coffey     10-3   764       62
1998  KS     Coffey     10-4   951       77.2
1998  KS     Coffey     10-5   1015      82.4
1998  KS     Coffey     11-2   1231      100
1998  KS     Crawford   10-3   79        6.1
1998  KS     Crawford   10-4   764       15.8
1998  KS     Crawford   10-5   951       84.1
1998  KS     Crawford   11-2   1015      100", header=T)

library(ggplot2)
ggplot(dfx1, aes(Week,percent, col=Year)) + 
    geom_point() +
    facet_wrap(~Cty, 2, scales = "fixed") 

enter image description here

ggplot(dfx1, aes(Week,percent, col=Year, group=1)) + 
    geom_point() + geom_line() +
    facet_wrap(~Cty, 2, scales = "fixed") 

enter image description here

Prradep
  • 5,506
  • 5
  • 43
  • 84
1

You can look at other answers like this one to see that you're missing group = Year in your plot. Adding it in will give you what you are looking for:

library(ggplot2)

dfx1$Week <- factor(dfx1$Week, ordered = T)

ggplot(dfx1, aes(Week, percent, col = Year, group = Year)) +
  geom_line() +
  facet_wrap(~Cty, 2, scales = 'fixed')

enter image description here

With your last line it looks like you're wanting to only show the Weeks that actually have data. You can do that with scales = 'free', like so:

ggplot(dfx1, aes(Week, percent, col = Year, group = Year)) +
  geom_line() +
  facet_wrap(~Cty, 2, scales = 'free')

enter image description here

BLT
  • 2,492
  • 1
  • 24
  • 33
  • Thanks. I tried yours above but in my huge dataset I have "Week" column data starting from 6-1. I want the X-axis to be fixed. So in the plots x-axis labels starts from 10-1 and after 10-5 it shows 6-1 and then 9-1, 9-2, etc..How can I format the X-axis to start from 6-1. Right now my plot does not make any sense because of the X-axis labels. – user8848543 Oct 30 '17 at 19:01
  • Basically I need to get the min and max of X-axis labels here. – user8848543 Oct 30 '17 at 19:15
  • Looks tricky. I'm not sure you can specify differing xlims across your different facets beyond just `scales = 'free'` – BLT Oct 30 '17 at 19:55
  • OK. Then, is there as way I can order the 'Week" column from lowest ti highest before it plots. Order from for example, from 9-1, 9-2, 9-3, 9-4, etc. for each "Cty" – user8848543 Oct 30 '17 at 20:02
  • even if I use scales = "free" it did not solve the problem. – user8848543 Oct 30 '17 at 20:12
  • I apologize, I added in the line where I made `Week` an ordered factor. I mistakenly omitted it before. Then when I used `scales = 'free'` it will at least leave them in the right order. – BLT Oct 30 '17 at 23:20
  • Thanks. I tried ordering as factors of the "Weeks" but still it DID NOT correct the issue. – user8848543 Oct 31 '17 at 03:26