1

EDIT: I have tried what has been suggested to me but na.omit is plotting my NAs as if they are a value. I have also updated my post with some sample data, and a new ggplot script to reflect suggestions made to me.

I am trying to plot monthly data in ggplot, using geom line. However, there are months where I do not have data. For example, I have data for the months of, April, May, June, and July, no data in August, and then data again in September. What I would like to do is plot months, including the months without data in order to represent time scale correctly. I have quite a few gaps where there is no data for several months (due to a shift from monthly to seasonal surveillance), but would still like all months to show general trend.

Below is some sample data (all columns are Factors), my code, and a screen grab of my output.

Sample Data

Material    Month RelativeFrequency
1    Compost Apr 2017  29.2817679558011
2     Hybrid Apr 2017   37.292817679558
3 Wood Chips Apr 2017  33.4254143646409
4    Compost May 2017  28.8401253918495
5     Hybrid May 2017  34.4827586206897
6 Wood Chips May 2017  36.6771159874608
7    Compost Aug 2017  NA
8    Hybrid  Aug 2017  NA
9 Wood Chips Aug 2017  NA

Script

  library (ggplot2)
  ggplot(data = Mound.Freq, aes(x = Month, y = RelativeFrequency, color = 
  Material, group =
  Material))+
  geom_point(data = na.omit(Mound.Freq))+
  geom_line(data = na.omit(Mound.Freq))+
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

Sample Output

Thanks!

Ko Jo
  • 9
  • 3
  • welcome to SO. You'll have a higher probability of getting answers if your question is a reproducible question. See this [post](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on how to create one. – mnm Jun 19 '18 at 00:56

2 Answers2

0

Let's create some dummy data, as the OP has not provided one.

# create some dummy data
R> df <- data.frame(A = 1:10, B = 11:20, c = 21:30)

Next, I introduce some missing values.

R> set.seed(4)
R> df<- as.data.frame(lapply(df,function(cc) cc[ sample(c(TRUE, NA), prob = c(0.85, 0.15), size = length(cc), replace = TRUE) ]))
R> head(df)
  A  B  c
1 1 11 21
2 2 NA NA
3 3 13 23
4 4 NA 24
5 5 15 25
6 6 16 26

Now, let's try to plot the variable A and B using both the line and dot plot, so that its easy to visualize the missing values.

R> library(ggplot2)
R> ggplot(data = df, aes(x=A, y=B))+
   geom_point()+ 
   geom_line()
Warning messages:
1: Removed 4 rows containing missing values (geom_point). 
2: Removed 2 rows containing missing values (geom_path).

enter image description here

The warning message is telling us, that geom_line() has automatically removed the missing values. It also suggest to use geom_path() instead. So to plot the missing values, we can do something like this;

R> ggplot(data = df, aes(x=A, y=B))+
       geom_point(data = na.omit(df))+
       geom_line(data = na.omit(df))

enter image description here

mnm
  • 1,962
  • 4
  • 19
  • 46
  • Hi, thanks for you advice and suggestions! I have taken them into account but have not been able to replicate your results with my data. I have edited my original post with some sample data as well as a new script and output that reflect your suggestions. As you will see, the NAs are being plotted but as if NA is a value. I would like the months with NA to be on the x-axis but have the NAs ignored as you have demonstrated in your example. Any more help is much appreciated, thanks! – Ko Jo Jun 21 '18 at 01:13
0

Note sure if you ever got an answer to this, but I was trying to do the same thing. Below is my rather crude solution. It will need some refining. I basically put an if statement for x and y

    geom_line(  data = subData[!is.na(as.numeric(subData $Hwa2)),] ,

            aes(    if (length (na.omit(as.numeric(subData $Hwa2))) == 0) {
                    x = as.POSIXct(strptime(paste(yr,i,1, sep = "-"),"%Y-%m-%d"), tz = "GMT")
                } else {
                    x = as.POSIXct(subData $DateTime[!is.na(subData $Hwa2)], origin = "1970-01-01")
                },
                if (length (na.omit(as.numeric(subData $Hwa2))) == 0) {
                    y = 0
                } else {
                    y = na.omit(as.numeric(subData $Hwa2))
                }

            , color = "FM-13"),


            size = 0.5, 
            alpha = 1)