2

I am trying to connect the dots in my plot and tried geom_point()+geom_line() but it won't work.

The code below just has it for the points. Does anyone have any ideas why geom_line() does not add any line?

DensityE = read.csv("DensityElk.csv", header = TRUE)
str(DensityE)

DensityE$Date <- factor(DensityE$Date, levels=
        c("20-May","3-Jun",
      "17-Jun","1-Jul","16-Jul", 
      "22-Jul", "15-Aug"), order=TRUE)

ggplot(data=DensityE, aes(Date,Density)) + 
geom_point(aes(shape = factor(Genus)), size = 4, 
position="jitter") + 
theme_bw() + xlab("Date") +
ylab("Density per m2") + ggtitle("COP 1992") +
opts(legend.key = theme_blank()) + 
opts (legend.title = theme_blank())+
opts(legend.text = theme_text(size=9))
zx8754
  • 52,746
  • 12
  • 114
  • 209
  • 3
    Please add [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). What does `DensityElk.csv` look like? – zx8754 Dec 09 '13 at 13:32
  • Hi, the subset of the data is:1-Jul Epeorus 3.5 16-Jul Epeorus 3.25 22-Jul Epeorus 1 3-Jun Rhyacophila 1 17-Jun Rhyacophila 0.75 –  Dec 11 '13 at 01:32
  • Thank you so much for your help! I did try using the code on stack flow before but nothing would work. –  Dec 11 '13 at 01:33

1 Answers1

6

Because you're using a factor (Date) on the x-axis, ggplot2 won't automatically connect the lines across x values. Two solutions are (1) geom_line(aes(group=Genus)) or (2) geom_line(aes(x=as.numeric(Date)))

Construct data frame:

DensityE <- data.frame(
    Date=c("1-Jul","16-Jul","22-Jul","3-Jun","17-Jun"),
    Genus=c("Epeorus","Epeorus","Epeorus","Rhyacophila","Rhyacophila"),
    Density=c(3.5,3.25,1,1,0.75))

Make the plot: I have made a few changes

  • changed theme_blank to element_blank and opt to theme for consistency with recent ggplot2 releases
  • removed jittering -- if you want jittering and points and lines that connect the same points, you will have to add the jittering manually to the y values.

Code:

 library(ggplot2)
 ggplot(data=DensityE, aes(Date,Density)) + 
    geom_point(aes(shape = factor(Genus)), size = 4)+
    geom_line(aes(group=Genus))+
 theme_bw() + xlab("Date") +
 ylab("Density per m2") + ggtitle("COP 1992") +
 theme(legend.key = element_blank()) + 
 theme(legend.title = element_blank())+
 theme(legend.text = element_text(size=9))

enter image description here

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Hi, unfortunately it still didn't work. I have attached a sample of the dataset with Date, Genus and Density. I still can't figure out how to get it to work. Thank you for all your help! –  Dec 11 '13 at 01:30
  • 1-Jul Epeorus 3.5 16-Jul Epeorus 3.25 22-Jul Epeorus 1 3-Jun Rhyacophila 1 17-Jun Rhyacophila 0.75 –  Dec 11 '13 at 01:32
  • Thank you! that did work but only on the subset of data that i gave you. Unfortunately my data sheep is incredibly big and it didn't work when i used the code for the whole set of data. Im sorry, I'm really new to this and am struggling. Is there a way that i can use my big data set or do i have to make a data frame like you did for the whole thing? There would be about 300 density measurements, dates and genus. –  Dec 11 '13 at 04:00